C# 在UpdatePanel控件里下载文件Excel
参考的例子
asp.net中下载文件的问题 - 九天星辰 - 博客园 (cnblogs.com)
例子是SearchAllInspectList.aspx
1、在aspx页面加上Triggers标签
//一定要加在ContentTemplate和asp:UpdatePanel之间 //btnExport就是一个button按钮,点击btnExport就下载excel </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="btnExport" /> </Triggers> </asp:UpdatePanel>
//这是btnExport_Click事件里的代码 ExportToExcel12(dtReInpect); // ExportToExcel(gvSearchResult); ScriptManager scriptManager = (ScriptManager)((Page)HttpContext.Current.Handler).FindControl("ScriptManager1");//获取父页面ScriptManager1控件 scriptManager.RegisterPostBackControl((System.Web.UI.Control)sender); #region//导出模板 //服务器的路径地址文件夹,在根目录对应的文件相关的FileTempletModel下;如本系统的生成的文件在(根目录-MenuWFrm-JCSZ)下的FileTempletModel里 string savePath = HttpContext.Current.Server.MapPath("Excel"); if (!System.IO.Directory.Exists(savePath)) {//如果没有文件夹,则建立 System.IO.Directory.CreateDirectory(savePath); } savePath = savePath + "\\IPQCSearchList.xls";//服务器上的文件名称:文件夹路径 + 模板名称 + 后缀名(.xml) dtReInpect.WriteXml(savePath); // dsReInpect.WriteXml(savePath);//数据导出到文件中(服务器上) GC.Collect(); string filePath = savePath;//+ ".xml";//服务器上的文件名称,用于从服务器下载到客户端上 if (System.IO.File.Exists(filePath)) {//将服务器上的文件下载到客户端上 System.IO.FileInfo file = new System.IO.FileInfo(filePath); Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码 Response.AddHeader("Content-length", file.Length.ToString());//下载时显示进度条 Response.ContentType = "appliction/octet-stream"; //Response.ContentType = "application/x-zip-compressed"; //Response.WriteFile(file.FullName); /* 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 代码如下: */ Response.TransmitFile(filePath, 0, file.Length); //这个路径是在根目录下 Response.Flush(); // Response.End(); HttpContext.Current.ApplicationInstance.CompleteRequest();
ExportToExcel12方法
private void ExportToExcel12(DataTable dt) { string fileName = "IPQCSearchList"; IWorkbook workbook = null; //保存的数据 FileStream fs = null; IRow row = null; ISheet sheet = null; //生成表格 ICell cell = null; if (dt != null && dt.Rows.Count > 0) { workbook = new HSSFWorkbook(); sheet = workbook.CreateSheet("Sheet0");//创建一个名称为Sheet0的表 int rowCount = dt.Rows.Count;//行数 int columnCount = dt.Columns.Count;//列数 //设置列头 row = sheet.CreateRow(0);//excel第一行设为列头,在第一行中写入数据(所有列的第一行) for (int c = 0; c < columnCount; c++) { cell = row.CreateCell(c, CellType.Numeric); //创建一个单元格,保留文本格式 cell.SetCellValue(dt.Columns[c].ColumnName); //将数据写入到单元格中 } //设置每行每列的单元格, for (int i = 0; i < rowCount; i++) //写完所有的行和列 { row = sheet.CreateRow(i + 1); //这里是新开启一行,从第二行开始写,第一行上面已经写完 for (int j = 0; j < columnCount; j++) { cell = row.CreateCell(j);//excel第二行开始写入数据,一列一列的分别创建单元格 cell.SetCellValue(dt.Rows[i][j].ToString()); //创建列的单元格的内容 } } string TempletFileName = Server.MapPath("Excel/").Replace("\\Form", "") + "IPQCSearchList" + ".xls"; using (fs = File.OpenWrite(TempletFileName)) { workbook.Write(fs);//向打开的这个xls文件中写入数据 } } }
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~