当前位置:网站首页>NPOI导出Excel并下载到客户端
NPOI导出Excel并下载到客户端
2022-06-28 13:36:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
报表数据导出Excel,很常见的需求,然而每次都能忘了,今天再次遇到了,总结一下。
一般来说都需要有个标题
//需要输出的字段
string[] fieldArr = { "Title", "Sequence", "WorkSheetDescription", "Position", "StartTime", "EndTime", "Transactor", "Status", "WorkClass", "Result", "StartDoTime", "EndDoTime" };
//对应的中文描述 System.Collections.Generic.Dictionary<string, string> fieldDic = new System.Collections.Generic.Dictionary<string, string> { { "Title", "工单编号" }, { "Sequence", "顺序" }, { "WorkSheetDescription", "工单描述" }, { "Position", "位置" }, { "StartTime", "计划开始时间" }, { "EndTime", "计划结束时间" }, { "Transactor", "处理人" }, { "Status", "工单状态" }, { "WorkClass", "班次" }, { "Result", "执行结果" }, { "StartDoTime", "实际开始时间" }, { "EndDoTime", "实际结束时间" } };设置表单表格样式
ICellStyle titleStyle= wb.CreateCellStyle();
IFont font1 = wb.CreateFont();//字体
font1.FontName = "楷体";
font1.Color = HSSFColor.White.Index;//字体颜色
font1.Boldweight = (short)FontBoldWeight.Normal;//字体加粗
titleStyle.SetFont(font1);//字体设置具体的字体
//背景色
titleStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Blue.Index;
titleStyle.FillPattern = FillPattern.SolidForeground;
titleStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Blue.Index;
titleStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//文字水平对齐方式
titleStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式设置列宽
int[] columnWidth = { 10, 10, 25, 10, 15, 15, 20, 10, 20, 20, 15, 15 };
for (int i = 0; i < columnWidth.Length; i++)
{ //设置列宽度,256*字符数,因为单位是1/256个字符 sheet.SetColumnWidth(i, 256 * columnWidth[i]); }把生成的Excel发送到客户端
MemoryStream ms = new MemoryStream();
wb.Write(ms);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode("WS" + "_" + DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8)));
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
wb = null;
ms.Close();
ms.Dispose();因为我的项目是SharePoint的,所以数据源是SPListItemCollection传入,其他的DataTable或者其他的相应改动一下就行。
代码如下
public void NPOIExcel(SPListItemCollection items)
{
HSSFWorkbook wb = new HSSFWorkbook();
//标题样式样式
ICellStyle titleStyle= wb.CreateCellStyle();
IFont font1 = wb.CreateFont();//字体
font1.FontName = "楷体";
font1.Color = HSSFColor.White.Index;//字体颜色
font1.Boldweight = (short)FontBoldWeight.Normal;//字体加粗
titleStyle.SetFont(font1);//设置字体
//设置背景色
titleStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Blue.Index;
titleStyle.FillPattern = FillPattern.SolidForeground;
titleStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Blue.Index;
titleStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//文字水平对齐方式
titleStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式
//创建一个表单
ISheet sheet = wb.CreateSheet("Sheet0");
//设置列宽
int[] columnWidth = { 10, 10, 25, 10, 15, 15, 20, 10, 20, 20, 15, 15 };
for (int i = 0; i < columnWidth.Length; i++)
{
//设置列宽度,256*字符数,因为单位是1/256个字符
sheet.SetColumnWidth(i, 256 * columnWidth[i]);
}
IRow row;
ICell cell;
string[] fieldArr = { "Title", "Sequence", "WorkSheetDescription", "Position", "StartTime", "EndTime", "Transactor", "Status", "WorkClass", "Result", "StartDoTime", "EndDoTime" };
System.Collections.Generic.Dictionary<string, string> fieldDic = new System.Collections.Generic.Dictionary<string, string> { { "Title", "工单编号" }, { "Sequence", "顺序" }, { "WorkSheetDescription", "工单描述" }, { "Position", "位置" }, { "StartTime", "计划开始时间" }, { "EndTime", "计划结束时间" }, { "Transactor", "处理人" }, { "Status", "工单状态" }, { "WorkClass", "班次" }, { "Result", "执行结果" }, { "StartDoTime", "实际开始时间" }, { "EndDoTime", "实际结束时间" } };
//写入标题行
row = sheet.CreateRow(0);
for (int i = 0; i < fieldArr.Length; i++)
{
cell = row.CreateCell(i);//创建第j列
cell.CellStyle = titleStyle;
SetCellValue(cell, fieldDic[fieldArr[i]]);
}
//写入数据,从第2行开始
for (int i = 1; i <= items.Count; i++)
{
row = sheet.CreateRow(i);//创建第i行
for (int j = 0; j < fieldArr.Length; j++)
{
cell = row.CreateCell(j);
//根据数据类型设置不同类型的cell
var field = fieldArr[j];
object obj = null;
//如果报错,跳过该字段
try
{
obj = items[i - 1][field];
}
catch
{
continue;
}
if (obj != null)
{
SetCellValue(cell, obj);
}
}
}
//发送到客户端
MemoryStream ms = new MemoryStream();
wb.Write(ms);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode("WS" + "_" + DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8)));
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
wb = null;
ms.Close();
ms.Dispose();
}
/// <summary>
/// 根据数据类型设置不同类型的cell
/// </summary>
/// <param name="cell"></param>
/// <param name="obj"></param>
public static void SetCellValue(ICell cell, object obj)
{
try
{
if (obj.GetType() == typeof(int))
{
cell.SetCellValue((int)obj);
}
else if (obj.GetType() == typeof(double))
{
cell.SetCellValue((double)obj);
}
else if (obj.GetType() == typeof(IRichTextString))
{
cell.SetCellValue((IRichTextString)obj);
}
else if (obj.GetType() == typeof(string))
{
cell.SetCellValue(obj.ToString());
}
else if (obj.GetType() == typeof(DateTime))
{
cell.SetCellValue(Convert.ToDateTime(obj).ToString("yyyy/MM/dd"));
}
else if (obj.GetType() == typeof(bool))
{
cell.SetCellValue((bool)obj);
}
else
{
cell.SetCellValue(obj.ToString());
}
}
catch (Exception ex)
{
}
}需要注意的是,如果在页面直接用按钮事件导出Excel的话,只能导出一次,然后因为页面被End了,导致页面没有刷新,如果需要多次导出的话,可以把下载的操作放到一个页面去执行,然后前端js创建iframe的方式去做。
好记忆不如烂笔头,记录一下,免得以后又要到处找。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/150604.html原文链接:https://javaforall.cn
边栏推荐
- 为什么越来越多的用户放弃 Swagger,选择Apifox
- 决策树预测红酒品质
- Fh511+tp4333 form an outdoor mobile power lighting camping lamp scheme.
- Centos7——安装mysql5.7
- thinkphp6 多级控制器目录访问解决方法
- China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
- Arduino-ESP32闪存文件插件程序搭建和上传
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
- Special test for cold and hot start of app
- 真香啊!最全的 Pycharm 常用快捷键大全!
猜你喜欢

