当前位置:网站首页>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
边栏推荐
- Introduction to the paper | application of machine learning in database cardinality estimation
- 高频面试题
- 2022编译原理期末考试 回忆版
- Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
- PHP-Parser羽毛球预约小程序开发require线上系统
- 教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
- Learn the knowledge points of eight part essay ~ ~ 1
- R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
- Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
- Excel查找一列中的相同值,删除该行或替换为空值
猜你喜欢
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Processing strategy of message queue message loss and repeated message sending
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
【测试开发】软件测试—概念篇
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
[test development] software testing - concept
论文导读 | 机器学习在数据库基数估计中的应用
Excel查找一列中的相同值,删除该行或替换为空值
随机推荐
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Tips for material UV masking
日期工具类(不定时更新)
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
高级性能测试系列《24. 通过jdbc执行sql脚本》
Learn the knowledge points of eight part essay ~ ~ 1
[daily question] the next day
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
医院在线问诊源码 医院视频问诊源码 医院小程序源码
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
Excel如何进行隔行复制粘贴
论文导读 | 机器学习在数据库基数估计中的应用
juypter notebook 修改默认打开文件夹以及默认浏览器
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
High frequency interview questions
全志A33使用主线U-Boot
yolov3 训练自己的数据集之生成train.txt
移动机器人路径规划:人工势场法[通俗易懂]