首先,通过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. [Baltic2009]Radio Transmission

    bzoj 1355: [Baltic2009]Radio Transmission http://www.lydsy.com/JudgeOnline/problem.php?id=1355 Time ...

  2. github 新创建repositories

    1. 在github上新建repo 2. 找到改repo的地址,用命令git clone https://....,  拉取到本地 3. 打开一个单独的窗口,打开此文件夹 4. 创建自己的python ...

  3. CentOS 6 / RHEL 6配置bonding 4模式

    实现bond 802.3ad or 4 模式:(IEEE 802.3ad), 方式:创建一个整合的组,这个组会共享网速和网络双工(duplex)设置.模式 4 会根据 IEEE 802.3ad 标准使 ...

  4. linux环境下mysql默认是区分表名大小写的

    在linux环境下,mysql默认表明是区分大小写的,我们可以查看全局变量发现: mysql> show variables like 'lower%'; +------------------ ...

  5. imperva 更改web界面的密码

    通过SSH作为用户根登录到MX(或通过另一个用户并提升) 运行命令“su oracle” //首先切换到oracle用户 sqlplus secure/(密码)   /用此命令登录到数据库     s ...

  6. 理解mipi协议【转】

    转自:http://blog.csdn.net/wanglining1987/article/details/50202615 完成mipi信号通道分配后,需要生成与物理层对接的时序.同步信号: MI ...

  7. Shell脚本中字符串判空:使用-z 字符串长度为0时,为真,-n字符串长度不为0,为真。这两个都不靠谱【转】

    最近发现使用  -z   和  -n  来判断字符串判空,或不空时,很不靠谱. 使用下面的方法最可靠: if [ "x${value}" == "x" ]    ...

  8. MySQL参数设置

    InnoDB配置 从MySQL 5.5版本开始,InnoDB就是默认的存储引擎并且它比任何其它存储引擎的使用要多得多.那也是为什么它需要小心配置的原因. 1 innodb_file_per_table ...

  9. 一步一步搭建oracle 11gR2 rac+dg之环境准备(二)【转】

    一步一步在RHEL6.5+VMware Workstation 10上搭建 oracle 11gR2 rac + dg 之环境准备 (二) 一步一步搭建oracle 11gR2 rac+dg之环境准备 ...

  10. asp.net mvc发送邮件

    参考文献: 第一篇:http://www.cnblogs.com/qinpengming/archive/2011/06/08/2075040.html 第二篇:http://www.cnblogs. ...