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 ...
随机推荐
- SourceTree免密克隆仓库及问题说明
前言说明: 公司陆陆续续进新员工,新员工都需要了解公司各类资料,而公司资料都存储于gitlab中,因此,新员工需要安装工具进行资料拉取. 公司采用SourceTree的是工具.相关下载地址如下: WI ...
- Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...
- highCharts参数实例解释
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 20155212 2016-2017-2《Java程序设计》课程总结
每周博客 每周作业链接汇总 预备作业一:专业理解.未来展望.期望的师生关系. 预备作业二:HOMEWORK-2 预备作业三:HOMEWORK-3 第一周作业:学习教材Chapter 1 Java平台概 ...
- webrtc前景如何
首先WebRTC是什么? WebRTC --- Web browsers with Real-Time Communications (RTC). WebRTC是一个免费.开放的项目.使web浏览器通 ...
- 简明Python教程 ~ 随书笔记
本文是阅读<简明Python教程>所做的随书笔记,主要是记录一些自己不熟悉的用法,或者所看到的比较有意思的内容,本书英文版A Byte of Python, 中文译版 简明Python教程 ...
- 下拉框 select
1.select 用来做什么? select 用于实现下来下拉列表,其 html 结构是这样的: <select name="city" id="city" ...
- React Native新手入门
前言 React Native是最近非常火的一个话题,想要学习如何使用它,首先就要知道它是什么. 好像面对一个新手全面介绍它的文章还不多,我就归纳一下所有的资料和刚入门的小伙伴一起来认识它~ 将从以下 ...
- spfa+差分约束系统(D - POJ - 1201 && E - POJ - 1364&&G - POJ - 1)+建边的注意事项+超级源点的建立
题目链接:https://cn.vjudge.net/contest/276233#problem/D 具体大意: 给出n个闭合的整数区间[ai,bi]和n个整数c1,-,cn. 编写一个程序: 从标 ...
- 一步一步搭建oracle 11gR2 rac+dg之共享磁盘设置(三)【转】
一步一步在RHEL6.5+VMware Workstation 10上搭建 oracle 11gR2 rac + dg 之共享磁盘准备 (三) 注意:这一步是配置rac的过程中非常重要的一步,很多童鞋 ...