当前位置:网站首页>NPOI导出Excel2007
NPOI导出Excel2007
2022-07-02 18:09:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
public void Export_ProjectList(DataTable dt, HttpRequestBase Request) { string strModelFile = “”; strModelFile = Request.PhysicalApplicationPath + “ProjectList.xls”;//模板地址 XSSFWorkbook workbook = null; FileStream file = new FileStream(strModelFile, FileMode.Open, FileAccess.Read); workbook = new XSSFWorkbook(file);//如果导出2003 则是HSSFWorkbook HSSFSheet sheet = (HSSFSheet)workbook.GetSheet(“ProjectList”);//可以是ISheet Sheet=null; if (sheet == null) { return; } ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; cellStyle.BorderTop = BorderStyle.Thin; cellStyle.WrapText = true; cellStyle.Alignment = HorizontalAlignment.Center; cellStyle.VerticalAlignment = VerticalAlignment.Center;
//设置详细信息 if (dt != null && dt.Rows.Count > 0) { IRow dataRowtitle = sheet.CreateRow(0); dataRowtitle.Height = 500; dataRowtitle.CreateCell(0).SetCellValue(“序号”); dataRowtitle.CreateCell(1).SetCellValue(“项目编号”); dataRowtitle.CreateCell(2).SetCellValue(“项目名称”); dataRowtitle.CreateCell(3).SetCellValue(“项目内容”); dataRowtitle.CreateCell(4).SetCellValue(“行政区”); dataRowtitle.CreateCell(5).SetCellValue(“所属分公司”); dataRowtitle.CreateCell(6).SetCellValue(“联系人”); dataRowtitle.CreateCell(7).SetCellValue(“联系人电话”);
for (int i = 0; i < dt.Rows.Count; i++) { IRow dataRow = sheet.CreateRow(i + 1); dataRow.Height = 450;
for (int j = 0; j < 8; j++) { ICell iCell = dataRow.CreateCell(j); iCell.CellStyle = cellStyle; switch (j) { case 0: iCell.SetCellValue(i + 1);//序号 break; case 1: iCell.SetCellValue(dt.Rows[i][“PROJECTCODE”] == null ? “” : String.Format(“{0:d}”, dt.Rows[i][“PROJECTCODE”]));//项目编号 break; case 2: iCell.SetCellValue(dt.Rows[i][“NAME”].ToString());//项目名称 break; case 3: iCell.SetCellValue(dt.Rows[i][“CONTENT”].ToString());//项目内容 break; case 4: iCell.SetCellValue(GetListName(GFun.SafeToInt32(dt.Rows[i][“DISTRICT”])));//行政区 break; case 5: iCell.SetCellValue(GetListName(GFun.SafeToInt32(dt.Rows[i][“BRANCH”])));//所属分公司 break; case 6: iCell.SetCellValue(dt.Rows[i][“CONTACTS”].ToString());//联系人 break; case 7: iCell.SetCellValue(dt.Rows[i][“PHONENUM”].ToString());//联系人电话 break; }
} } } string fileName = string.Empty; //sheet.GetRow(0).GetCell(0).SetCellValue(“项目信息”);//标题 SetExportFileName(fileName + “查询结果导出.xlsx”, workbook); } public static void SetExportFileName(string sFileName, XSSFWorkbook workbook) { System.Web.HttpContext.Current.Response.Charset = “GB2312”; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; sFileName = System.Web.HttpUtility.UrlEncode(sFileName, System.Text.Encoding.UTF8); // 添加头信息,为”文件下载/另存为”对话框指定默认文件名 System.Web.HttpContext.Current.Response.AddHeader(“Content-Disposition”, “attachment; filename=” + sFileName);
// 指定返回的是一个不能被客户端读取的流,必须被下载 System.Web.HttpContext.Current.Response.ContentType = “application/ms-Excel”;
// 把文件流发送到客户端 workbook.Write(System.Web.HttpContext.Current.Response.OutputStream); // 停止页面的执行 System.Web.HttpContext.Current.Response.End(); }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148598.html原文链接:https://javaforall.cn
边栏推荐
- R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
- Installation of thingsboard, an open source IOT platform
- The difference between interceptor and filter
- Singapore summer tourism strategy: play Singapore Sentosa Island in one day
- Processing strategy of message queue message loss and repeated message sending
- 论文导读 | 机器学习在数据库基数估计中的应用
- Mini Golf Course: a good place for leisure and tourism in London
- How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
- 性能测试如何创造业务价值
- R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
猜你喜欢

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

Talk about the design of red envelope activities in e-commerce system

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors

Installation of thingsboard, an open source IOT platform

The difference between interceptor and filter

全链路数字化转型下,零售企业如何打开第二增长曲线

性能测试如何创造业务价值

ICDE 2023|TKDE Poster Session(CFP)

Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent

yolov3 训练自己的数据集之生成train.txt
随机推荐
The difference between interceptor and filter
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
拦截器与过滤器的区别
Masa framework - DDD design (1)
2022 software engineering final exam recall Edition
聊聊电商系统中红包活动设计
LightGroupButton* sender = static_ cast<LightGroupButton*>(QObject::sender());
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
【JVM调优实战100例】03——JVM堆调优四例
How to enable the run dashboard function of idea
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
C file input operation
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
Emmet基础语法
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
虚拟机初始化脚本, 虚拟机相互免秘钥
在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]