首先需要下载一个NPOI.dll  
下载地址:http://download.csdn.net/detail/president810/9503038 using System;
using System.Collections.Generic;
using System.IO;
//引用NPOI
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace Sola.ExportTakeClass
{
public class ExportToExcelUtil
{
private string _excelPath = "";
private IWorkbook workbook;
private ISheet sheet;
private int row = ;
private IList<string> sheetList = new List<string>(); public ExportToExcelUtil(string excelPath,IList<string> sheetList)
{
_excelPath = excelPath;
this.sheetList = sheetList;
ExcelFormat();
}
/// <summary>
/// 遍历sheet 并添加到excel
/// </summary>
private void ExcelFormat()
{
try
{
workbook = new HSSFWorkbook();
foreach (var sheetname in sheetList)
{
workbook.CreateSheet(sheetname);
} }
catch
{
GC.Collect();
GC.Collect();
}
}
/// <summary>
/// 将头部列表添加至 指定的sheet页
/// </summary>
/// <param name="titleList"></param>
/// <param name="sheetName"></param>
public void SetHead(IList<string> titleList,string sheetName)
{
row = ;
sheet = workbook.GetSheet(sheetName);
IRow headerRow = sheet.CreateRow(row); int col = ;
foreach (string t in titleList)
{
headerRow.CreateCell(col).SetCellValue(t);
col++;
}
}
/// <summary>
/// 将数据写入指定的 sheet页
/// </summary>
/// <param name="valueList"></param>
/// <param name="sheetName"></param>
public void WriteExcel(IList<string> valueList, string sheetName)
{
int col = ;
sheet = workbook.GetSheet(sheetName);
row++;
IRow itemRow = sheet.CreateRow(row);
foreach (string v in valueList)
{
itemRow.CreateCell(col).SetCellValue(v);
col++;
}
} public void Close()
{
try
{
FileStream file = new FileStream(_excelPath, FileMode.Create);
workbook.Write(file);
file.Close();
workbook.Clear();
}
finally
{
GC.Collect();
GC.Collect();
}
}
}
}

最新文章

  1. phpcms V9 内容模型管理
  2. 瀑布流 &amp;留言板
  3. 精通JavaScript的this关键字
  4. iptables命令详解和举例
  5. C#获得和发送网站Session
  6. javascript 基础2第12节
  7. Docker终极指南:为什么Docker能做这么多事
  8. Bootstrap--下拉菜单.dropdown
  9. SpringBoot是什么,可以做什么?
  10. AJAX问题 XMLHttpRequest.status = 0是什么含义
  11. Windows10 VS2017 C++ Json解析(使用jsoncpp库)
  12. ros自定义消息的时候报错ImportError: No module named em
  13. Kafka的安装和设置
  14. Readability Assessment for Text Simplification -paper
  15. 图片训练:使用卷积神经网络(CNN)识别手写数字
  16. XML和JSON优缺点
  17. JIRA python篇之展示多人未完成任务列表
  18. eclipse异常关闭,而Tomcat然在运行解决方法
  19. Linux-- 查看文件 cat/tac
  20. OrhtoMCL 使用方法

热门文章

  1. .net HTMLParser详细使用说明 强大的Filter类 解析HTML文档如此简单
  2. 分布式缓存BeIT Memcached简介
  3. CodeBlocks+opencv2.4.4+cmake+MinGW
  4. window上使用GIT的个人经验(入门级)
  5. jsf服务器端验证用户输入
  6. Storm系列(十)聚流示例
  7. HW5.8
  8. HDU1671 - Phone List(Trie树)
  9. Android:从程序员到架构师之路Ⅰ
  10. DataTable 导出到 Excel 类