首先,通过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文件的更多相关文章

  1. C#利用NPOI操作Excel文件

    NPOI作为开源免费的组件,功能强大,可用来读写Excel(兼容xls和xlsx两种版本).Word.PPT文件.可是要让我们记住所有的操作,这便有点困难了,至此,总结一些在开发中常用的针对Excel ...

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

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

  3. C#项目中操作Excel文件——使用NPOI库

    转载自:http://blog.csdn.net/dcrmg/article/details/52356236# 感谢-牧野- 实际C#项目中经常会涉及到需要对本地Excel文件进行操作,特别是一些包 ...

  4. NPOI操作Excel辅助类

    /// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...

  5. NPOI操作excel之写入数据到excel表

    在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...

  6. C#开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

  7. C#使用oledb操作excel文件的方法

    本文实例讲述了C#使用oledb操作excel文件的方法.分享给大家供大家参考.具体分析如下: 不管什么编程语言都会提供操作Excel文件的方式,C#操作Excel主要有以下几种方式: 1.Excel ...

  8. C# 如何使用NPOI操作Excel以及读取合并单元格等

    C#操作Excel方法有很多,以前用的需要电脑安装office才能用,但因为版权问题公司不允许安装office.所以改用NPOI进行Excel操作,基本上一些简单的Excel操作都没有问题,读写合并单 ...

  9. C# 操作 Excel 文件(.xls 或 .xlsx)

    在.net中,常用的操作excel文件的方式,有三种: OLE DB的形式, 第三方框架NPOI, Office组件. 总结: 通过对比,在读取大数据量的excel文件,建议用OLE DB的形式,把e ...

随机推荐

  1. [Luogu 3128] USACO15DEC Max Flow

    [Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...

  2. Linux文件系统_每一个的意义

    2017年1月10日, 星期二 Linux文件系统_每一个的意义 rootfs: 根文件系统 FHS:Linux /boot: 系统启动相关的文件,如内核.initrd,以及grub(bootload ...

  3. Python3中urllib使用与源代码

    Py2.x: Urllib库 Urllin2库 Py3.x: Urllib库 变化: 在Pytho2.x中使用import urllib2---对应的,在Python3.x中会使用import url ...

  4. 在Emacs中画思维导图

    是的,你没有看错.其实,不只画思维导图,画结构图.流程图等,都可以.那怎么办呢?就是借助 Graphviz . Graphviz 可以方便地表达概念之间的联系,因此用它画思维导图是可行的,再加上它是个 ...

  5. Presto通过RESTful接口新增Connector

    在实际使用Presto的过程中,经常会有以下的一些需求. 添加一个新的Catalog 对不再使用的Catalog希望把它删除 修改某个Catalog的参数 但在Presto中如果进行上述的修改,需要重 ...

  6. bzoj 5055: 膜法师——树状数组

    Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然,他能为长者所续的时间,为这三个维度上能量的乘 ...

  7. www.sojson.com网站高级JS加密破解

    在网上冲浪,看到了一个网站的JS加密,下面有一句话: 乍一看这句话吓一跳,我去这么猛,然后就很有兴趣想看看究竟是怎样一种加密算法. 对于破解JS加密算法的时候,都是先输入一个简单的语句然后分析加密后语 ...

  8. jQuery遍历方式

    对象遍历

  9. Hibernate5笔记8--Hibernate事务相关内容

    Hibernate事务相关内容: (1) 事务四大特性(简称ACID): (1)原子性(Atomicity) 事务中的全部操作在数据库中是不可分割的,要么全部完成,要么均不执行. (2)一致性(Con ...

  10. 【比赛游记】THUSC2018酱油记

    day -1 早上4:30就要起来去飞机场…… 7点的飞机,10:30就到北京了. 北京的街景并没有我想像的漂亮……大概是因为我在四环外〒▽〒 晚上还有CF div3场,果断的去水了,因为太累就没有打 ...