惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
爱范儿
爱范儿
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
C
Check Point Blog
小众软件
小众软件
I
InfoQ
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
酷 壳 – CoolShell
酷 壳 – CoolShell
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
B
Blog
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
D
Docker
博客园 - 司徒正美
博客园_首页
Recent Announcements
Recent Announcements
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
GbyAI
GbyAI
Jina AI
Jina AI
V
V2EX
Vercel News
Vercel News
IT之家
IT之家
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
V
Visual Studio Blog
C
Cybersecurity and Infrastructure Security Agency CISA

博客园 - 五蕴非空

AI工具实践日记(二):在 OpenClaw 中调用 OpenCode 进行开发任务 AI工具实践日记(一):在树莓派上搭建OpenClaw,一个后端开发者的真实踩坑记录 .net core XML 解析帮助类 常用工具类 .net Core 同一接口不同实现的依赖注入 - 五蕴非空 大批量数据操作的性能优化方案 .net core 3.1 配置文件立即更新 为.net Core 3.0 WebApi 创建Linux守护进程 asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported .net Core2.2 WebApi通过OAuth2.0实现微信登录 Ext.Net 使用总结之GridPanel中的选中行 Ext.Net 使用总结之查询条件中的起始日期 Ext.Net 使用总结之GridPanel的删除事件 使用 NuGet 管理项目库 JavaScript 获取客户端计算机硬件及系统信息 程序员技术练级攻略 ThoughtWorks(中国)程序员读书雷达 Sql 分割 键值对字符串 得到某值对应的名称 Ext.net 中日期格式的计算
Asp.net导出Excel文件
五蕴非空 · 2015-03-24 · via 博客园 - 五蕴非空

参考了网上其他大神的写法,也是利用控件的RenderControl功能,得到该控件生成的HTML,然后以Excel文件的类型输出到客户端。

不同的地方是我自己添加了Response.Flush()方法,这样在生成数据的时候就已经弹出了下载框,而不必在同样的页面等待一会儿。如图:

点击导出Excel按钮后,直接弹出下载框。

注:Response.Flush()的作用是将缓冲信息输出到页面。

    /// <summary>
    /// 把DataTable内容导出excel并返回客户端 
    /// </summary>
    /// <param name="header">标题行</param>
    /// <param name="fileName">文件名称</param>
    public void DataTable2Excel(TableCell[] header, string fileName)
    {
       // IO用于导出并返回excel文件 
       var strWriter = new StringWriter();
       var htmlWriter = new HtmlTextWriter(strWriter);
       // 设置编码和附件格式 
       Response.ContentType = "application/ms-excel";
       Response.ContentEncoding = Encoding.GetEncoding("gb2312");
       Response.Charset = "gb2312";
       if (!string.IsNullOrEmpty(fileName))
       {
           fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);//处理中文名乱码问题
           Response.AppendHeader("Content-Disposition", ("attachment;filename=" + (fileName.ToLower().EndsWith(".xls") ? fileName : fileName + ".xls")));
       }
       Response.Flush();
       var gvExport = new GridView();// 重新定义一个无分页的GridView var dt = new DataTable();//这里是要导出的数据
       gvExport.DataSource = dt.DefaultView;
       gvExport.AllowPaging = false;
       gvExport.RowDataBound += dgExport_RowDataBound; //优化导出数据显示,如身份证、12-1等显示异常问题
       gvExport.DataBind();
       if (header != null && header.Length > 0)//处理表头
       {
            gvExport.HeaderRow.Cells.Clear();
            gvExport.HeaderRow.Cells.AddRange(header);
       }
       gvExport.RenderControl(htmlWriter);// 返回客户端 
       Response.Write(strWriter);
       Response.End();
    }
    /// <summary>
    /// 用来优化导出数据显示
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dgExport_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow) return;
        foreach (var cell in e.Row.Cells.Cast<TableCell>().Where(cell => Regex.IsMatch(cell.Text.Trim(), @"^\d{12,}$") || Regex.IsMatch(cell.Text.Trim(), @"^\d+[-]\d+$")))
        {
            cell.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        }
    }

总结:

1、方法调用时总会报错:Server cannot set content type after HTTP headers have been sent.

  原因:在 Response.AppendHeader 方法调用之前,先调用了Response.Flush()方法。

2、报错:必须放在具有 runat=server 的窗体标记内.

     解决办法: public override void VerifyRenderingInServerForm(Control control){} //不需要添加内容