Excel操作之 导出生成多个sheet页面
2016-04-26 14:19
295 查看
首先需要下载一个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 = 0; 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(1); } } /// <summary> /// 将头部列表添加至 指定的sheet页 /// </summary> /// <param name="titleList"></param> /// <param name="sheetName"></param> public void SetHead(IList<string> titleList,string sheetName) { row = 0; sheet = workbook.GetSheet(sheetName); IRow headerRow = sheet.CreateRow(row); int col = 0; 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 = 0; 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); } } } }
相关文章推荐
- 支付宝集成获取私钥与公钥
- 五种排序的思想及其基本实现
- Android笔记:Activity的生命周期以及启动模式
- 人生如梦
- Java IO:BufferedInputStream使用详解及源码分析
- 在Linux中使用unzip打开zip文件失败
- 常见的各个平台下面的内存检查工具或者插件
- 矩阵、向量求导法则
- UNPv1第二十三章:线程
- VS设置使用UNICODE
- Android 监听电话---在多页面同时使用不产生影响的解决方法
- extern "C" c++如何实现函数的重载
- jQuery常用方法1
- valgrind
- hadoop2.x常用端口、定义方法及默认端口、hadoop1.X端口对比
- Android多渠道打包
- GCD 之线程死锁
- Duplication with Oracle Managed Files(OMF)
- POJ-3114 Countries in War (强连通分量[Tarjan]&&Dijkstra)
- TortoiseGit 使用教程