Excel操作之 导出生成多个sheet页面
2024-10-11 06:34:43
首先需要下载一个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();
}
}
}
}
最新文章
- phpcms V9 内容模型管理
- 瀑布流 &;留言板
- 精通JavaScript的this关键字
- iptables命令详解和举例
- C#获得和发送网站Session
- javascript 基础2第12节
- Docker终极指南:为什么Docker能做这么多事
- Bootstrap--下拉菜单.dropdown
- SpringBoot是什么,可以做什么?
- AJAX问题 XMLHttpRequest.status = 0是什么含义
- Windows10 VS2017 C++ Json解析(使用jsoncpp库)
- ros自定义消息的时候报错ImportError: No module named em
- Kafka的安装和设置
- Readability Assessment for Text Simplification -paper
- 图片训练:使用卷积神经网络(CNN)识别手写数字
- XML和JSON优缺点
- JIRA python篇之展示多人未完成任务列表
- eclipse异常关闭,而Tomcat然在运行解决方法
- Linux-- 查看文件 cat/tac
- OrhtoMCL 使用方法