NPOI,顧名思義,就是POI的.NET版本。那POI又是什麼呢?POI是一套用Java寫成的庫,可以幫助開發者在沒有安裝微軟Office的狀況下讀寫Office的文件。web
支持的文件格式包括xls, doc, ppt等。app
官方網站:http://npoi.codeplex.com/xss
nuget直接獲取使用ide
1、NPOI生成Excel
//建立工做薄 var workbook = new HSSFWorkbook(); //建立表 var table = workbook.CreateSheet("joye.net"); //模擬20行20列數據 for (var i = 0; i < 20; i++) { var row = table.CreateRow(i); for (int j = 0; j < 20; j++) { var cell = row.CreateCell(j); cell.SetCellValue(j); } } //打開xls文件,如沒有則建立,如存在則在建立是不要打開該文件 using (var fs = File.OpenWrite(@"c:/joye.net.xls")) { workbook.Write(fs); //向打開的這個xls文件中寫入mySheet表並保存。 Console.WriteLine("生成成功"); }
2、NPOI讀取Excel
using (var fs = File.OpenRead(@"c:/joye.net.xls")) { //把xls文件中的數據寫入workbook1中 var workbook1 = new HSSFWorkbook(fs); for (var i = 0; i < workbook1.NumberOfSheets; i++) { var sheet = workbook1.GetSheetAt(i); for (var j = 0; j <= sheet.LastRowNum; j++) { //讀取當前行數據 var row = sheet.GetRow(j); if (row != null) { for (var k = 0; k <= row.LastCellNum; k++) { //當前表格 var cell = row.GetCell(k); if (cell != null) { Console.Write(cell.ToString() + " "); } } } Console.WriteLine(); } } }
讀出的結果學習
3、簡單學習
學習代碼
代碼調用
4、NPOI導出Excel 65536問題
public static HSSFWorkbook BuildWorkbook(DataTable dt) { var book = new HSSFWorkbook(); ISheet sheet = book.CreateSheet("Sheet1"); //Data Rows for (int i = 0; i < dt.Rows.Count; i++) { IRow drow = sheet.CreateRow(i); for (int j = 0; j < dt.Columns.Count; j++) { ICell cell = drow.CreateCell(j, CellType.String); cell.SetCellValue(dt.Rows[i][j].ToString()); } } //自動列寬 for (int i = 0; i <= dt.Columns.Count; i++) sheet.AutoSizeColumn(i, true); return book; }
NPOI導出Excel超過65536會報異常,原來是因爲NPOI這個動態庫致使的,而後看了下版本,發現是1.2.5。而後百度了下,發現這個版本的NPOI只支持office2003,二office2003最多支持65536行網站
解決方式:ui
一、只是在插入數據的時候,加個判斷,若是數據條數大於65536時,就在建立一個sheetspa
//65536判斷處理 public static HSSFWorkbook BuildWorkbook(DataTable dt) { var book = new HSSFWorkbook(); ISheet sheet1 = book.CreateSheet("Sheet1"); ISheet sheet2 = book.CreateSheet("Sheet2"); //填充數據 for (int i = 0; i < dt.Rows.Count; i++) { if (i < 65536) { IRow drow = sheet1.CreateRow(i); for (int j = 0; j < dt.Columns.Count; j++) { ICell cell = drow.CreateCell(j, CellType.String); cell.SetCellValue(dt.Rows[i][j].ToString()); } } if (i >= 65536) { IRow drow = sheet2.CreateRow(i - 65536); for (int j = 0; j < dt.Columns.Count; j++) { ICell cell = drow.CreateCell(j, CellType.String); cell.SetCellValue(dt.Rows[i][j].ToString()); } } } //自動列寬 for (int i = 0; i <= dt.Columns.Count; i++) { sheet1.AutoSizeColumn(i, true); sheet2.AutoSizeColumn(i, true); } return book; }
二、考慮使用高版本Office,使用用對象支持高版本的NPOI.net
//高版本 public static XSSFWorkbook BuildWorkbook(DataTable dt) { var book = new XSSFWorkbook(); ISheet sheet = book.CreateSheet("Sheet1"); //Data Rows for (int i = 0; i < dt.Rows.Count; i++) { IRow drow = sheet.CreateRow(i); for (int j = 0; j < dt.Columns.Count; j++) { ICell cell = drow.CreateCell(j, CellType.String); cell.SetCellValue(dt.Rows[i][j].ToString()); } } //自動列寬 for (int i = 0; i <= dt.Columns.Count; i++) sheet.AutoSizeColumn(i, true); return book; }
5、web開發中導出Excel
public static void ExportExcel(DataTable dt, string fileName = "") { //生成Excel IWorkbook book = BuildWorkbook(dt); //web 下載 if (fileName == "") fileName = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now); fileName = fileName.Trim(); string ext = Path.GetExtension(fileName); if (ext.ToLower() == ".xls" || ext.ToLower() == ".xlsx") fileName = fileName.Replace(ext, string.Empty); HttpResponse httpResponse = HttpContext.Current.Response; httpResponse.Clear(); httpResponse.Buffer = true; httpResponse.Charset = Encoding.UTF8.BodyName; httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); httpResponse.ContentEncoding = Encoding.UTF8; httpResponse.ContentType = "application/vnd.ms-excel; charset=UTF-8"; book.Write(httpResponse.OutputStream); httpResponse.End(); }
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Reflection; using System.Text; using System.Linq; namespace Gto.Report.Contract { public static class ExcelHelperForCs { /// <summary> /// 組裝workbook. /// </summary> /// <param name="dt">dataTable資源</param> /// <param name="columnHeader">表頭</param> /// <returns></returns> public static HSSFWorkbook BuildWorkbook1(DataTable dt, string columnHeader = "") { var workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(string.IsNullOrWhiteSpace(dt.TableName) ? "Sheet1" : dt.TableName); var dateStyle = workbook.CreateCellStyle(); var format = workbook.CreateDataFormat(); dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); //取得列寬 var arrColWidth = new int[dt.Columns.Count]; foreach (DataColumn item in dt.Columns) { arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; } for (var i = 0; i < dt.Rows.Count; i++) { for (var j = 0; j < dt.Columns.Count; j++) { int intTemp = Encoding.GetEncoding(936).GetBytes(dt.Rows[i][j].ToString()).Length; if (intTemp > arrColWidth[j]) { arrColWidth[j] = intTemp; } } } int rowIndex = 0; foreach (DataRow row in dt.Rows) { #region 表頭 列頭 if (rowIndex == 65535 || rowIndex == 0) { if (rowIndex != 0) { sheet = workbook.CreateSheet(); } #region 表頭及樣式 { var headerRow = sheet.CreateRow(0); headerRow.HeightInPoints = 25; headerRow.CreateCell(0).SetCellValue(columnHeader); //CellStyle ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;// 左右居中 headStyle.VerticalAlignment = VerticalAlignment.Center;// 上下居中 // 設置單元格的背景顏色(單元格的樣式會覆蓋列或行的樣式) headStyle.FillForegroundColor = (short)11; //定義font IFont font = workbook.CreateFont(); font.FontHeightInPoints = 20; font.Boldweight = 700; headStyle.SetFont(font); headerRow.GetCell(0).CellStyle = headStyle; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dt.Columns.Count - 1)); } #endregion #region 列頭及樣式 { var headerRow = sheet.CreateRow(1); //CellStyle ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;// 左右居中 headStyle.VerticalAlignment = VerticalAlignment.Center;// 上下居中 //定義font IFont font = workbook.CreateFont(); font.FontHeightInPoints = 10; font.Boldweight = 700; headStyle.SetFont(font); foreach (DataColumn column in dt.Columns) { headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); headerRow.GetCell(column.Ordinal).CellStyle = headStyle; sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); } } #endregion if (columnHeader != "") { //header row IRow row0 = sheet.CreateRow(0); for (int i = 0; i < dt.Columns.Count; i++) { ICell cell = row0.CreateCell(i, CellType.String); cell.SetCellValue(dt.Columns[i].ColumnName); } } rowIndex = 2; } #endregion #region 內容 var dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in dt.Columns) { var newCell = dataRow.CreateCell(column.Ordinal); string drValue = row[column].ToString(); switch (column.DataType.ToString()) { case "System.String"://字符串類型 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++; } //自動列寬 for (int i = 0; i <= dt.Columns.Count; i++) sheet.AutoSizeColumn(i, true); return workbook; } public static DataTable ToDataTable<T>(IList<T> items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } foreach (T item in items) { var values = new object[props.Length]; for (int i = 0; i < props.Length; i++) { values[i] = props[i].GetValue(item, null); } tb.Rows.Add(values); } return tb; } public static bool IsNullable(Type t) { return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } public static Type GetCoreType(Type t) { if (t != null && IsNullable(t)) { if (!t.IsValueType) { return t; } else { return Nullable.GetUnderlyingType(t); } } else { return t; } } /// <summary> /// DataTable導出Excel2007(.xlsx) /// </summary> /// <param name="dt">DataTable</param> /// <param name="file">文件路徑(.xlsx)</param> /// <param name="sheetname">Excel工做表名</param> public static void TableToExcelForXLSX2007(DataTable dt, string file, string sheetname) { XSSFWorkbook xssfworkbook = new XSSFWorkbook();//創建Excel2007對象 ISheet sheet = xssfworkbook.CreateSheet(sheetname);//新建一個名稱爲sheetname的工做簿 //設置基本樣式 ICellStyle style = xssfworkbook.CreateCellStyle(); style.WrapText = true; IFont font = xssfworkbook.CreateFont(); font.FontHeightInPoints = 9; font.FontName = "Arial"; style.SetFont(font); //設置統計樣式 ICellStyle style1 = xssfworkbook.CreateCellStyle(); style1.WrapText = true; IFont font1 = xssfworkbook.CreateFont(); font1.FontHeightInPoints = 9; font1.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold; font1.FontName = "Arial"; style1.SetFont(font1); //設置大類樣式 ICellStyle style2 = xssfworkbook.CreateCellStyle(); style2.WrapText = true; //style2.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Gold.Index; //style2.FillPattern = FillPattern.SolidForeground; IFont font2 = xssfworkbook.CreateFont(); font2.FontHeightInPoints = 9; font2.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold; font2.FontName = "Arial"; style2.SetFont(font2); //設置列名 IRow row = sheet.CreateRow(0); for (int i = 0; i < dt.Columns.Count; i++) { ICell cell = row.CreateCell(i); var rowName = dt.Columns[i].ColumnName; string rowRealName = ""; switch (rowName) { case "IncomeType": rowRealName = "交易類型"; break; case "CreateDate": rowRealName = "發生日期"; break; case "ChangeAmount": rowRealName = "合計金額"; break; case "SubsectionName": rowRealName = "分段名稱"; break; case "CorporateName": rowRealName = "公司名稱"; break; case "Province": rowRealName = "省份"; break; case "ShuntName": rowRealName = "項目"; break; case "CountAmount": rowRealName = "本年累計金額"; break; default: rowRealName = ""; break; } cell.SetCellValue(rowRealName); cell.CellStyle = style; } int paymentRowIndex = 1; //單元格賦值 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); if (dt.Rows[i][j].ToString().Contains("小計") || dt.Rows[i][j].ToString().Contains("流量淨額")) { cell.SetCellValue(dt.Rows[i][j].ToString()); cell.CellStyle = style2; } else if (dt.Rows[i][j].ToString().Contains("一") || dt.Rows[i][j].ToString().Contains("二") || dt.Rows[i][j].ToString().Contains("三")) { cell.SetCellValue(dt.Rows[i][j].ToString()); cell.CellStyle = style1; } else { cell.SetCellValue(dt.Rows[i][j].ToString()); cell.CellStyle = style; } } paymentRowIndex++; } //列寬自適應,只對英文和數字有效 for (int i = 0; i <= dt.Rows.Count; i++) { sheet.AutoSizeColumn(i); } //獲取當前列的寬度,而後對比本列的長度,取最大值 for (int columnNum = 0; columnNum <= dt.Rows.Count; columnNum++) { int columnWidth = sheet.GetColumnWidth(columnNum) / 256; for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++) { IRow currentRow; //當前行未被使用過 if (sheet.GetRow(rowNum) == null) { currentRow = sheet.CreateRow(rowNum); } else { currentRow = sheet.GetRow(rowNum); } if (currentRow.GetCell(columnNum) != null) { ICell currentCell = currentRow.GetCell(columnNum); int length = Encoding.Default.GetBytes(currentCell.ToString()).Length; if (columnWidth < length) { columnWidth = length; } } } sheet.SetColumnWidth(columnNum, columnWidth * 256); } using (System.IO.Stream stream = System.IO.File.OpenWrite(file)) { //寫入文件 xssfworkbook.Write(stream); stream.Close(); } } /// <summary> /// DataTable導出Excel2003(.xls) /// </summary> /// <param name="dt">DataTable</param> /// <param name="file">文件路徑(.xls)</param> /// <param name="sheetname">Excel工做表名</param> public static void TableToExcelForXLSX2003(DataTable dt, string file, string sheetname) { HSSFWorkbook xssfworkbook = new HSSFWorkbook();//創建Excel2003對象 HSSFSheet sheet = (HSSFSheet)xssfworkbook.CreateSheet(sheetname);//新建一個名稱爲sheetname的工做簿 //設置基本樣式 ICellStyle style = xssfworkbook.CreateCellStyle(); style.WrapText = true; IFont font = xssfworkbook.CreateFont(); font.FontHeightInPoints = 9; font.FontName = "Arial"; style.SetFont(font); //設置統計樣式 ICellStyle style1 = xssfworkbook.CreateCellStyle(); style1.WrapText = true; IFont font1 = xssfworkbook.CreateFont(); font1.FontHeightInPoints = 9; font1.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold; font1.FontName = "Arial"; style1.SetFont(font1); //設置大類樣式 ICellStyle style2 = xssfworkbook.CreateCellStyle(); style2.WrapText = true; //style2.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Gold.Index; //style2.FillPattern = FillPattern.SolidForeground; IFont font2 = xssfworkbook.CreateFont(); font2.FontHeightInPoints = 9; font2.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold; font2.FontName = "Arial"; style2.SetFont(font2); //設置列名 HSSFRow row = (HSSFRow)sheet.CreateRow(0); for (int i = 0; i < dt.Columns.Count; i++) { ICell cell = (ICell)row.CreateCell(i); var rowName = dt.Columns[i].ColumnName; //cell.SetCellValue(dt.Columns[i].ColumnName); string rowRealName = ""; switch (rowName) { case "IncomeType": rowRealName = "交易類型"; break; case "CreateDate": rowRealName = "發生日期"; break; case "ChangeAmount": rowRealName = "合計金額"; break; case "SubsectionName": rowRealName = "分段名稱"; break; case "CorporateName": rowRealName = "公司名稱"; break; case "Province": rowRealName = "省份"; break; case "ShuntName": rowRealName = "項目"; break; case "CountAmount": rowRealName = "本年累計金額"; break; default: rowRealName = ""; break; } cell.SetCellValue(rowRealName); cell.CellStyle = style; } int paymentRowIndex = 1; //單元格賦值 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); if (dt.Rows[i][j].ToString().Contains("小計") || dt.Rows[i][j].ToString().Contains("流量淨額")) { cell.SetCellValue(dt.Rows[i][j].ToString()); cell.CellStyle = style2; } else if (dt.Rows[i][j].ToString().Contains("一") || dt.Rows[i][j].ToString().Contains("二") || dt.Rows[i][j].ToString().Contains("三")) { cell.SetCellValue(dt.Rows[i][j].ToString()); cell.CellStyle = style1; } else { cell.SetCellValue(dt.Rows[i][j].ToString()); cell.CellStyle = style; } } paymentRowIndex++; } //列寬自適應,只對英文和數字有效 for (int i = 0; i <= dt.Rows.Count; i++) { sheet.AutoSizeColumn(i); } //獲取當前列的寬度,而後對比本列的長度,取最大值 for (int columnNum = 0; columnNum <= dt.Rows.Count; columnNum++) { int columnWidth = sheet.GetColumnWidth(columnNum) / 256; for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++) { IRow currentRow; //當前行未被使用過 if (sheet.GetRow(rowNum) == null) { currentRow = sheet.CreateRow(rowNum); } else { currentRow = sheet.GetRow(rowNum); } if (currentRow.GetCell(columnNum) != null) { ICell currentCell = currentRow.GetCell(columnNum); int length = Encoding.Default.GetBytes(currentCell.ToString()).Length; if (columnWidth < length) { columnWidth = length; } } } sheet.SetColumnWidth(columnNum, columnWidth * 256); } using (System.IO.Stream stream = System.IO.File.OpenWrite(file)) { xssfworkbook.Write(stream); stream.Close(); } } } }
基於.xls模板生成Excel文件有時間再看excel
代碼下載:https://yunpan.cn/cRBVnTCSchz7k (提取碼:779e)