c# 使用NOPI 操作Excel

时间:2021-09-07 05:10:00

  比来项目需要导出Excel,找来找去,微软有本身的Excel组件 using Microsoft.Office.Core;using Microsoft.Office.Interop.Excel;,但是有一个短处,就是措施地址电脑安置Office,这个问题的确是致命的,因为导出处事我们要做在处事端,措施直接生成Excel,然后客户端路径去下载,所以我们不成能在部署处事的时候还要在处事器上安置office.最后终于发明有个NOPI库,可以很好的解决这个问题,此刻就将项目的Excel 片段记录一下

  NPOI,顾名思义,就是POI的.NET版本。那POI又是什么呢?POI是一套用Java写成的库,能够辅佐开发者在没有安置微软Office的情况下读写Office 97-2003的文件,撑持的文件格局包孕xls, doc, ppt等。在本文颁布时,POI的最新版本是3.5 beta 6。NPOI 1.x是基于POI 3.x版本开发的,与poi 3.2对应的版本是NPOI 1.2,

  此刻我们要做这样一个表格,设计到字体样式,合并单元格。

c# 使用NOPI 操作Excel

  创建表头样式,列样式还有正文样式

public static ICellStyle CreateHeaderStyle(IWorkbook book) { ICellStyle style = book.CreateCellStyle(); //设置单元格的样式:程度对齐居中 style.Alignment = HorizontalAlignment.Center; style.VerticalAlignment = VerticalAlignment.Center; //新建一个字体样式东西 IFont font = book.CreateFont(); //设置字体加粗样式 font.Boldweight = short.MaxValue; font.FontHeightInPoints = 20; font.Boldweight = (short)FontBoldWeight.Bold; font.FontName = "微软雅黑"; //使用SetFont要领将字体样式添加到单元格样式中 style.SetFont(font); return style; } public static ICellStyle CreateTitleStyle(IWorkbook book) { ICellStyle cellStyle = book.CreateCellStyle(); cellStyle.Alignment = HorizontalAlignment.Center; cellStyle.VerticalAlignment = VerticalAlignment.Center; IFont fontLeft = book.CreateFont(); fontLeft.FontHeightInPoints = 15; fontLeft.Boldweight = (short)FontBoldWeight.Bold; fontLeft.FontName = "宋体"; cellStyle.ShrinkToFit = true; cellStyle.SetFont(fontLeft); return cellStyle; } public static ICellStyle CreateContentStyle(IWorkbook book) { ICellStyle cellStyle = book.CreateCellStyle(); IFont fontLeft = book.CreateFont(); fontLeft.FontHeightInPoints = 15; fontLeft.FontName = "宋体"; cellStyle.ShrinkToFit = true; cellStyle.SetFont(fontLeft); return cellStyle; }

  一个Excel文件就是IWorkbook一个页就是 一个Isheet,行是IRow,一个单元格是ICell,知道这些就好办了。上面的就是创建各类样式,

  创建表格,对照注意的一点就是xlsx 格局的文件需要new  XSSFWorkbook(),创建xls格局的文件需要new HSSFWorkbook();

  合并单元格   sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10))

  创建行    IRow rowHeader = sheet.CreateRow(0);

  创建页      ISheet sheet = book.CreateSheet(dt.TableName);

  创建单元格   cell = rowTitle.CreateCell(i);

#region 写Excel 文件 //HSSFWorkbook book = new HSSFWorkbook(); IWorkbook book = null; if (filepath.IndexOf(".xlsx") > 0) // 2007版本 book = new XSSFWorkbook(); else if (filepath.IndexOf(".xls") > 0) // 2003版本 book = new HSSFWorkbook(); ISheet sheet = book.CreateSheet(dt.TableName); //创建Excel 头,合并单元格10列 sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10)); IRow rowHeader = sheet.CreateRow(0); //在行中:成立单元格,参数为列号,从0计 ICell cellHeader = rowHeader.CreateCell(0); //设置单元格内容 cellHeader.SetCellValue("健康一体机检测呈报"); cellHeader.CellStyle = CreateHeaderStyle(book); rowHeader.Height = 650; rowHeader.RowStyle = CreateHeaderStyle(book); //创建Excel 列 IRow rowTitle = sheet.CreateRow(1); rowTitle.Height = 500; ICell cell = null; for (int i = 0; i < 9; i++) { cell = rowTitle.CreateCell(i); cell.SetCellValue(dt.Columns[i].ColumnName); cell.CellStyle = CreateTitleStyle(book); } cell = rowTitle.CreateCell(9); cell.SetCellValue("心电"); sheet.AddMergedRegion(new CellRangeAddress(1, 1, 9, 23)); cell.CellStyle = CreateTitleStyle(book); cell = rowTitle.CreateCell(24); cell.SetCellValue("血压"); sheet.AddMergedRegion(new CellRangeAddress(1, 1, 24, 31)); cell.CellStyle = CreateTitleStyle(book); cell = rowTitle.CreateCell(43); cell.SetCellValue("血氧"); sheet.AddMergedRegion(new CellRangeAddress(1, 1, 32, 33)); cell.CellStyle = CreateTitleStyle(book); cell = rowTitle.CreateCell(34); cell.SetCellValue("体温"); sheet.AddMergedRegion(new CellRangeAddress(1, 1, 34, 35)); cell.CellStyle = CreateTitleStyle(book); cell = rowTitle.CreateCell(36); cell.SetCellValue("血糖"); sheet.AddMergedRegion(new CellRangeAddress(1, 1, 36, 37)); cell.CellStyle = CreateTitleStyle(book); cell = rowTitle.CreateCell(38); cell.SetCellValue("尿液"); sheet.AddMergedRegion(new CellRangeAddress(1, 1, 38, 48)); cell.CellStyle = CreateTitleStyle(book); rowTitle = sheet.CreateRow(2); for (int i = 9; i <= 48; i++) { cell = rowTitle.CreateCell(i); cell.SetCellValue(dt.Columns[i].ColumnName); cell.CellStyle = CreateTitleStyle(book); } for (int i = 0; i < 9; i++) { sheet.AddMergedRegion(new CellRangeAddress(1, 2, i, i)); } //开始写数据 for (int i = 0; i < dt.Rows.Count; i++) { IRow rowContent = sheet.CreateRow(i + 3); rowContent.Height = 500; for (int j = 0; j < dt.Columns.Count; j++) { cell = rowContent.CreateCell(j); if (cell != null) { cell.SetCellValue(Convert.ToString(dt.Rows[i][j])); cell.CellStyle = CreateContentStyle(book); } } } // 写入到客户端 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { book.Write(ms); using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write)) { byte[] d = ms.ToArray(); fs.Write(d, 0, d.Length); fs.Flush(); } book = null; } #endregion

  知道这些根基上就可以创建一个稍微庞大的表格了,但是在这之前,必需要进行NOPI库的引入,