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

推荐订阅源

量子位
T
The Blog of Author Tim Ferriss
U
Unit 42
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
Y
Y Combinator Blog
F
Full Disclosure
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
S
SegmentFault 最新的问题
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
T
Threatpost
GbyAI
GbyAI
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
L
LangChain Blog
T
Tenable Blog
C
Cisco Blogs
T
Threat Research - Cisco Blogs
Google Online Security Blog
Google Online Security Blog

博客园 - 五蕴非空

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){} //不需要添加内容