100分 |
hssfworkbook.NumberOfSheets
|
找到了。
XSSFWorkbook xssfworkbook = new XSSFWorkbook(fs); int count=xssfworkbook.Count; |
|
多个工作簿获取可以用
ISheet sheet = xssfworkbook.GetSheetAt(sheetNo); |
|
/// <summary>
/// 将Excel文件中的数据读出到DataSet中(xlsx) /// </summary> /// <param name=”file”></param> /// <returns></returns> public static DataSet ExcelToDSForXLSX(string file) { DataSet ds = new DataSet(); //模板中excel有两个 第一个默认规上企业 第二个默认不是规上企业 using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { XSSFWorkbook xssfworkbook = new XSSFWorkbook(fs); for (int count = 0; count < xssfworkbook.NumberOfSheets; count++) { DataTable dt = new DataTable(); ISheet sheet = xssfworkbook.GetSheetAt(count); //表头 IRow header = sheet.GetRow(sheet.FirstRowNum); if (header == null) break; List<int> columns = new List<int>(); for (int i = 0; i < header.LastCellNum; i++) { object obj = GetValueTypeForXLSX(header.GetCell(i) as XSSFCell); if (obj == null || obj.ToString() == string.Empty) { dt.Columns.Add(new DataColumn(“Columns” + i.ToString())); } else { dt.Columns.Add(new DataColumn(obj.ToString())); } columns.Add(i); } //数据 for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) { DataRow dr = dt.NewRow(); bool hasValue = false; foreach (int j in columns) { dr[j] = GetValueTypeForXLSX(sheet.GetRow(i).GetCell(j) as XSSFCell); if (dr[j] != null && dr[j].ToString() != string.Empty) { hasValue = true; } } if (hasValue) { dt.Rows.Add(dr); } } //datatable添加ds ds.Merge(dt, false, MissingSchemaAction.Add); ds.Tables[count].TableName = count.ToString(); } } return ds; } |