首先,通过NuGet添加NPOI.

NPOI依赖SharpZipLib,通过NuGet添加SharpZipLib.

然后添加NPOI.

添加后项目的引用列表如下:

把DataTable转换成Excel文件。

代码如下:

  1. public static MemoryStream RenderDataTableToExcel(DataTable table)
  2. {
  3. MemoryStream ms = new MemoryStream();
  4.  
  5. IWorkbook workbook = new HSSFWorkbook();
  6. ISheet sheet = workbook.CreateSheet(table.TableName);
  7.  
  8. for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
  9. {
  10. IRow dataRow = sheet.CreateRow(rowIndex);
  11. foreach (DataColumn column in table.Columns)
  12. {
  13. dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString());
  14. }
  15. }
  16.  
  17. workbook.Write(ms);
  18. ms.Close();
  19.  
  20. return ms;
  21. }

转换Excel文件内容如下:

Excel文件添加表头

代码:

  1. public static MemoryStream RenderDataTableToExcelWithHeader(DataTable table)
  2. {
  3. MemoryStream ms = new MemoryStream();
  4.  
  5. IWorkbook workbook = new HSSFWorkbook();
  6. ISheet sheet = workbook.CreateSheet(table.TableName);
  7. IRow headerRow = sheet.CreateRow();
  8. foreach (DataColumn column in table.Columns)
  9. {
  10. headerRow.CreateCell(column.Ordinal).SetCellValue(string.Format(" {0} ", column.Caption));
  11. }
  12.  
  13. for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
  14. {
  15. IRow dataRow = sheet.CreateRow(rowIndex+);
  16. foreach (DataColumn column in table.Columns)
  17. {
  18. dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString());
  19. }
  20. }
  21.  
  22. workbook.Write(ms);
  23. ms.Close();
  24.  
  25. return ms;
  26. }

转换Excel文件内容如下:

添加Excel文件添加表头样式

代码:

  1. public static MemoryStream RenderDataTableToExcelWithHeaderRowStyle(DataTable table)
  2. {
  3. MemoryStream ms = new MemoryStream();
  4.  
  5. IWorkbook workbook = new HSSFWorkbook();
  6. ISheet sheet = workbook.CreateSheet(table.TableName);
  7. IRow headerRow = sheet.CreateRow();
  8.  
  9. ICellStyle headStyle = workbook.CreateCellStyle();
  10. headStyle.Alignment = HorizontalAlignment.Center;
  11. headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
  12. headStyle.FillPattern = FillPattern.SolidForeground;
  13. IFont font = workbook.CreateFont();
  14. font.FontName = "Microsoft Yahei";
  15. font.FontHeightInPoints = ;
  16. font.IsBold = true;
  17. font.Color = HSSFColor.White.Index;
  18. headStyle.SetFont(font);
  19. headStyle.BorderBottom = BorderStyle.Thin;
  20. headStyle.BorderRight = BorderStyle.Thin;
  21. headStyle.BorderLeft = BorderStyle.Thin;
  22.  
  23. foreach (DataColumn column in table.Columns)
  24. {
  25. ICell cell = headerRow.CreateCell(column.Ordinal);
  26. cell.SetCellValue(string.Format(" {0} ", column.Caption));
  27. cell.CellStyle = headStyle;
  28. }
  29.  
  30. for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
  31. {
  32. IRow dataRow = sheet.CreateRow(rowIndex + );
  33. foreach (DataColumn column in table.Columns)
  34. {
  35. dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[rowIndex][column].ToString());
  36. }
  37. }
  38.  
  39. workbook.Write(ms);
  40. ms.Close();
  41.  
  42. return ms;
  43. }

转换Excel文件内容如下:

添加Excel文件添加数据行样式

