NPOI導出excel(居中,合並單元格),excel表頭作為參數傳入
BLL層:
using System; using System.Collections.Generic; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using NPOI.HSSF.UserModel; using System.IO; using System.Data; using NPOI.SS.Util; namespace Business { public class ExcelHelper : IDisposable { private string fileName = null; //文件名 private IWorkbook workbook = null; private FileStream fs = null; private bool disposed; public ExcelHelper(string fileName) { this.fileName = fileName; disposed = false; } /// <summary> /// DataTable數據導入到excel /// </summary> /// <param name="title">excel表頭數組</param> /// <param name="data">datatable數據</param> /// <param name="sheetName"></param> /// <returns></returns> public int DataTableToExcel(string[] title, DataTable data, string sheetName) { int count = 1; ISheet sheet = null; fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 workbook = new XSSFWorkbook(); else if (fileName.IndexOf(".xls") > 0) // 2003版本 workbook = new HSSFWorkbook(); ICellStyle cellstyle = workbook.CreateCellStyle(); cellstyle.VerticalAlignment = VerticalAlignment.Center; cellstyle.Alignment = HorizontalAlignment.Center; try { if (workbook != null) { sheet = workbook.CreateSheet(sheetName); } else { return -1; } //excel表頭 IRow rowtitle = sheet.CreateRow(0); for (var j = 0; j < title.Length; ++j) { ICell cell = rowtitle.CreateCell(j); cell.SetCellValue(title[j]); cell.CellStyle = cellstyle; } //data for (var i = 0; i < data.Rows.Count; ++i) { IRow rowdata = sheet.CreateRow(count); ICell cellNo = rowdata.CreateCell(0); cellNo.SetCellValue(i+1); cellNo.CellStyle = cellstyle; for (var j = 0; j < data.Columns.Count; ++j) { ICell cell = rowdata.CreateCell(j+1); cell.SetCellValue(data.Rows[i][j].ToString()); cell.CellStyle = cellstyle; } ++count; } workbook.Write(fs); //寫入到excel return count; } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); return -1; } } /// <summary> /// DataTable數據導出到excel /// </summary> /// <param name="title">多表頭二維數組</param> /// <param name="data">dt數據</param> /// <param name="sheetName"></param> /// <returns></returns> public int DataTableToExcel(string[,] title,DataTable data, string sheetName) { int count; ISheet sheet = null; fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 workbook = new XSSFWorkbook(); else if (fileName.IndexOf(".xls") > 0) // 2003版本 workbook = new HSSFWorkbook(); ICellStyle cellstyle = workbook.CreateCellStyle(); cellstyle.VerticalAlignment = VerticalAlignment.Center; cellstyle.Alignment = HorizontalAlignment.Center; try { if (workbook != null) { sheet = workbook.CreateSheet(sheetName); } else { return -1; } //表頭 for (var k = 0; k < title.GetLength(0); k++) { IRow rowtitle = sheet.CreateRow(k); for (var m = 0; m < title.GetLength(1); m++) { ICell cell = rowtitle.CreateCell(m); cell.SetCellValue(title[k, m]); cell.CellStyle = cellstyle; if ((k == 0 && m < 9) || (k == 0 && m > 14)) { sheet.AddMergedRegion(new CellRangeAddress(k, k + 1, m, m)); } sheet.AddMergedRegion(new CellRangeAddress(0, 0, 8, 10)); sheet.AddMergedRegion(new CellRangeAddress(0, 0, 11, 14)); } } //data count = title.GetLength(0); for (var i = 0; i < data.Rows.Count; ++i) { IRow rowdata = sheet.CreateRow(count); ICell cellNo = rowdata.CreateCell(0); cellNo.SetCellValue(i + 1); cellNo.CellStyle = cellstyle; for (var j = 0; j < data.Columns.Count; ++j) { ICell cell = rowdata.CreateCell(j+1); cell.SetCellValue(data.Rows[i][j].ToString()); cell.CellStyle = cellstyle; } ++count; } workbook.Write(fs); //寫入到excel return count; } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); return -1; } } }
Controller代碼:
public string CreateProjectExcel() { string fileName = Utils.GetCurrentTime() + ".xls"; string path = Server.MapPath("~/") + "Temp\\"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } path = path + fileName; DataTable data = QueryData(9); string[] title = new string[] { "序號", "編號", "企業名稱", "所屬一級企業", "變化類型", "調整后的上級單位", "所在企業層級代碼", "調整前企業編碼", "原所屬一級企業", "備注" }; using (ExcelHelper eh = new ExcelHelper(path)) { int count = eh.DataTableToExcel(title, data, "二級企業宗地編號"); } return fileName; } public string CreateExcel_tdjbqk() { string[,] title = {{"序號", "宗地編號", "所屬一級企業", "地籍號", "土地面積(平方米)", "宗地位置", "土地使用權人", "土地區域范圍", "土地取得方式", "土地權屬","", "土地利用、開發","","","","使用者與權利人是否一致", "變化類型", "調整列的列號", "調整前內容", "原宗地編號", "原所屬一級企業", "備注"},{ "", "", "", "", "", "", "", "", "", "土地證件辦理情況", "未辦理土地證原因", "出租情況", "是否已列入污染擾民搬遷", "是否已列入拆遷范圍", "是否已列入開發土地","","","","","","",""}}; string fileName = Utils.GetCurrentTime() + ".xls"; DataTable dt = QueryData(16); string path = Server.MapPath("~/") + "Temp\\"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } path = path + fileName; using (ExcelHelper eh = new ExcelHelper(path)) { int count = eh.DataTableToExcel(title,dt, "土地基本情況"); } return fileName; }
html中:
$("#btn_op_search").click(function () { $.post('/Report/CreateExcel_tdjbqk', function (data) { window.location.href = '../Temp/' + data; }); });
