接上篇_C#批量讀取Execl文件_ C#_NPOI_批量讀取Execl數據導入導出數據》

接上篇_C#批量讀取Execl文件_ C#_NPOI_批量讀取Execl數據導入導出數據》

上篇地址:C#_Execl導入導出

之前寫了一個單個文件的Execl導入導出,能滿足一般Execl數據操作,後面有個需求是這樣的。有三十三萬的數據量,甚至更多,並且分別存在不同的Execl文件中。每個文件500條數據的樣子。想要集中批量處理。如果還是之前那個方式,那多不實際。

話不多說,上代碼。

程序截圖:

上圖爲左邊爲上篇寫的Execl單文件處理,右邊是新增的多Execl文件處理。

詳細可以看看上篇_C#_Execl導入導出

在上篇的基礎上,稍作修改。代碼如下

 public static DataTable MuchExcelToDataTable(List<string> filePath, bool isColumnName)
        {
            DataTable dataTable = null;//零時存取一個Execl文件的數據
            DataTable lastdata = null;//最後返回的Datatable 
            FileStream fs = null;
            DataColumn column = null;
            DataRow dataRow = null;
            IWorkbook workbook = null;
            ISheet sheet = null;
            IRow row = null;
            ICell cell = null;
            int startRow = 0;
            bool isFisrtTime = true;
            try
            {
                if (filePath.Count > 0)
                {
                    foreach (var item in filePath)
                    {
                        using (fs = File.OpenRead(item))
                        {
                            // 2007版本
                            if (item.IndexOf(".xlsx") > 0)
                                workbook = new XSSFWorkbook(fs);
                            // 2003版本
                            else if (item.IndexOf(".xls") > 0)
                                workbook = new HSSFWorkbook(fs);
                            if (workbook != null)
                            {
                                sheet = workbook.GetSheetAt(0);//讀取第一個sheet,當然也可以循環讀取每個sheet
                                //需要新建一個DataTable來將每一次讀取的都合併存起來,然後這個dataTable只存每一個的
                                if (dataTable != null)
                                {
                                    if (isFisrtTime)
                                    {
                                        lastdata = dataTable.Copy();
                                        isFisrtTime = false;
                                    }
                                    for (int i = 0; i < dataTable.Rows.Count; i++)
                                    {
                                        lastdata.Rows.Add(dataTable.Rows[i].ItemArray);
                                        int count = lastdata.Rows.Count;
                                    }
                                }

                                dataTable = new DataTable();//每次在這裏重新實例化Datatable會導致數據被覆蓋抹掉
                                if (sheet != null)
                                {
                                    int rowCount = sheet.LastRowNum;//總行數
                                    if (rowCount > 0)
                                    {
                                        IRow firstRow = sheet.GetRow(0);//第一行
                                        int cellCount = firstRow.LastCellNum;//列數

                                        //構建datatable的列
                                        if (isColumnName)
                                        {
                                            startRow = 1;//如果第一行是列名,則從第二行開始讀取
                                            for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                            {
                                                cell = firstRow.GetCell(i);
                                                if (cell != null)
                                                {
                                                    if (cell.StringCellValue != null)
                                                    {
                                                        column = new DataColumn(cell.StringCellValue);
                                                        dataTable.Columns.Add(column);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                            {
                                                column = new DataColumn("column" + (i + 1));
                                                dataTable.Columns.Add(column);
                                            }
                                        }

                                        //填充行
                                        for (int i = startRow; i <= rowCount; ++i)
                                        {
                                            row = sheet.GetRow(i);
                                            if (row == null) continue;

                                            dataRow = dataTable.NewRow();
                                            for (int j = row.FirstCellNum; j < cellCount; ++j)
                                            {
                                                cell = row.GetCell(j);
                                                if (cell == null)
                                                {
                                                    dataRow[j] = "";
                                                }
                                                else
                                                {
                                                    //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
                                                    switch (cell.CellType)
                                                    {
                                                        case CellType.Blank:
                                                            dataRow[j] = "";
                                                            break;
                                                        case CellType.Numeric:
                                                            short format = cell.CellStyle.DataFormat;
                                                            //對時間格式(2015.12.5、2015/12/5、2015-12-5等)的處理
                                                            if (format == 14 || format == 31 || format == 57 || format == 58)
                                                                dataRow[j] = cell.DateCellValue;
                                                            else
                                                                dataRow[j] = cell.NumericCellValue;
                                                            break;
                                                        case CellType.String:
                                                            dataRow[j] = cell.StringCellValue;
                                                            break;
                                                    }
                                                }
                                            }
                                            dataTable.Rows.Add(dataRow);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return lastdata;
            }
            catch (Exception)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                return null;
            }
        }

不同之處,請仔細對比吧!

下面貼上,按鈕事件,添加一個Listbox控件

 /// <summary>
        /// 批量讀取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_muchread_Click(object sender, EventArgs e)
        {
            if (listpath.Items.Count > 0)
            {
                List<string> lp = new List<string>();
                for (int i = 0; i <= listpath.Items.Count - 1; i++)
                {
                    lp.Add(listpath.Items[i].ToString());
                }
                stopwatch.Start();
                DataTable dt = MuchExeclToRead.MuchExcelToDataTable(lp, true) as DataTable;
                dgv_ExeclData.DataSource = dt;
                stopwatch.Stop();
                label6.Text = stopwatch.ElapsedMilliseconds + "ms";
                label4.Text = dt.Rows.Count.ToString();
            }
            else
            {
                MessageBox.Show("未選擇文件夾,請選擇後重試");
                return;
            }
        }

操作,調用都有了。希望能對你有幫助。歡迎指正,謝謝!

下面爲我自己的親測;

一次讀取661個文件,數據量330000條;

時間在三四秒的樣子。效率也算湊合;還可以優化;

好了,整篇內容就這些了;拜拜!

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章