Excel操作之 导出生成多个sheet页面

首先需要下载一个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(); } /// /// 遍历sheet 并添加到excel /// private void ExcelFormat() { try { workbook = new HSSFWorkbook(); foreach (var sheetname in sheetList) { workbook.CreateSheet(sheetname); } } catch { GC.Collect(); GC.Collect(1); } } /// /// 将头部列表添加至 指定的sheet页 /// /// /// 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++; } } /// /// 将数据写入指定的 sheet页 /// /// /// 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); } } } }

 

转载于:https://www.cnblogs.com/president/p/406bac494dab3ca04f704e27e4aed3ae.html

你可能感兴趣的:(Excel操作之 导出生成多个sheet页面)