/// 读取excel数据 private void button10_Click(object sender, EventArgs e) { string path = "C:/Users/Administrator/Desktop/打印/excel文件/test.xlsx"; System.Data.DataTable excelDt = ReadFromExcel(path); //把Excel读取到DataTable里面 然后再把DataTable存入数据库 for (int j = 0; j < excelDt.Columns.Count; j++) { string xuhao = excelDt.Rows[j][0].ToString(); string tuhao = excelDt.Rows[j][1].ToString(); string tuname = excelDt.Rows[j][2].ToString(); string bumen = excelDt.Rows[j][3].ToString(); string shoujianname = excelDt.Rows[j][4].ToString(); } }
/// 从Excel读取数据,只支持单表
///FilePath文件路径
public static System.Data.DataTable ReadFromExcel(string FilePath) { IWorkbook wk = null; string extension = System.IO.Path.GetExtension(FilePath); //获取扩展名 try { using (FileStream fs = File.OpenRead(FilePath)) { if (extension.Equals(".xls")) //2003 { wk = new HSSFWorkbook(fs); } else //2007以上 { wk = new XSSFWorkbook(fs); } } //读取当前表数据 ISheet sheet = wk.GetSheetAt(0); //构建DataTable IRow row = sheet.GetRow(0); System.Data.DataTable result = BuildDataTable(row); if (result != null) { if (sheet.LastRowNum >= 1) { for (int i = 1; i < sheet.LastRowNum + 1; i++) { IRow temp_row = sheet.GetRow(i); if (temp_row == null) { continue; }//2019-01-14 修复 行为空时会出错 List<object> itemArray = new List<object>(); for (int j = 0; j < result.Columns.Count; j++)//解决Excel超出DataTable列问题 lqwvje20181027 { //itemArray.Add(temp_row.GetCell(j) == null ? string.Empty : temp_row.GetCell(j).ToString()); itemArray.Add(GetValueType(temp_row.GetCell(j)));//解决 导入Excel 时间格式问题 lqwvje 20180904 } result.Rows.Add(itemArray.ToArray()); } } } return result; } catch (Exception ex) { return null; } }
private static System.Data.DataTable BuildDataTable(IRow Row) { System.Data.DataTable result = null; if (Row.Cells.Count > 0) { result = new System.Data.DataTable(); for (int i = 0; i < Row.LastCellNum; i++) { if (Row.GetCell(i) != null) { result.Columns.Add(Row.GetCell(i).ToString()); } } } return result; }
///
/// 获取单元格类型
///
///
///
private static object GetValueType(ICell cell) { if (cell == null) return null; switch (cell.CellType) { case CellType.Blank: //BLANK: return null; case CellType.Boolean: //BOOLEAN: return cell.BooleanCellValue; case CellType.Numeric: //NUMERIC: if (DateUtil.IsCellDateFormatted(cell)) { return cell.DateCellValue; } return cell.NumericCellValue; case CellType.String: //STRING: return cell.StringCellValue; case CellType.Error: //ERROR: return cell.ErrorCellValue; case CellType.Formula: //FORMULA: cell.SetCellType(CellType.String); return cell.StringCellValue; default: return "=" + cell.CellFormula; } }