当前位置:网站首页>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
边栏推荐
- PHP非对称加密方法私钥及公钥加密解密的方法
- PHP-Parser羽毛球预约小程序开发require线上系统
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- Talk about the design of red envelope activities in e-commerce system
- [daily question] first day
- Emmet basic syntax
- ORA-01455: converting column overflows integer datatype
- [100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
- C的内存管理
猜你喜欢
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
新手必看,點擊兩個按鈕切換至不同的內容
codeforces每日5题(均1700)-第四天
Imitation Jingdong magnifying glass effect (pink teacher version)
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
UML class diagram
使用CLion编译OGLPG-9th-Edition源码
随机推荐
Markdown basic grammar
Processing strategy of message queue message loss and repeated message sending
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
PHP非对称加密方法私钥及公钥加密解密的方法
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
聊聊电商系统中红包活动设计
[test development] takes you to know what software testing is
PyTorch函数中的__call__和forward函数
新手必看,點擊兩個按鈕切換至不同的內容
【测试开发】软件测试—概念篇
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
高级性能测试系列《24. 通过jdbc执行sql脚本》
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
codeforces每日5题(均1700)-第四天
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
[paper reading] Ca net: leveraging contextual features for lung cancer prediction