Fh511+tp4333 form an outdoor mobile power lighting camping lamp scheme.

Google Earth engine (GEE) - Global organic soil area of FAO (1992-2018)

How vscade sets auto save code

Luogu_ P1303 A*B Problem_ High precision calculation

单元测试 CI/CD

New product experience: Alibaba cloud's new generation of local SSD instance I4 open beta

新品体验:阿里云新一代本地SSD实例i4开放公测

Stackoverflow 2022 database annual survey

Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects

Arduino-ESP32闪存文件插件程序搭建和上传
随机推荐
恒生电子:金融分布式数据库LightDB通过中国信通院多项测评
Rk3399 platform development series explanation (use part) pinctrl subsystem introduction - Video Introduction
行动诠释价值,城联优品韩董事长出席广东英德抗洪捐赠公益活动会
(original) [Maui] realize "floating action button" step by step
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
决策树预测红酒品质
yii2编写swoole的websocket服务
Electronic components distribution 1billion Club [easy to understand]
Tiantian mathematics serial 53: February 22
再谈exception——异常抛出时会发生什么?
嵌入式设计与开发项目-液位检测告警系统
MySQL multi table joint query
China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
APP冷热启动专项测试
NFT digital collection system development (3D modeling economic model development case)
Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”
Class structure in C language - dot
BERT为何无法彻底干掉BM25??
为什么新的5G标准将为技术栈带来更低的 TCO