当前位置:网站首页>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
边栏推荐
- ICDE 2023|TKDE Poster Session(CFP)
- metric_ Logger urination
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
- R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
- Use cheat engine to modify money, life and stars in Kingdom rush
- Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
- Introduction to the paper | application of machine learning in database cardinality estimation
- Learn the knowledge points of eight part essay ~ ~ 1
- [test development] takes you to know what software testing is
- 在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
猜你喜欢

PHP-Parser羽毛球预约小程序开发require线上系统

M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)

全志A33使用主线U-Boot

Novice must see, click two buttons to switch to different content

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
![[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning](/img/59/6c776e0607a52962b72fbea2e64c8e.png)
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning

ICDE 2023|TKDE Poster Session(CFP)

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

高级性能测试系列《24. 通过jdbc执行sql脚本》

新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
随机推荐
Develop fixed asset management system, what voice is used to develop fixed asset management system
Yolov3 trains its own data set to generate train txt
在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
C文件输入操作
Progress-进度条
How to copy and paste interlaced in Excel
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
预处理和预处理宏
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
页面标题组件
[daily question] first day
UML class diagram
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
2022 software engineering final exam recall Edition
数字滚动带动画
Use cheat engine to modify money, life and stars in Kingdom rush
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
新手必看,点击两个按钮切换至不同的内容