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 ...
随机推荐
- poj 1961 Period
Period http://poj.org/problem?id=1961 Time Limit: 3000MS Memory Limit: 30000K Description Fo ...
- 动态规划:树形DP-景点中心(树的带权重心)
话说宁波市的中小学生在镇海中学参加计算机程序设计比赛,比赛之余,他们在镇海中学的各个景点参观.镇海中学共有n个景点,每个景点均有若干学生正在参 观.这n个景点以自然数1至n编号,每两个景点的编号均不同 ...
- 超酷JQuery动画分页按钮,鼠标悬停滑动展开
1.效果及功能说明 animate动画分页按钮制作鼠标悬停分页按钮上滑动展开分页按钮,鼠标离开后分页按钮收缩 2.实现原理 主要是靠动画方法,来让原本的箭头图像的长度发生变长,正好可以融入下标题的文字 ...
- 在EF6.0中打印数据库操作日志
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C++传递二维数字给一个自定义函数
如果参数是多维数组,那么参数必须指明第一维意外得所有未得长度:比如你的 void tt(char a[][20])或者 void tt(char (*a)[20]) 另外这样也是可以的char *a[ ...
- 18、Random类简介
Random类概述 Random类在java.util包下,使用这个类可以生成随机数 package com.sutaoyu.usually_class; import java.util.Rando ...
- base64 与字符串互转
#region 将Base64编码的文本转换成普通文本 /// <summary> /// 将Base64编码的文本转换成普通文本 /// </summary> /// < ...
- 关于 jQuery 中的 $.data() 方法和 jQuery 对象上的data 方法
参见文章:http://www.it165.net/pro/html/201404/11922.html
- sed实例收集
url:http://blog.csdn.net/hepeng597/article/details/7852468 一.元字符集 1)^锚定行的开始 如:/^sed/匹配所有以sed开头的行. ...
- shell函数-页面跳转练习->
实现思维导图-> 实现思路-> 分析:1:先把三个页面的流程作为函数先写下来,定义在脚本的开头,方便下面的调用.2:先从一个流 程开始做,其他的流程类似,比如nginx3:整体实现思路是 ...