最近项目需要导出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,

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

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

 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 = ;
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 = ;
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 = ;
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") > ) // 2007版本
book = new XSSFWorkbook();
else if (filepath.IndexOf(".xls") > ) // 2003版本
book = new HSSFWorkbook();
ISheet sheet = book.CreateSheet(dt.TableName);
//创建Excel 头,合并单元格10列
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
IRow rowHeader = sheet.CreateRow();
//在行中:建立单元格,参数为列号,从0计
ICell cellHeader = rowHeader.CreateCell();
//设置单元格内容
cellHeader.SetCellValue("健康一体机检测报告");
cellHeader.CellStyle = CreateHeaderStyle(book);
rowHeader.Height = ;
rowHeader.RowStyle = CreateHeaderStyle(book); //创建Excel 列
IRow rowTitle = sheet.CreateRow();
rowTitle.Height = ;
ICell cell = null;
for (int i = ; i < ; i++)
{
cell = rowTitle.CreateCell(i);
cell.SetCellValue(dt.Columns[i].ColumnName);
cell.CellStyle = CreateTitleStyle(book);
}
cell = rowTitle.CreateCell();
cell.SetCellValue("心电");
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
cell.CellStyle = CreateTitleStyle(book);
cell = rowTitle.CreateCell();
cell.SetCellValue("血压");
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
cell.CellStyle = CreateTitleStyle(book);
cell = rowTitle.CreateCell();
cell.SetCellValue("血氧");
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
cell.CellStyle = CreateTitleStyle(book);
cell = rowTitle.CreateCell();
cell.SetCellValue("体温");
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
cell.CellStyle = CreateTitleStyle(book);
cell = rowTitle.CreateCell();
cell.SetCellValue("血糖");
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
cell.CellStyle = CreateTitleStyle(book);
cell = rowTitle.CreateCell();
cell.SetCellValue("尿液");
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
cell.CellStyle = CreateTitleStyle(book);
rowTitle = sheet.CreateRow();
for (int i = ; i <= ; i++)
{
cell = rowTitle.CreateCell(i);
cell.SetCellValue(dt.Columns[i].ColumnName);
cell.CellStyle = CreateTitleStyle(book);
} for (int i = ; i < ; i++)
{
sheet.AddMergedRegion(new CellRangeAddress(, , i, i));
}
//开始写数据
for (int i = ; i < dt.Rows.Count; i++)
{
IRow rowContent = sheet.CreateRow(i + );
rowContent.Height = ;
for (int j = ; 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, , d.Length);
fs.Flush();
}
book = null;
}
#endregion

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

  官方网站:http://npoi.codeplex.com/,里面下载最新的版本库,目前应该是2.3版本的。下载好之后,进入到npoi-master\npoi-master\solution\visualstudio,打开OOXML.sln,进行重新生成DLL,在npoi-master\npoi-master\solution\Lib这个目录。 如下所示

  我们需要的是将NOPI.DLL  ,NOPI.OOXML.DLL ,NOPI.OPENXML4NET.DLL,ICSharpCode.SharpZipLib.DLL.NPOI.OpenXmlFormats.dll这五个库进入到工程即可。现在将编译好的库供大家下载   https://files.cnblogs.com/files/techdreaming/nopi.zip

c# 使用NOPI 操作Excel的更多相关文章

  1. 使用NPIO操作Excel

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...

  2. NOPI导出Excel 自定义列名

    NOPI 做Excel 导出确实很方便 ,但是一直在用没好好研究. 在网上没找到自定义Columns的方法 ,于是乎自己就在原来的方法上简单地改改. 想用的童鞋们可以直接拿去用! /// 数据大于65 ...

  3. 使用NPOI操作Excel文件及其日期处理

    工作中经常遇到需要读取或导出Excel文件的情况,而NPOI是目前最宜用.效率最高的操作的Office(不只是Excel哟)文件的组件,使用方便,不详细说明了. Excel工作表约定:整个Excel表 ...

  4. .Net Core NOPI操作word(二) 表格操作

    一.创建表格操作 private void btnExport_Click(object sender, EventArgs e) { var dbcontext = new BlogModel(); ...

  5. 免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)

    很多的软件项目几乎都包含着对文档的操作,前面已经介绍过两款操作文档的组件,现在介绍一款文档操作的组件NPOI. NPOI可以生成没有安装在您的服务器上的Microsoft Office套件的Excel ...

  6. C#通过NPOI操作Excel

    参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-post.html http://www.yuanjiaocheng.net/w ...

  7. POI操作Excel

    POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...

  8. NPOI操作EXCEL(六)——矩阵类表头EXCEL模板的解析

    哈哈~~~很高兴还活着.总算加班加点的把最后一类EXCEL模板的解析做完了... 前面几篇文章介绍了博主最近项目中对于复杂excel表头的解析,写得不好,感谢园友们的支持~~~ 今天再简单讲诉一下另一 ...

  9. VB操作EXCEL文件

    用VB操作Excel(VB6.0)(整理) 首先创建Excel对象,使用ComObj:Dim ExcelID as Excel.ApplicationSet ExcelID as new Excel. ...

随机推荐

  1. Head First PHP &MySQL学习笔记

      最近一段时间在学习PHP,买了<Head First PHP&MySQL>中文版这本书,之前买过<Head First设计模式>,感觉这系列的书籍总体来说很不错. ...

  2. pytest的执行方式及搜索原则

    pytest的执行方式 Pytest/py.test(终端,命令行,pycharm可配置pytest方式执行) 1. Pytest –v (最高级别信息—verbose) 2. pytest -v - ...

  3. 函数节流及手机端点击延迟200ms解决方法

    不论是H5开发还是微信小程序,手机端点击都会有300ms的延迟,在实际项目中,会到此频繁触发,如有接口会频繁的请求接口,除了会引起不必要的多次请求还会导致数据有问题.下面有二种方式来处理这个问题: 1 ...

  4. 【记录】Nginx错误could not build the server_names_hash you should increase server_names_hash_bucket_size: 32

    今天遇到这个错误,现记录下解决方案: 在nginx的配置文件的http段中增加如下配置: server_names_hash_bucket_size 64; 下面是nginx官方文档解释: 如果定义了 ...

  5. 2019-8-31-gif-格式

    title author date CreateTime categories gif 格式 lindexi 2019-08-31 16:55:59 +0800 2018-2-13 17:23:3 + ...

  6. shell变量替换扩展 变量测试

  7. java反射的使用场合和作用、及其优缺点

    1)使用场合 在编译时根本无法知道该对象或类可能属于哪些类,程序只依靠运行时信息来发现该对象和类的真实信息. 2)主要作用 通过反射可以使程序代码访问装载到JVM 中的类的内部信息,获取已装载类的属性 ...

  8. 基于.Net4.0实现 ToastNotification

    基于.Net4.0实现 ToastNotification Windows更新之路的特色之一就是消息提示由气泡变成了通知窗口,效果简直不要太好.最近公司有这方面的需求,需要在xp,win7系统上给出提 ...

  9. Hive学内置条件和字符串函数

    https://blog.csdn.net/skywalker_only/article/details/38752003 条件函数 下表为Hive支持的一些条件函数. 返回类型 函数名 描述 T i ...

  10. Shiro学习(10)Session管理

    Shiro提供了完整的企业级会话管理功能,不依赖于底层容器(如web容器tomcat),不管JavaSE还是JavaEE环境都可以使用,提供了会话管理.会话事件监听.会话存储/持久化.容器无关的集群. ...