NPOI操作Excel文件
首先,通过NuGet添加NPOI.
NPOI依赖SharpZipLib,通过NuGet添加SharpZipLib.
然后添加NPOI.
添加后项目的引用列表如下:
把DataTable转换成Excel文件。
代码如下:
public static MemoryStream RenderDataTableToExcel(DataTable table)
{
MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(table.TableName); for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in table.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString());
}
} workbook.Write(ms);
ms.Close(); return ms;
}
转换Excel文件内容如下:
Excel文件添加表头
代码:
public static MemoryStream RenderDataTableToExcelWithHeader(DataTable table)
{
MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(table.TableName);
IRow headerRow = sheet.CreateRow();
foreach (DataColumn column in table.Columns)
{
headerRow.CreateCell(column.Ordinal).SetCellValue(string.Format(" {0} ", column.Caption));
} for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
{
IRow dataRow = sheet.CreateRow(rowIndex+);
foreach (DataColumn column in table.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString());
}
} workbook.Write(ms);
ms.Close(); return ms;
}
转换Excel文件内容如下:
添加Excel文件添加表头样式
代码:
public static MemoryStream RenderDataTableToExcelWithHeaderRowStyle(DataTable table)
{
MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(table.TableName);
IRow headerRow = sheet.CreateRow(); ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.Center;
headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
headStyle.FillPattern = FillPattern.SolidForeground;
IFont font = workbook.CreateFont();
font.FontName = "Microsoft Yahei";
font.FontHeightInPoints = ;
font.IsBold = true;
font.Color = HSSFColor.White.Index;
headStyle.SetFont(font);
headStyle.BorderBottom = BorderStyle.Thin;
headStyle.BorderRight = BorderStyle.Thin;
headStyle.BorderLeft = BorderStyle.Thin; foreach (DataColumn column in table.Columns)
{
ICell cell = headerRow.CreateCell(column.Ordinal);
cell.SetCellValue(string.Format(" {0} ", column.Caption));
cell.CellStyle = headStyle;
} for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
{
IRow dataRow = sheet.CreateRow(rowIndex + );
foreach (DataColumn column in table.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString());
}
} workbook.Write(ms);
ms.Close(); return ms;
}
转换Excel文件内容如下:
添加Excel文件添加数据行样式
代码:
public static MemoryStream RenderDataTableToExcelWithDataRowStyle(DataTable table)
{
MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(table.TableName);
IRow headerRow = sheet.CreateRow(); ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.Center;
headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
headStyle.FillPattern = FillPattern.SolidForeground;
IFont font = workbook.CreateFont();
font.FontName = "Microsoft Yahei";
font.FontHeightInPoints = ;
font.IsBold = true;
font.Color = HSSFColor.White.Index;
headStyle.SetFont(font);
headStyle.BorderBottom = BorderStyle.Thin;
headStyle.BorderRight = BorderStyle.Thin;
headStyle.BorderLeft = BorderStyle.Thin; ICellStyle dataRowEvenStyle = workbook.CreateCellStyle();
dataRowEvenStyle.Alignment = HorizontalAlignment.Center;
dataRowEvenStyle.FillForegroundColor = HSSFColor.LightOrange.Index;
dataRowEvenStyle.FillPattern = FillPattern.SolidForeground;
IFont dataRowEvenFont = workbook.CreateFont();
dataRowEvenFont.FontName = "Microsoft Yahei";
dataRowEvenFont.FontHeightInPoints = ;
dataRowEvenFont.Color = HSSFColor.Blue.Index;
dataRowEvenStyle.SetFont(dataRowEvenFont);
dataRowEvenStyle.BorderBottom = BorderStyle.Thin;
dataRowEvenStyle.BorderRight = BorderStyle.Thin;
dataRowEvenStyle.BorderLeft = BorderStyle.Thin; ICellStyle dataRowOddStyle = workbook.CreateCellStyle();
dataRowOddStyle.Alignment = HorizontalAlignment.Center;
dataRowOddStyle.FillForegroundColor = HSSFColor.LightGreen.Index;
dataRowOddStyle.FillPattern = FillPattern.SolidForeground;
IFont dataRowOddFont = workbook.CreateFont();
dataRowOddFont.FontName = "Microsoft Yahei";
dataRowOddFont.FontHeightInPoints = ;
dataRowOddFont.Color = HSSFColor.Black.Index;
dataRowOddStyle.SetFont(dataRowOddFont);
dataRowOddStyle.BorderBottom = BorderStyle.Thin;
dataRowOddStyle.BorderRight = BorderStyle.Thin;
dataRowOddStyle.BorderLeft = BorderStyle.Thin; foreach (DataColumn column in table.Columns)
{
ICell cell = headerRow.CreateCell(column.Ordinal);
cell.SetCellValue(string.Format(" {0} ", column.Caption));
cell.CellStyle = headStyle;
} for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
{
IRow dataRow = sheet.CreateRow(rowIndex + );
foreach (DataColumn column in table.Columns)
{
ICell cell = dataRow.CreateCell(column.Ordinal);
cell.SetCellValue(table.Rows[rowIndex][column].ToString());
if (rowIndex % == )
{
cell.CellStyle = dataRowEvenStyle;
}
else
{
cell.CellStyle = dataRowOddStyle;
}
}
} workbook.Write(ms);
ms.Close(); return ms;
}
转换Excel文件内容如下:
Excel文件合并单元格
代码:
public static MemoryStream RenderDataTableToExcelMergedRegion(DataTable table)
{
MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(table.TableName);
IRow headerRow = sheet.CreateRow(); ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.Center;
headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
headStyle.FillPattern = FillPattern.SolidForeground;
IFont font = workbook.CreateFont();
font.FontName = "Microsoft Yahei";
font.FontHeightInPoints = ;
font.IsBold = true;
font.Color = HSSFColor.White.Index;
headStyle.SetFont(font);
headStyle.BorderBottom = BorderStyle.Thin;
headStyle.BorderRight = BorderStyle.Thin;
headStyle.BorderLeft = BorderStyle.Thin; ICellStyle dataRowEvenStyle = workbook.CreateCellStyle();
dataRowEvenStyle.Alignment = HorizontalAlignment.Center;
dataRowEvenStyle.FillForegroundColor = HSSFColor.LightOrange.Index;
dataRowEvenStyle.FillPattern = FillPattern.SolidForeground;
IFont dataRowEvenFont = workbook.CreateFont();
dataRowEvenFont.FontName = "Microsoft Yahei";
dataRowEvenFont.FontHeightInPoints = ;
dataRowEvenFont.Color = HSSFColor.Blue.Index;
dataRowEvenStyle.SetFont(dataRowEvenFont);
dataRowEvenStyle.BorderBottom = BorderStyle.Thin;
dataRowEvenStyle.BorderRight = BorderStyle.Thin;
dataRowEvenStyle.BorderLeft = BorderStyle.Thin; ICellStyle dataRowOddStyle = workbook.CreateCellStyle();
dataRowOddStyle.Alignment = HorizontalAlignment.Center;
dataRowOddStyle.FillForegroundColor = HSSFColor.LightGreen.Index;
dataRowOddStyle.FillPattern = FillPattern.SolidForeground;
IFont dataRowOddFont = workbook.CreateFont();
dataRowOddFont.FontName = "Microsoft Yahei";
dataRowOddFont.FontHeightInPoints = ;
dataRowOddFont.Color = HSSFColor.Black.Index;
dataRowOddStyle.SetFont(dataRowOddFont);
dataRowOddStyle.BorderBottom = BorderStyle.Thin;
dataRowOddStyle.BorderRight = BorderStyle.Thin;
dataRowOddStyle.BorderLeft = BorderStyle.Thin; foreach (DataColumn column in table.Columns)
{
ICell cell = headerRow.CreateCell(column.Ordinal);
cell.SetCellValue(string.Format(" {0} ", column.Caption));
cell.CellStyle = headStyle;
} for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
{
IRow dataRow = sheet.CreateRow(rowIndex + );
foreach (DataColumn column in table.Columns)
{
ICell cell = dataRow.CreateCell(column.Ordinal);
cell.SetCellValue(table.Rows[rowIndex][column].ToString());
if (rowIndex % == )
{
cell.CellStyle = dataRowEvenStyle;
}
else
{
cell.CellStyle = dataRowOddStyle;
}
}
} sheet.AddMergedRegion(new CellRangeAddress(, , , ));
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
sheet.AddMergedRegion(new CellRangeAddress(, , , )); workbook.Write(ms);
ms.Close(); return ms;
}
转换Excel文件内容如下:
NPOI操作Excel文件的更多相关文章
- C#利用NPOI操作Excel文件
NPOI作为开源免费的组件,功能强大,可用来读写Excel(兼容xls和xlsx两种版本).Word.PPT文件.可是要让我们记住所有的操作,这便有点困难了,至此,总结一些在开发中常用的针对Excel ...
- 使用NPOI操作Excel文件及其日期处理
工作中经常遇到需要读取或导出Excel文件的情况,而NPOI是目前最宜用.效率最高的操作的Office(不只是Excel哟)文件的组件,使用方便,不详细说明了. Excel工作表约定:整个Excel表 ...
- C#项目中操作Excel文件——使用NPOI库
转载自:http://blog.csdn.net/dcrmg/article/details/52356236# 感谢-牧野- 实际C#项目中经常会涉及到需要对本地Excel文件进行操作,特别是一些包 ...
- NPOI操作Excel辅助类
/// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...
- NPOI操作excel之写入数据到excel表
在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...
- C#开发中使用Npoi操作excel实例代码
C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...
- C#使用oledb操作excel文件的方法
本文实例讲述了C#使用oledb操作excel文件的方法.分享给大家供大家参考.具体分析如下: 不管什么编程语言都会提供操作Excel文件的方式,C#操作Excel主要有以下几种方式: 1.Excel ...
- C# 如何使用NPOI操作Excel以及读取合并单元格等
C#操作Excel方法有很多,以前用的需要电脑安装office才能用,但因为版权问题公司不允许安装office.所以改用NPOI进行Excel操作,基本上一些简单的Excel操作都没有问题,读写合并单 ...
- C# 操作 Excel 文件(.xls 或 .xlsx)
在.net中,常用的操作excel文件的方式,有三种: OLE DB的形式, 第三方框架NPOI, Office组件. 总结: 通过对比,在读取大数据量的excel文件,建议用OLE DB的形式,把e ...
随机推荐
- bzoj千题计划157:bzoj1220:[HNOI2002]跳蚤
扩展欧几里得:ax+by=gcd(a,b) 一定有解 能跳到左边一格,即ax+by=-1 若a,b的gcd=1,则一定有解 所以问题转化为 求n个不大于m的数,他们与m的gcd=1 的方案数 容斥原理 ...
- opencv产生随机的颜色
//将HSV颜色空间值转换成RGB值,参考这里cv::Scalar HSV2RGB(const float h, const float s, const float v) { ); - h_i; - ...
- Django框架下的小人物--Cookie
1. 什么是Cookie,它的用途是什么? Cookies是一些存储在用户电脑上的小文件.它是被设计用来保存一些站点的用户数据,这样能够让服务器为这样的用户定制内容,后者页面代码能够获取到Cookie ...
- ORB_SLAM2 源码阅读 ORB_SLAM2::ORBextractor
整体架构 构造函数进行初始化,传入设定几个重要的成员变量.nfeatures(特征点的个数).nlevels(构造金字塔的层数).scaleFactor(金字塔中相邻层图像的比例系数).iniThFA ...
- CMD命令利用tasklist与taskkill关闭程序
昨天远程服务器后,服务器无故卡住了,鼠标各种延迟与无反应,想在进程管理器里关闭程序也卡住,想点击重启系统也卡死无反应.纠结后win+R打开了cmd用shutdown重启才算搞定.重启期间思考了下,如何 ...
- 数据库名(DB_NAME)、实例名(Instance_name)、以及操作系统环境变量(ORACLE_SID)
数据库名(DB_NAME).实例名(Instance_name).以及操作系统环境变量(ORACLE_SID) 在ORACLE7.8数据库中只有数据库名(db_name)和数据库实例名(instanc ...
- 关于注入抽象类报could not autowire field的问题
昨天工作中遇到了一个很奇葩的问题,之前一直都没考虑过抽象类这块,一直用的注入接口实现类: 先看下错误: 因为在类中注入了一个抽象类,之前只有一个继承子类,所以没问题,这里要说一下抽象类的实例化: 抽象 ...
- Tomcat安装与优化
Tomcat安装与优化 1.安装jdk环境 最新的JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downlo ...
- Jenkins关联GitHub进行构建
一.创建一个自由风格的项目 并在高级中勾选你构建完成后保存项目的路径 二.配置你存放代码的GitHub的地址并添加用户名密码 三.立即构建
- elasticsearch5.5-head
修改 elasticsearch/config/elasticsearch.yml 添加 http.cors.enabled: true http.cors.allow-origin: "* ...