首页 > 代码库 > 使用NPOI 2.1.1读取EXCEL2003/2007返回DataTable
使用NPOI 2.1.1读取EXCEL2003/2007返回DataTable
一,不借助插件读取Excel2003、2007:
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "Data Source=" + path + ";" + "Extended Properties=‘Excel 12.0; HDR=Yes; IMEX=1‘"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel = "select * from [sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds, "table1"); return ds;
二,使用NPOI 2.1.1读取EXCEL2003/2007
在使用NPOI 2.0.1读取Excel2007文档时,标准的日期格式2014/12/4 给读成了04-十二月-2014,不知道是我的人品差,还是版本bug,又不想手动去转换格式,试了下最新版本,解决了这问题。
调用方法就一句话:DataTable dt1 = ExcelNpoiHelper.ImportExcelAllToDt(file);
附ExcelNpoiHelper类。
using NPOI.HSSF.UserModel;using NPOI.SS.Formula.Eval;using NPOI.SS.UserModel;using NPOI.SS.Util;using NPOI.XSSF.UserModel;using System;using System.Collections;using System.Collections.Generic;using System.Data;using System.IO;using System.Text;using System.Text.RegularExpressions;namespace UploadValuationReport.excelhelper{ public class ExcelNpoiHelper { #region 从datatable中将数据导出到excel /// <summary> /// DataTable导出到Excel的MemoryStream /// </summary> /// <param name="dtSource">源DataTable</param> /// <param name="strHeaderText">表头文本</param> static MemoryStream ExportDt(DataTable dtSource, string strHeaderText) { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.CreateSheet() as HSSFSheet; #region 右击文件 属性信息 //{ // DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); // dsi.Company = "NPOI"; // workbook.DocumentSummaryInformation = dsi; // SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); // si.Author = "文件作者信息"; //填加xls文件作者信息 // si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息 // si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息 // si.Comments = "作者信息"; //填加xls文件作者信息 // si.Title = "标题信息"; //填加xls文件标题信息 // si.Subject = "主题信息"; //填加文件主题信息 // si.CreateDateTime = DateTime.Now; // workbook.SummaryInformation = si; //} #endregion HSSFCellStyle dateStyle = workbook.CreateCellStyle() as HSSFCellStyle; HSSFDataFormat format = workbook.CreateDataFormat() as HSSFDataFormat; dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); //取得列宽 int[] arrColWidth = new int[dtSource.Columns.Count]; foreach (DataColumn item in dtSource.Columns) { arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; } for (int i = 0; i < dtSource.Rows.Count; i++) { for (int j = 0; j < dtSource.Columns.Count; j++) { int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length; if (intTemp > arrColWidth[j]) { arrColWidth[j] = intTemp; } } } int rowIndex = 0; foreach (DataRow row in dtSource.Rows) { #region 新建表,填充表头,填充列头,样式 if (rowIndex == 65535 || rowIndex == 0) { if (rowIndex != 0) { sheet = workbook.CreateSheet() as HSSFSheet; } #region 表头及样式 { HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow; headerRow.HeightInPoints = 25; headerRow.CreateCell(0).SetCellValue(strHeaderText); HSSFCellStyle headStyle = workbook.CreateCellStyle() as HSSFCellStyle; headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; HSSFFont font = workbook.CreateFont() as HSSFFont; font.FontHeightInPoints = 20; font.Boldweight = 700; headStyle.SetFont(font); headerRow.GetCell(0).CellStyle = headStyle; sheet.AddMergedRegion(new Region(0, 0, 0, dtSource.Columns.Count - 1)); //headerRow.Dispose(); } #endregion #region 列头及样式 { HSSFRow headerRow = sheet.CreateRow(1) as HSSFRow; HSSFCellStyle headStyle = workbook.CreateCellStyle() as HSSFCellStyle; headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; HSSFFont font = workbook.CreateFont() as HSSFFont; font.FontHeightInPoints = 10; font.Boldweight = 700; headStyle.SetFont(font); foreach (DataColumn column in dtSource.Columns) { headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); headerRow.GetCell(column.Ordinal).CellStyle = headStyle; //设置列宽 sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); } //headerRow.Dispose(); } #endregion rowIndex = 2; } #endregion #region 填充内容 HSSFRow dataRow = sheet.CreateRow(rowIndex) as HSSFRow; foreach (DataColumn column in dtSource.Columns) { HSSFCell newCell = dataRow.CreateCell(column.Ordinal) as HSSFCell; string drValue =http://www.mamicode.com/ row[column].ToString(); switch (column.DataType.ToString()) { case "System.String": //字符串类型 double result; if (IsNumeric(drValue, out result)) { double.TryParse(drValue, out result); newCell.SetCellValue(result); break; } else { newCell.SetCellValue(drValue); break; } case "System.DateTime": //日期类型 DateTime dateV; DateTime.TryParse(drValue, out dateV); newCell.SetCellValue(dateV); newCell.CellStyle = dateStyle; //格式化显示 break; case "System.Boolean": //布尔型 bool boolV = false; bool.TryParse(drValue, out boolV); newCell.SetCellValue(boolV); break; case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": int intV = 0; int.TryParse(drValue, out intV); newCell.SetCellValue(intV); break; case "System.Decimal": //浮点型 case "System.Double": double doubV = 0; double.TryParse(drValue, out doubV); newCell.SetCellValue(doubV); break; case "System.DBNull": //空值处理 newCell.SetCellValue(""); break; default: newCell.SetCellValue(""); break; } } #endregion rowIndex++; } using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); ms.Flush(); ms.Position = 0; //sheet; //workbook.Dispose(); return ms; } } /// <summary> /// DataTable导出到Excel文件 /// </summary> /// <param name="dtSource">源DataTable</param> /// <param name="strHeaderText">表头文本</param> /// <param name="strFileName">保存位置</param> public static void ExportDTtoExcel(DataTable dtSource, string strHeaderText, string strFileName) { using (MemoryStream ms = ExportDt(dtSource, strHeaderText)) { using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write)) { byte[] data =http://www.mamicode.com/ ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } } } #endregion #region 从excel2003/2007中将数据导出到datatable(默认第一行为标头,实际数据从第二行读取) /// <summary> /// 自动辨别excel2003/2007 /// </summary> /// <param name="strFileName">excel文档路径</param> /// <returns></returns> public static DataTable ImportExcelAllToDt(string strFileName) { DataTable dt = new DataTable(); IWorkbook workbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { workbook = WorkbookFactory.Create(file);//使用接口,自动识别excel2003/2007格式 } ISheet sheet = workbook.GetSheetAt(0);//得到里面第一个sheet //HSSFSheet sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet; if (Path.GetExtension(strFileName).Equals(".xlsx")) { dt = ImportExcel2007InDt(sheet, 0, true); return dt; } else { dt = ImportExcel2003InDt(sheet, 0, true); return dt; } } /// <summary>读取excel /// 默认第一行为标头,实际数据从第二行读取 /// </summary> /// <param name="strFileName">excel文档路径</param> /// <returns></returns> public static DataTable ImportExcel2003ToDt(string strFileName) { DataTable dt = new DataTable(); IWorkbook hssfworkbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { hssfworkbook = new HSSFWorkbook(file); } HSSFSheet sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet; dt = ImportExcel2003InDt(sheet, 0, true); return dt; } /// <summary>读取excel /// 默认第一行为标头,实际数据从第二行读取 /// </summary> /// <param name="strFileName">excel文档路径</param> /// <returns></returns> public static DataTable ImportExcel2007ToDt(string strFileName) { DataTable dt = new DataTable(); IWorkbook hssfworkbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { hssfworkbook = new XSSFWorkbook(file); } ISheet sheet = hssfworkbook.GetSheetAt(0); dt = ImportExcel2007InDt(sheet, 0, true); return dt; } /// <summary> /// 读取excel /// </summary> /// <param name="strFileName">excel文件路径</param> /// <param name="sheetName">需要导出的sheet</param> /// <param name="headerRowIndex">列头所在行号,-1表示没有列头</param> /// <returns></returns> public static DataTable ImportExcel2003ToDt(string strFileName, string sheetName, int headerRowIndex) { IWorkbook workbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(file); } HSSFSheet sheet = workbook.GetSheet(sheetName) as HSSFSheet; DataTable table = new DataTable(); table = ImportExcel2003InDt(sheet, headerRowIndex, true); //ExcelFileStream.Close(); workbook = null; sheet = null; return table; } /// <summary> /// 读取excel /// </summary> /// <param name="strFileName">excel文件路径</param> /// <param name="sheetName">需要导出的sheet</param> /// <param name="headerRowIndex">列头所在行号,-1表示没有列头</param> /// <returns></returns> public static DataTable ImportExcel2007ToDt(string strFileName, string sheetName, int headerRowIndex) { IWorkbook workbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { workbook = new XSSFWorkbook(file); } HSSFSheet sheet = workbook.GetSheet(sheetName) as HSSFSheet; DataTable table = new DataTable(); table = ImportExcel2007InDt(sheet, headerRowIndex, true); //ExcelFileStream.Close(); workbook = null; sheet = null; return table; } /// <summary> /// 读取excel /// </summary> /// <param name="strFileName">excel文件路径</param> /// <param name="sheetIndex">需要导出的sheet序号</param> /// <param name="headerRowIndex">列头所在行号,-1表示没有列头</param> /// <returns></returns> public static DataTable ImportExcel2003ToDt(string strFileName, int sheetIndex, int headerRowIndex) { HSSFWorkbook workbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(file); } HSSFSheet sheet = workbook.GetSheetAt(sheetIndex) as HSSFSheet; DataTable table = new DataTable(); table = ImportExcel2003InDt(sheet, headerRowIndex, true); //ExcelFileStream.Close(); workbook = null; sheet = null; return table; } /// <summary> /// 读取excel /// </summary> /// <param name="strFileName">excel文件路径</param> /// <param name="sheetIndex">需要导出的sheet序号</param> /// <param name="headerRowIndex">列头所在行号,-1表示没有列头</param> /// <returns></returns> public static DataTable ImportExcel2007ToDt(string strFileName, int sheetIndex, int headerRowIndex) { IWorkbook workbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { workbook = new XSSFWorkbook(file); } HSSFSheet sheet = workbook.GetSheetAt(sheetIndex) as HSSFSheet; DataTable table = new DataTable(); table = ImportExcel2007InDt(sheet, headerRowIndex, true); //ExcelFileStream.Close(); workbook = null; sheet = null; return table; } /// <summary> /// 读取excel /// </summary> /// <param name="strFileName">excel文件路径</param> /// <param name="sheetName">需要导出的sheet</param> /// <param name="headerRowIndex">列头所在行号,-1表示没有列头</param> /// <param name="needHeader">是否需要头</param> /// <returns></returns> public static DataTable ImportExcel2003ToDt(string strFileName, string sheetName, int headerRowIndex, bool needHeader) { IWorkbook workbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(file); } HSSFSheet sheet = workbook.GetSheet(sheetName) as HSSFSheet; DataTable table = new DataTable(); table = ImportExcel2003InDt(sheet, headerRowIndex, needHeader); //ExcelFileStream.Close(); workbook = null; sheet = null; return table; } /// <summary> /// 读取excel /// </summary> /// <param name="strFileName">excel文件路径</param> /// <param name="sheetIndex">需要导出的sheet序号</param> /// <param name="headerRowIndex">列头所在行号,-1表示没有列头</param> /// <param name="needHeader">是否需要头</param> /// <returns></returns> public static DataTable ImportExcel2003ToDt(string strFileName, int sheetIndex, int headerRowIndex, bool needHeader) { HSSFWorkbook workbook; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(file); } HSSFSheet sheet = workbook.GetSheetAt(sheetIndex) as HSSFSheet; DataTable table = new DataTable(); table = ImportExcel2003InDt(sheet, headerRowIndex, needHeader); //ExcelFileStream.Close(); workbook = null; sheet = null; return table; } static DataTable ImportExcel2003InDt(ISheet sheet, int headerRowIndex, bool needHeader) { DataTable table = new DataTable(); HSSFRow headerRow; int cellCount; try { if (headerRowIndex < 0 || !needHeader) { headerRow = sheet.GetRow(0) as HSSFRow; cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i <= cellCount; i++) { DataColumn column = new DataColumn(Convert.ToString(i)); table.Columns.Add(column); } } else { headerRow = sheet.GetRow(headerRowIndex) as HSSFRow; cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i <= cellCount; i++) { if (headerRow.GetCell(i) == null) { if (table.Columns.IndexOf(Convert.ToString(i)) > 0) { DataColumn column = new DataColumn(Convert.ToString("重复列名" + i)); table.Columns.Add(column); } else { DataColumn column = new DataColumn(Convert.ToString(i)); table.Columns.Add(column); } } else if (table.Columns.IndexOf(headerRow.GetCell(i).ToString()) > 0) { DataColumn column = new DataColumn(Convert.ToString("重复列名" + i)); table.Columns.Add(column); } else { DataColumn column = new DataColumn(headerRow.GetCell(i).ToString()); table.Columns.Add(column); } } } int rowCount = sheet.LastRowNum; for (int i = (headerRowIndex + 1); i <= sheet.LastRowNum; i++) { try { HSSFRow row; if (sheet.GetRow(i) == null) { row = sheet.CreateRow(i) as HSSFRow; } else { row = sheet.GetRow(i) as HSSFRow; } DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j <= cellCount; j++) { try { if (row.GetCell(j) != null) { switch (row.GetCell(j).CellType) { case CellType.String: string str = row.GetCell(j).StringCellValue; if (str != null && str.Length > 0) { dataRow[j] = str.ToString(); } else { dataRow[j] = null; } break; case CellType.Numeric: if (DateUtil.IsCellDateFormatted(row.GetCell(j))) { dataRow[j] = DateTime.FromOADate(row.GetCell(j).NumericCellValue); } else { dataRow[j] = Convert.ToDouble(row.GetCell(j).NumericCellValue); } break; case CellType.Boolean: dataRow[j] = Convert.ToString(row.GetCell(j).BooleanCellValue); break; case CellType.Error: dataRow[j] = ErrorEval.GetText(row.GetCell(j).ErrorCellValue); break; case CellType.Formula: switch (row.GetCell(j).CachedFormulaResultType) { case CellType.String: string strFORMULA = row.GetCell(j).StringCellValue; if (strFORMULA != null && strFORMULA.Length > 0) { dataRow[j] = strFORMULA.ToString(); } else { dataRow[j] = null; } break; case CellType.Numeric: dataRow[j] = Convert.ToString(row.GetCell(j).NumericCellValue); break; case CellType.Boolean: dataRow[j] = Convert.ToString(row.GetCell(j).BooleanCellValue); break; case CellType.Error: dataRow[j] = ErrorEval.GetText(row.GetCell(j).ErrorCellValue); break; default: dataRow[j] = ""; break; } break; default: dataRow[j] = ""; break; } } } catch (Exception exception) { //wl.WriteLogs(exception.ToString()); } } table.Rows.Add(dataRow); } catch (Exception exception) { //wl.WriteLogs(exception.ToString()); } } } catch (Exception exception) { //wl.WriteLogs(exception.ToString()); } return table; } /// <summary> /// 将制定sheet中的数据导出到datatable中 /// </summary> /// <param name="sheet">需要导出的sheet</param> /// <param name="headerRowIndex">列头所在行号,-1表示没有列头</param> /// <param name="needHeader">是否需要列头</param> /// <returns></returns> static DataTable ImportExcel2007InDt(ISheet sheet, int headerRowIndex, bool needHeader) { DataTable table = new DataTable(); NPOI.XSSF.UserModel.XSSFRow headerRow; int cellCount; try { if (headerRowIndex < 0 || !needHeader) { headerRow = sheet.GetRow(0) as NPOI.XSSF.UserModel.XSSFRow; cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i <= cellCount; i++) { DataColumn column = new DataColumn(Convert.ToString(i)); table.Columns.Add(column); } } else { headerRow = sheet.GetRow(headerRowIndex) as NPOI.XSSF.UserModel.XSSFRow; cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i <= cellCount; i++) { if (headerRow.GetCell(i) == null) { if (table.Columns.IndexOf(Convert.ToString(i)) > 0) { DataColumn column = new DataColumn(Convert.ToString("重复列名" + i)); table.Columns.Add(column); } else { DataColumn column = new DataColumn(Convert.ToString(i)); table.Columns.Add(column); } } else if (table.Columns.IndexOf(headerRow.GetCell(i).ToString()) > 0) { DataColumn column = new DataColumn(Convert.ToString("重复列名" + i)); table.Columns.Add(column); } else { DataColumn column = new DataColumn(headerRow.GetCell(i).ToString()); table.Columns.Add(column); } } } int rowCount = sheet.LastRowNum; for (int i = (headerRowIndex + 1); i <= sheet.LastRowNum; i++) { try { NPOI.XSSF.UserModel.XSSFRow row; if (sheet.GetRow(i) == null) { row = sheet.CreateRow(i) as NPOI.XSSF.UserModel.XSSFRow; } else { row = sheet.GetRow(i) as NPOI.XSSF.UserModel.XSSFRow; } DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j <= cellCount; j++) { try { if (row.GetCell(j) != null) { switch (row.GetCell(j).CellType) { case CellType.String: string str = row.GetCell(j).StringCellValue; if (str != null && str.Length > 0) { dataRow[j] = str.ToString(); } else { dataRow[j] = null; } break; case CellType.Numeric: if (DateUtil.IsCellDateFormatted(row.GetCell(j))) { dataRow[j] = DateTime.FromOADate(row.GetCell(j).NumericCellValue); } else { dataRow[j] = Convert.ToDouble(row.GetCell(j).NumericCellValue); } break; case CellType.Boolean: dataRow[j] = Convert.ToString(row.GetCell(j).BooleanCellValue); break; case CellType.Error: dataRow[j] = ErrorEval.GetText(row.GetCell(j).ErrorCellValue); break; case CellType.Formula: switch (row.GetCell(j).CachedFormulaResultType) { case CellType.String: string strFormula = row.GetCell(j).StringCellValue; if (!string.IsNullOrEmpty(strFormula)) { dataRow[j] = strFormula; } else { dataRow[j] = null; } break; case CellType.Numeric: dataRow[j] = Convert.ToString(row.GetCell(j).NumericCellValue); break; case CellType.Boolean: dataRow[j] = Convert.ToString(row.GetCell(j).BooleanCellValue); break; case CellType.Error: dataRow[j] = ErrorEval.GetText(row.GetCell(j).ErrorCellValue); break; default: dataRow[j] = ""; break; } break; default: dataRow[j] = ""; break; } } } catch (Exception exception) { //wl.WriteLogs(exception.ToString()); } } table.Rows.Add(dataRow); } catch (Exception exception) { //wl.WriteLogs(exception.ToString()); } } } catch (Exception exception) { //wl.WriteLogs(exception.ToString()); } return table; } #endregion #region 更新excel中的数据 /// <summary> /// 更新Excel表格 /// </summary> /// <param name="outputFile">需更新的excel表格路径</param> /// <param name="sheetname">sheet名</param> /// <param name="updateData">需更新的数据</param> /// <param name="coluid">需更新的列号</param> /// <param name="rowid">需更新的开始行号</param> public static void UpdateExcel(string outputFile, string sheetname, string[] updateData, int coluid, int rowid) { FileStream readfile = new FileStream(outputFile, FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(readfile); ISheet sheet1 = hssfworkbook.GetSheet(sheetname); for (int i = 0; i < updateData.Length; i++) { try { if (sheet1.GetRow(i + rowid) == null) { sheet1.CreateRow(i + rowid); } if (sheet1.GetRow(i + rowid).GetCell(coluid) == null) { sheet1.GetRow(i + rowid).CreateCell(coluid); } sheet1.GetRow(i + rowid).GetCell(coluid).SetCellValue(updateData[i]); } catch (Exception ex) { // wl.WriteLogs(ex.ToString()); throw; } } try { readfile.Close(); FileStream writefile = new FileStream(outputFile, FileMode.Create, FileAccess.Write); hssfworkbook.Write(writefile); writefile.Close(); } catch (Exception ex) { // wl.WriteLogs(ex.ToString()); } } /// <summary> /// 更新Excel表格 /// </summary> /// <param name="outputFile">需更新的excel表格路径</param> /// <param name="sheetname">sheet名</param> /// <param name="updateData">需更新的数据</param> /// <param name="coluids">需更新的列号</param> /// <param name="rowid">需更新的开始行号</param> public static void UpdateExcel(string outputFile, string sheetname, string[][] updateData, int[] coluids, int rowid) { FileStream readfile = new FileStream(outputFile, FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(readfile); readfile.Close(); ISheet sheet1 = hssfworkbook.GetSheet(sheetname); for (int j = 0; j < coluids.Length; j++) { for (int i = 0; i < updateData[j].Length; i++) { try { if (sheet1.GetRow(i + rowid) == null) { sheet1.CreateRow(i + rowid); } if (sheet1.GetRow(i + rowid).GetCell(coluids[j]) == null) { sheet1.GetRow(i + rowid).CreateCell(coluids[j]); } sheet1.GetRow(i + rowid).GetCell(coluids[j]).SetCellValue(updateData[j][i]); } catch (Exception ex) { // wl.WriteLogs(ex.ToString()); } } } try { FileStream writefile = new FileStream(outputFile, FileMode.Create); hssfworkbook.Write(writefile); writefile.Close(); } catch (Exception ex) { //wl.WriteLogs(ex.ToString()); } } /// <summary> /// 更新Excel表格 /// </summary> /// <param name="outputFile">需更新的excel表格路径</param> /// <param name="sheetname">sheet名</param> /// <param name="updateData">需更新的数据</param> /// <param name="coluid">需更新的列号</param> /// <param name="rowid">需更新的开始行号</param> public static void UpdateExcel(string outputFile, string sheetname, double[] updateData, int coluid, int rowid) { FileStream readfile = new FileStream(outputFile, FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(readfile); ISheet sheet1 = hssfworkbook.GetSheet(sheetname); for (int i = 0; i < updateData.Length; i++) { try { if (sheet1.GetRow(i + rowid) == null) { sheet1.CreateRow(i + rowid); } if (sheet1.GetRow(i + rowid).GetCell(coluid) == null) { sheet1.GetRow(i + rowid).CreateCell(coluid); } sheet1.GetRow(i + rowid).GetCell(coluid).SetCellValue(updateData[i]); } catch (Exception ex) { //wl.WriteLogs(ex.ToString()); throw; } } try { readfile.Close(); FileStream writefile = new FileStream(outputFile, FileMode.Create, FileAccess.Write); hssfworkbook.Write(writefile); writefile.Close(); } catch (Exception ex) { //wl.WriteLogs(ex.ToString()); } } /// <summary> /// 更新Excel表格 /// </summary> /// <param name="outputFile">需更新的excel表格路径</param> /// <param name="sheetname">sheet名</param> /// <param name="updateData">需更新的数据</param> /// <param name="coluids">需更新的列号</param> /// <param name="rowid">需更新的开始行号</param> public static void UpdateExcel(string outputFile, string sheetname, double[][] updateData, int[] coluids, int rowid) { FileStream readfile = new FileStream(outputFile, FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(readfile); readfile.Close(); ISheet sheet1 = hssfworkbook.GetSheet(sheetname); for (int j = 0; j < coluids.Length; j++) { for (int i = 0; i < updateData[j].Length; i++) { try { if (sheet1.GetRow(i + rowid) == null) { sheet1.CreateRow(i + rowid); } if (sheet1.GetRow(i + rowid).GetCell(coluids[j]) == null) { sheet1.GetRow(i + rowid).CreateCell(coluids[j]); } sheet1.GetRow(i + rowid).GetCell(coluids[j]).SetCellValue(updateData[j][i]); } catch (Exception ex) { //wl.WriteLogs(ex.ToString()); } } } try { FileStream writefile = new FileStream(outputFile, FileMode.Create); hssfworkbook.Write(writefile); writefile.Close(); } catch (Exception ex) { //wl.WriteLogs(ex.ToString()); } } #endregion #region 读取Excel文件,获取有多少个Sheet数 /// <summary> /// 读取Excel文件,获取有多少个Sheet数 /// </summary> /// <param name="filePath">文件地址</param> /// <returns></returns> public static int GetSheetNumber(string filePath) { int number = 0; try { FileStream readfile = new FileStream(filePath, FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(readfile); number = hssfworkbook.NumberOfSheets; } catch (Exception exception) { //wl.WriteLogs(exception.ToString()); } return number; } #endregion #region 获取Excel文件中,Sheet表单的名字列表 /// <summary> /// 获取Excel文件中,Sheet表单的名字列表 /// </summary> /// <param name="filePath">文件地址</param> /// <returns></returns> public static ArrayList GetSheetName(string filePath) { ArrayList arrayList = new ArrayList(); try { FileStream readfile = new FileStream(filePath, FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(readfile); for (int i = 0; i < hssfworkbook.NumberOfSheets; i++) { arrayList.Add(hssfworkbook.GetSheetName(i)); } } catch (Exception exception) { //wl.WriteLogs(exception.ToString()); } return arrayList; } #endregion #region 是否是数字 /// <summary> /// 是否是数字 /// </summary> /// <param name="message"></param> /// <param name="result"></param> /// <returns></returns> public static bool IsNumeric(String message, out double result) { Regex rex = new Regex(@"^[-]?\d+[.]?\d*$"); result = -1; if (rex.IsMatch(message)) { result = double.Parse(message); return true; } else return false; } #endregion #region Excel2007 的导入 导出 /// <summary> /// 将Excel文件中的数据读出到DataTable中(xlsx) /// </summary> /// <param name="file"></param> /// <returns></returns> public static DataTable ExcelToTableForXLSX(string file) { DataTable dt = new DataTable(); using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { XSSFWorkbook xssfworkbook = new XSSFWorkbook(fs); ISheet sheet = xssfworkbook.GetSheetAt(0); //表头 IRow header = sheet.GetRow(sheet.FirstRowNum); 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())); //continue; } 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 = http://www.mamicode.com/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); } } } return dt; } /// <summary> /// 将DataTable数据导出到Excel文件中(xlsx) /// </summary> /// <param name="dt"></param> /// <param name="file"></param> public static void TableToExcelForXLSX(DataTable dt, string file) { XSSFWorkbook xssfworkbook = new XSSFWorkbook(); ISheet sheet = xssfworkbook.CreateSheet("Test"); //表头 IRow row = sheet.CreateRow(0); for (int i = 0; i < dt.Columns.Count; i++) { ICell cell = row.CreateCell(i); cell.SetCellValue(dt.Columns[i].ColumnName); } //数据 for (int i = 0; i < dt.Rows.Count; i++) { IRow row1 = sheet.CreateRow(i + 1); for (int j = 0; j < dt.Columns.Count; j++) { ICell cell = row1.CreateCell(j); cell.SetCellValue(dt.Rows[i][j].ToString()); } } //转为字节数组 MemoryStream stream = new MemoryStream(); xssfworkbook.Write(stream); var buf = stream.ToArray(); //保存为Excel文件 using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write)) { fs.Write(buf, 0, buf.Length); fs.Flush(); } } /// <summary> /// 获取单元格类型(xlsx) /// </summary> /// <param name="cell"></param> /// <returns></returns> private static object GetValueTypeForXLSX(XSSFCell 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: return cell.NumericCellValue; case CellType.String: //STRING: return cell.StringCellValue; case CellType.Error: //ERROR: return cell.ErrorCellValue; case CellType.Formula: //FORMULA: default: return "=" + cell.CellFormula; } } #endregion }}
使用NPOI 2.1.1读取EXCEL2003/2007返回DataTable
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。