/// <summary> /// 获取单元格类型 /// </summary> /// <param name="cell">目标单元格</param> /// <returns></returns> private static object GetValueType(ICell cell) { if (cell == null) return null; switch (cell.CellType) { case CellType.Blank: return null; case CellType.Boolean: return cell.BooleanCellValue; case CellType.Numeric: return cell.NumericCellValue; case CellType.String: return cell.StringCellValue; case CellType.Error: return cell.ErrorCellValue; case CellType.Formula: default: return "=" + cell.CellFormula; } }
/// <summary> /// Excel导入成DataTble /// </summary> /// <param name="file">导入路径(包含文件名与扩展名)</param> /// <returns></returns> public static DataTable ExcelToTable(string file ,int pistion=0) { DataTable dt = new DataTable(); IWorkbook workbook; string fileExt = Path.GetExtension(file).ToLower(); using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(fs); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(fs); } else { workbook = null; } if (workbook == null) { return null; } ISheet sheet = workbook.GetSheetAt(pistion); //表头 IRow header = sheet.GetRow(sheet.FirstRowNum); List<int> columns = new List<int>(); for (int i = 0; i < header.LastCellNum; i++) { object obj = GetValueType(header.GetCell(i)); 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] = GetValueType(sheet.GetRow(i).GetCell(j)); if (dr[j] != null && dr[j].ToString() != string.Empty) { hasValue = true; } } if (hasValue) { dt.Rows.Add(dr); } } } return dt; }
private static IWorkbook workbook; public static int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten, string fileName) { int i = 0; int j = 0; int count = 0; ISheet sheet = null; if (File.Exists(fileName)) File.Delete(fileName); var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); if (fileName.IndexOf(".xlsx") > 0) workbook = new XSSFWorkbook(); else if (fileName.IndexOf(".xls") > 0) workbook = new HSSFWorkbook(); try { if (workbook != null) { sheet = workbook.CreateSheet(sheetName); } else { return -1; } if (isColumnWritten == true) { IRow row = sheet.CreateRow(0); for (j = 0; j < data.Columns.Count; ++j) { row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName); } count = 1; } else { count = 0; } for (i = 0; i < data.Rows.Count; ++i) { IRow row = sheet.CreateRow(count); for (j = 0; j < data.Columns.Count; ++j) { row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString()); } ++count; } workbook.Write(fs); return count; } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); return -1; } }
string worksheetname = string.Empty; HSSFWorkbook workbook1 = null; using (FileStream stream = File.OpenRead(fazhuSheetFilePath)) { workbook1 = new HSSFWorkbook(stream); } int position = 0; for (int i = 0; i < workbook1.NumberOfSheets; i++) { if (workbook1.GetSheetName(i).Contains("仕様条件編"))//判断Sheet名是否为所需要的 { worksheetname = workbook1.GetSheetName(i);//获取worksheet Name position = i; break; } } var TaggingTable = ExcelToTable(fazhuSheetFilePath, position);