当前位置:网站首页>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
边栏推荐
- STM32G0 USB DFU 升级校验出错-2
- 2022软件工程期末考试 回忆版
- 为什么要做企业固定资产管理系统,企业如何加强固定资产管理
- 拦截器与过滤器的区别
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- 以太网PHY层芯片LAN8720A简介
- 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
- 2022.7.1-----leetcode.241
- R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
猜你喜欢

Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5

电商系统中常见的 9 大坑,你踩过没?

新手必看,点击两个按钮切换至不同的内容

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

【测试开发】软件测试—概念篇

论文导读 | 机器学习在数据库基数估计中的应用

【JVM调优实战100例】01——JVM的介绍与程序计数器

9D电影是怎样的?(+维度空间常识)

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

Excel查找一列中的相同值,删除该行或替换为空值
随机推荐
[test development] software testing - concept
Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
Progress-进度条
Page title component
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Use MNIST in tensorflow 2_ 784 data set for handwritten digit recognition
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
仿京东放大镜效果(pink老师版)
Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
STM32G0 USB DFU 升级校验出错-2
ORA-01455: converting column overflows integer datatype
R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
Masa framework - DDD design (1)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format