1 Workbook workBook = new Workbook(this.fuFile.FileContent); 2 Aspose.Cells.Worksheet sheet = workBook.Worksheets[0]; 3 Cells cells = sheet.Cells; 4 5 //DataTable dt = cells.ExportDataTable(0, 0, cells.MaxRow + 1, cells.MaxDataColumn + 1, true); 6 DataTable dt = new DataTable(); 7 DataColumn newcolumn = null; 8 for (int i = 0; i < cells.MaxColumn + 1; i++) 9 { 10 newcolumn = new DataColumn(cells[0, i].StringValue); 11 dt.Columns.Add(newcolumn); 12 } 13 14 DataRow newRow = null; 15 for (int j = 0; j < cells.MaxRow; j++) 16 { 17 newRow = dt.NewRow(); 18 for (int k = 0; k < cells.MaxColumn + 1; k++) 19 { 20 newRow[k] = cells[j + 1, k].StringValue; 21 } 22 dt.Rows.Add(newRow); 23 } 24 25 DataView dv = new DataView(dt); string[] filterColumns = { name, ibn, auth, pub, time, path }; dt = dv.ToTable(true, filterColumns);
以上代码中,首先要引用Aspose.cells 命名空间,至于Aspose.cells动态库,网上皆可下载
fuFile指asp.net页面中的上传控件。被注释掉的第5行就相当于6-23行,为了将从excel文件中读到的内容转化为DataTable表。25行将datatable 转化为dataview,这样是为了下面的,按照指定的列对所有数据去掉重复记录,最后一句代码在去重的同时,把dataview又转化为了datatable
时间: 2024-10-29 19:06:22