首先,通过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. 2015/11/6用Python写游戏,pygame入门(6):控制大量的对象

    昨天我们已经实现了这个游戏的三个基本类. 但是现在它还是没办法做成一个适合玩的游戏,毕竟只有一架敌机的游戏是很乏味的.所以,我们需要好多子弹,也需要好多敌机. 所以,我们要创建list,这个list存 ...

  2. 【实操笔记】MySQL主从同步功能实现

    写在前边: 这两天来了个需求,配置部署两台服务器的MySQL数据同步,折腾了两天查了很多相关资料,一直连不上,后来发现其实是数据库授权的ip有问题,我们用的服务器是机房中的虚拟机加上反向代理出来的,坑 ...

  3. 《PHP和MySQL Web开发》读书笔记(上篇)

    最近过得太浮躁了,实在自己都看不下去了,看了PHP圣经之后,觉得非常有必要要总结一下. Chapter1.快速入门 ·PHP标记:总共有三种风格,常用的还是XML风格为主 <?php echo ...

  4. HDU 4370 0 or 1 (最短路)

    [题目链接](http://acm.hdu.edu.cn/showproblem.ph Problem Description Given a n/n matrix Cij (1<=i,j< ...

  5. node.js、git、bootstrap等安装配置

    纯记录 一,安装node.js 1 官方网址 http://nodejs.org/  点击install 下载node-v0.10.22-x86.msi 2 安装,修改安装目录到d盘,一路next,无 ...

  6. 【Python】POST上传APK检测是否存在ZipperDown漏洞

    前言 用POST的方式上传文件,检测APK是否存在ZipperDown漏洞. 代码 # authour:zzzhhh # 2018.08.08 # check ZipperDown # -*- cod ...

  7. DRM学习总结(1)--- DRM框架介绍

    一.DRM 简介 In computing, the Direct Rendering Manager (DRM), a subsystem of the Linux kernel, interfac ...

  8. Linux时间子系统之七:定时器的应用--msleep(),hrtimer_nanosleep()【转】

    转自:http://blog.csdn.net/droidphone/article/details/8104433 我们已经在前面几章介绍了低分辨率定时器和高精度定时器的实现原理,内核为了方便其它子 ...

  9. 爬虫基础---HTTP协议理解、网页的基础知识、爬虫的基本原理

    一.HTTP协议的理解 URL和URI 在学习HTTP之前我们需要了解一下URL.URI(精确的说明某资源的位置以及如果去访问它) URL:Universal Resource Locator 统一资 ...

  10. 数据库——mysql如何获取当前时间

    1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp() curren ...