码迷,mamicode.com
首页 > 其他好文 > 详细

Excel2Dataset

时间:2018-09-04 13:38:59      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:vat   editor   into   value   system   get   expr   pos   ram   

 /// <summary>
        /// 创建Excel Table.
        /// </summary>
        /// <param name="colCount">列数</param>
        /// <returns>DataTable</returns>
        private System.Data.DataTable CreateExcelTable(int colCount)
        {
            System.Data.DataTable returnTable = new System.Data.DataTable();
            for (int i = 0; i <= colCount; i++)
                returnTable.Columns.Add("col" + i.ToString(), typeof(string));
            return returnTable;
        }

        /// <summary>
        /// 根据Excel路径,读取数据至DataSet.
        /// </summary>
        /// <param name="excelPath">Excel Path</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSetFromExcel(string excelPath)
        {
            DataSet resultDS = new DataSet();
            Aspose.Cells.Workbook excelBook = new Aspose.Cells.Workbook();
            excelBook.Open(excelPath);


            // get the rows and insert into dataset.
            Aspose.Cells.Worksheet excelSheet = excelBook.Worksheets[0];
            if (!excelSheet.IsVisible)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏的Sheet:[{0}],请检查!", excelSheet.Name), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return resultDS;
            }

            Aspose.Cells.Cells excelValues = excelSheet.Cells;
            foreach (Row r in excelValues.Rows)
            {
                if (r.IsHidden)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏行,行号:[{0}],请检查!", r.Index + 1), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return resultDS;
                }
            }
            foreach (Column c in excelValues.Columns)
            {
                if (c.IsHidden)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏列,列号:[{0}],请检查!", (char)(c.Index + 65)), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return resultDS;
                }
            }
            int rowCount = excelValues.MaxRow;
            int colCount = excelValues.MaxColumn;

            System.Data.DataTable excelTable = CreateExcelTable(colCount);
            resultDS.Tables.Add(excelTable);
            for (int i = 0; i <= rowCount; i++)
            {
                //如果前5栏为空的话,则忽略添加新行。
                if (Convert.ToString(excelValues[i, 0].Value) == ""
                    && Convert.ToString(excelValues[i, 1].Value) == ""
                    && Convert.ToString(excelValues[i, 2].Value) == ""
                    && Convert.ToString(excelValues[i, 3].Value) == ""
                    && Convert.ToString(excelValues[i, 4].Value) == "")
                    continue;
                DataRow row = excelTable.NewRow();
                for (int j = 0; j <= colCount; j++)
                {
                    if (excelValues[i, j].Value == null)
                        row[j] = "";
                    else
                        row[j] = excelValues[i, j].Value.ToString();
                }
                excelTable.Rows.Add(row);
            }
            return resultDS;
        }

        /// <summary>
        /// 根据Excel路径,读取指定Sheet表数据至DataSet.
        /// </summary>
        /// <param name="excelPath">Excel Path</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSetFromExcel_SG3Nod(string excelPath,int x)
        {
            DataSet resultDS = new DataSet();
            Aspose.Cells.Workbook excelBook = new Aspose.Cells.Workbook();
            excelBook.Open(excelPath);


            // get the rows and insert into dataset.
            Aspose.Cells.Worksheet excelSheet = excelBook.Worksheets[x];
            if (!excelSheet.IsVisible)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏的Sheet:[{0}],请检查!", excelSheet.Name), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return resultDS;
            }

            Aspose.Cells.Cells excelValues = excelSheet.Cells;
            foreach (Row r in excelValues.Rows)
            {
                if (r.IsHidden)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏行,行号:[{0}],请检查!", r.Index + 1), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return resultDS;
                }
            }
            foreach (Column c in excelValues.Columns)
            {
                if (c.IsHidden)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏列,列号:[{0}],请检查!", (char)(c.Index + 65)), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return resultDS;
                }
            }
            int rowCount = excelValues.MaxRow;
            int colCount = excelValues.MaxColumn;

            System.Data.DataTable excelTable = CreateExcelTable(colCount);
            resultDS.Tables.Add(excelTable);
            for (int i = 0; i <= rowCount; i++)
            {
                //如果前5栏为空的话,则忽略添加新行。
                if (Convert.ToString(excelValues[i, 0].Value) == ""
                    && Convert.ToString(excelValues[i, 1].Value) == ""
                    && Convert.ToString(excelValues[i, 2].Value) == ""
                    && Convert.ToString(excelValues[i, 3].Value) == ""
                    && Convert.ToString(excelValues[i, 4].Value) == "")
                    continue;
                DataRow row = excelTable.NewRow();
                for (int j = 0; j <= colCount; j++)
                {
                    if (excelValues[i, j].Value == null)
                        row[j] = "";
                    else
                        row[j] = excelValues[i, j].Value.ToString();
                }
                excelTable.Rows.Add(row);
            }
            return resultDS;
        }

        /// <summary>
        /// 读取Excel第一个Sheet至DataTable
        /// </summary>
        /// <param name="excelPath"></param>
        /// <returns></returns>
        public DataTable GetDataTableFromExcel(string excelPath)
        {
            DataSet resultDS = new DataSet();
            Aspose.Cells.Workbook excelBook = new Aspose.Cells.Workbook(excelPath);
            //excelBook.Open(excelPath);

            // get the rows and insert into dataset.
            Aspose.Cells.Worksheet excelSheet = excelBook.Worksheets[0];
            Aspose.Cells.Cells excelValues = excelSheet.Cells;
            int rowCount = excelValues.MaxRow + 1;
            int colCount = excelValues.MaxColumn + 1;
            return excelValues.ExportDataTable(0, 0, rowCount, colCount);

        }

 

Excel2Dataset

标签:vat   editor   into   value   system   get   expr   pos   ram   

原文地址:https://www.cnblogs.com/evanmemo/p/9583373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!