代码:

  1. public static MemoryStream RenderDataTableToExcelWithDataRowStyle(DataTable table)
  2. {
  3. MemoryStream ms = new MemoryStream();
  4.  
  5. IWorkbook workbook = new HSSFWorkbook();
  6. ISheet sheet = workbook.CreateSheet(table.TableName);
  7. IRow headerRow = sheet.CreateRow();
  8.  
  9. ICellStyle headStyle = workbook.CreateCellStyle();
  10. headStyle.Alignment = HorizontalAlignment.Center;
  11. headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
  12. headStyle.FillPattern = FillPattern.SolidForeground;
  13. IFont font = workbook.CreateFont();
  14. font.FontName = "Microsoft Yahei";
  15. font.FontHeightInPoints = ;
  16. font.IsBold = true;
  17. font.Color = HSSFColor.White.Index;
  18. headStyle.SetFont(font);
  19. headStyle.BorderBottom = BorderStyle.Thin;
  20. headStyle.BorderRight = BorderStyle.Thin;
  21. headStyle.BorderLeft = BorderStyle.Thin;
  22.  
  23. ICellStyle dataRowEvenStyle = workbook.CreateCellStyle();
  24. dataRowEvenStyle.Alignment = HorizontalAlignment.Center;
  25. dataRowEvenStyle.FillForegroundColor = HSSFColor.LightOrange.Index;
  26. dataRowEvenStyle.FillPattern = FillPattern.SolidForeground;
  27. IFont dataRowEvenFont = workbook.CreateFont();
  28. dataRowEvenFont.FontName = "Microsoft Yahei";
  29. dataRowEvenFont.FontHeightInPoints = ;
  30. dataRowEvenFont.Color = HSSFColor.Blue.Index;
  31. dataRowEvenStyle.SetFont(dataRowEvenFont);
  32. dataRowEvenStyle.BorderBottom = BorderStyle.Thin;
  33. dataRowEvenStyle.BorderRight = BorderStyle.Thin;
  34. dataRowEvenStyle.BorderLeft = BorderStyle.Thin;
  35.  
  36. ICellStyle dataRowOddStyle = workbook.CreateCellStyle();
  37. dataRowOddStyle.Alignment = HorizontalAlignment.Center;
  38. dataRowOddStyle.FillForegroundColor = HSSFColor.LightGreen.Index;
  39. dataRowOddStyle.FillPattern = FillPattern.SolidForeground;
  40. IFont dataRowOddFont = workbook.CreateFont();
  41. dataRowOddFont.FontName = "Microsoft Yahei";
  42. dataRowOddFont.FontHeightInPoints = ;
  43. dataRowOddFont.Color = HSSFColor.Black.Index;
  44. dataRowOddStyle.SetFont(dataRowOddFont);
  45. dataRowOddStyle.BorderBottom = BorderStyle.Thin;
  46. dataRowOddStyle.BorderRight = BorderStyle.Thin;
  47. dataRowOddStyle.BorderLeft = BorderStyle.Thin;
  48.  
  49. foreach (DataColumn column in table.Columns)
  50. {
  51. ICell cell = headerRow.CreateCell(column.Ordinal);
  52. cell.SetCellValue(string.Format(" {0} ", column.Caption));
  53. cell.CellStyle = headStyle;
  54. }
  55.  
  56. for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
  57. {
  58. IRow dataRow = sheet.CreateRow(rowIndex + );
  59. foreach (DataColumn column in table.Columns)
  60. {
  61. ICell cell = dataRow.CreateCell(column.Ordinal);
  62. cell.SetCellValue(table.Rows[rowIndex][column].ToString());
  63. if (rowIndex % == )
  64. {
  65. cell.CellStyle = dataRowEvenStyle;
  66. }
  67. else
  68. {
  69. cell.CellStyle = dataRowOddStyle;
  70. }
  71. }
  72. }
  73.  
  74. workbook.Write(ms);
  75. ms.Close();
  76.  
  77. return ms;
  78. }

转换Excel文件内容如下:

Excel文件合并单元格

