由于项目的需要,将一个有数十万数据的Excel文件导入到数据库,直接导入的话太慢了,然后就考虑写一个分隔程序,多个文件同时导入。
下面就看看是怎么弄的:
1.将你的Excel文件读入到一个DataTable中,引用了Aspose.Cells.dll,这里就不提供下载了,需要的话请去找度娘^_^:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//读取获得数据 private static DataTable GetExcelTablenew(string uploadPath) { DataSet ds = new DataSet(); Workbook workbook = new Workbook(); workbook.Open(uploadPath); Names NameList = workbook.Worksheets.Names; Worksheet wsts = workbook.Worksheets[0]; if (wsts.Cells.Count > 0) { int MaxR = wsts.Cells.MaxRow; int MaxC = wsts.Cells.MaxColumn; if (MaxR > 0 && MaxC > 0) { DataTable dt = wsts.Cells.ExportDataTableAsString(0, 0, MaxR + 1, MaxC + 1, true); ds.Tables.Add(dt); } } if (ds.Tables.Count < 1) { return new DataTable(); } return ds.Tables[0]; }
2.我们获得了一个Table,下面就开始分隔了哦(代码为直接拿出来的 一些地方就没有修改见谅):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary> 2 /// 分解数据表 3 /// </summary> 4 /// <param name="originalTab">需要分解的表</param> 5 /// <param name="rowsNum">每个表包含的数据量</param> 6 /// <returns></returns> 7 public static string[] SplitDataTable(DataTable originalTab, int rowsNum,string filePath) 8 { 9 Console.WriteLine("分隔真的要开始咯"); 10 string[] ListName=new string[10]; 11 //获取所需创建的表数量 12 int tableNum = originalTab.Rows.Count / rowsNum; 13 14 //获取数据余数 15 int remainder = originalTab.Rows.Count % rowsNum; 16 17 DataSet ds = new DataSet(); 18 19 //如果只需要创建1个表,直接将原始表存入DataSet 20 if (tableNum == 0) 21 { 22 ds.Tables.Add(originalTab); 23 } 24 else 25 { 26 DataTable[] tableSlice = new DataTable[tableNum]; 27 28 //Save orginal columns into new table. 29 for (int c = 0; c < tableNum; c++) 30 { 31 tableSlice[c] = new DataTable(); 32 foreach (DataColumn dc in originalTab.Columns) 33 { 34 tableSlice[c].Columns.Add(dc.ColumnName, dc.DataType); 35 } 36 } 37 //Import Rows 38 for (int i = 0; i < tableNum; i++) 39 { 40 // if the current table is not the last one 41 if (i != tableNum - 1) 42 { 43 for (int j = i * rowsNum; j < ((i + 1) * rowsNum); j++) 44 { 45 tableSlice[i].ImportRow(originalTab.Rows[j]); 46 } 47 } 48 else 49 { 50 for (int k = i * rowsNum; k < ((i + 1) * rowsNum + remainder); k++) 51 { 52 tableSlice[i].ImportRow(originalTab.Rows[k]); 53 } 54 } 55 } 56 57 for (int i = 0; i < tableSlice.Length; i++) { 58 Console.WriteLine("进行写入文件咯"); 59 ListName[i] = TableTntoCsv(tableSlice[i], filePath, i); 60 } 61 } 62 63 return ListName; 64 65 }
3.来个图看看分隔的文件:
4.好了,就这样,下篇再见!