代码:

  1. public static MemoryStream RenderDataTableToExcelMergedRegion(DataTable table)
  2. {
  3. MemoryStream ms = new MemoryStream();
  4.  
  5. IWorkbook workbook = new HSSFWorkbook();
  6. ISheet sheet = workbook.CreateSheet(table.TableName);
  7. IRow headerRow = sheet.CreateRow();
  8.  
  9. ICellStyle headStyle = workbook.CreateCellStyle();
  10. headStyle.Alignment = HorizontalAlignment.Center;
  11. headStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
  12. headStyle.FillPattern = FillPattern.SolidForeground;
  13. IFont font = workbook.CreateFont();
  14. font.FontName = "Microsoft Yahei";
  15. font.FontHeightInPoints = ;
  16. font.IsBold = true;
  17. font.Color = HSSFColor.White.Index;
  18. headStyle.SetFont(font);
  19. headStyle.BorderBottom = BorderStyle.Thin;
  20. headStyle.BorderRight = BorderStyle.Thin;
  21. headStyle.BorderLeft = BorderStyle.Thin;
  22.  
  23. ICellStyle dataRowEvenStyle = workbook.CreateCellStyle();
  24. dataRowEvenStyle.Alignment = HorizontalAlignment.Center;
  25. dataRowEvenStyle.FillForegroundColor = HSSFColor.LightOrange.Index;
  26. dataRowEvenStyle.FillPattern = FillPattern.SolidForeground;
  27. IFont dataRowEvenFont = workbook.CreateFont();
  28. dataRowEvenFont.FontName = "Microsoft Yahei";
  29. dataRowEvenFont.FontHeightInPoints = ;
  30. dataRowEvenFont.Color = HSSFColor.Blue.Index;
  31. dataRowEvenStyle.SetFont(dataRowEvenFont);
  32. dataRowEvenStyle.BorderBottom = BorderStyle.Thin;
  33. dataRowEvenStyle.BorderRight = BorderStyle.Thin;
  34. dataRowEvenStyle.BorderLeft = BorderStyle.Thin;
  35.  
  36. ICellStyle dataRowOddStyle = workbook.CreateCellStyle();
  37. dataRowOddStyle.Alignment = HorizontalAlignment.Center;
  38. dataRowOddStyle.FillForegroundColor = HSSFColor.LightGreen.Index;
  39. dataRowOddStyle.FillPattern = FillPattern.SolidForeground;
  40. IFont dataRowOddFont = workbook.CreateFont();
  41. dataRowOddFont.FontName = "Microsoft Yahei";
  42. dataRowOddFont.FontHeightInPoints = ;
  43. dataRowOddFont.Color = HSSFColor.Black.Index;
  44. dataRowOddStyle.SetFont(dataRowOddFont);
  45. dataRowOddStyle.BorderBottom = BorderStyle.Thin;
  46. dataRowOddStyle.BorderRight = BorderStyle.Thin;
  47. dataRowOddStyle.BorderLeft = BorderStyle.Thin;
  48.  
  49. foreach (DataColumn column in table.Columns)
  50. {
  51. ICell cell = headerRow.CreateCell(column.Ordinal);
  52. cell.SetCellValue(string.Format(" {0} ", column.Caption));
  53. cell.CellStyle = headStyle;
  54. }
  55.  
  56. for (int rowIndex = ; rowIndex < table.Rows.Count; rowIndex++)
  57. {
  58. IRow dataRow = sheet.CreateRow(rowIndex + );
  59. foreach (DataColumn column in table.Columns)
  60. {
  61. ICell cell = dataRow.CreateCell(column.Ordinal);
  62. cell.SetCellValue(table.Rows[rowIndex][column].ToString());
  63. if (rowIndex % == )
  64. {
  65. cell.CellStyle = dataRowEvenStyle;
  66. }
  67. else
  68. {
  69. cell.CellStyle = dataRowOddStyle;
  70. }
  71. }
  72. }
  73.  
  74. sheet.AddMergedRegion(new CellRangeAddress(, , , ));
  75. sheet.AddMergedRegion(new CellRangeAddress(, , , ));
  76. sheet.AddMergedRegion(new CellRangeAddress(, , , ));
  77. sheet.AddMergedRegion(new CellRangeAddress(, , , ));
  78. sheet.AddMergedRegion(new CellRangeAddress(, , , ));
  79.  
  80. workbook.Write(ms);
  81. ms.Close();
  82.  
  83. return ms;
  84. }

转换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. poj 1961 Period

    Period http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K       Description Fo ...

  2. 动态规划:树形DP-景点中心(树的带权重心)

    话说宁波市的中小学生在镇海中学参加计算机程序设计比赛,比赛之余,他们在镇海中学的各个景点参观.镇海中学共有n个景点,每个景点均有若干学生正在参 观.这n个景点以自然数1至n编号,每两个景点的编号均不同 ...

  3. 超酷JQuery动画分页按钮,鼠标悬停滑动展开

    1.效果及功能说明 animate动画分页按钮制作鼠标悬停分页按钮上滑动展开分页按钮,鼠标离开后分页按钮收缩 2.实现原理 主要是靠动画方法,来让原本的箭头图像的长度发生变长,正好可以融入下标题的文字 ...

  4. 在EF6.0中打印数据库操作日志

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. C++传递二维数字给一个自定义函数

    如果参数是多维数组,那么参数必须指明第一维意外得所有未得长度:比如你的 void tt(char a[][20])或者 void tt(char (*a)[20]) 另外这样也是可以的char *a[ ...

  6. 18、Random类简介

    Random类概述 Random类在java.util包下,使用这个类可以生成随机数 package com.sutaoyu.usually_class; import java.util.Rando ...

  7. base64 与字符串互转

    #region 将Base64编码的文本转换成普通文本 /// <summary> /// 将Base64编码的文本转换成普通文本 /// </summary> /// < ...

  8. 关于 jQuery 中的 $.data() 方法和 jQuery 对象上的data 方法

    参见文章:http://www.it165.net/pro/html/201404/11922.html

  9. sed实例收集

    url:http://blog.csdn.net/hepeng597/article/details/7852468 一.元字符集    1)^锚定行的开始 如:/^sed/匹配所有以sed开头的行. ...

  10. shell函数-页面跳转练习->

    实现思维导图-> 实现思路-> 分析:1:先把三个页面的流程作为函数先写下来,定义在脚本的开头,方便下面的调用.2:先从一个流 程开始做,其他的流程类似,比如nginx3:整体实现思路是 ...