ASP 最简单导出excel的方法

这段代码展示了如何使用ASP.NET将数据导出为Excel文件。通过创建HTML表格并利用Response对象设置相应头信息,实现了从网页直接下载带有网格线的Excel文件。主要涉及到Response.Clear(), Response.ContentType, Content-Disposition等HTTP响应设置。" 126635246,12947110,现代数据栈中的数据建模实践与工具解析,"['云原生', '数据库', '现代数据栈', '数据建模']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

protected void lbt_Export_Click(object sender, EventArgs e)
    {
        Export("application/excel", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");

    }


 private void Export(string FileType, string FileName)
    {
        StringBuilder htmltable = new StringBuilder();
        htmltable.Append("<table border='1' cellspacing='0' cellpadding='0'>");//添加黑色边框 (选用)


        htmltable.AppendFormat("<tr><td rowspan='2' style='width: 100px;'>" + Label5.Text + "</td><td align='center' style='width: 300px;' colspan='3'>" + Label6.Text + "</td></tr><tr><td style='width: 100px;'>市台</td><td style='width: 100px;'>中央台</td><td style='width: 100px;'>省台</td></tr><tr><td style='width: 100px;'>平均</td><td align='left'>" + Label7.Text + "</td><td align='left'>" + Label8.Text + "</td><td align='left'>" + Label9.Text + "</td></tr>");//添加表头


        foreach (RepeaterItem item in rpData_Result.Items)  //遍历表格
        {
            Label Label1 = (Label)item.FindControl("Label1");
            Label Label2 = (Label)item.FindControl("Label2");
            Label Label3 = (Label)item.FindControl("Label3");
            Label Label4 = (Label)item.FindControl("Label4");
            htmltable.AppendFormat("<tr><td align=\"left\">{0}</td><td align=\"left\">{1}</td><td align=\"left\">{2}</td><td align=\"left\">{3}</td></tr>", Label1.Text, Label2.Text, Label3.Text, Label4.Text);
        }
        htmltable.Append("</table>");
        Response.Clear();
        Response.BufferOutput = true;
        Response.Charset = "utf-8";
        //Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        Response.Write(AddExcelHead());//显示网格线(选用)
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        string content = htmltable.ToString();
        Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><head></head><body lang=ZH-CN>" + content);
        Response.End();
    }


 public static string AddExcelHead()//显示网格线(选用)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
        sb.Append(" <head>");
        sb.Append(" <!--[if gte mso 9]><xml>");
        sb.Append("<x:ExcelWorkbook>");
        sb.Append("<x:ExcelWorksheets>");
        sb.Append("<x:ExcelWorksheet>");
        sb.Append("<x:Name></x:Name>");
        sb.Append("<x:WorksheetOptions>");
        sb.Append("<x:Print>");
        sb.Append("<x:ValidPrinterInfo />");
        sb.Append(" </x:Print>");
        sb.Append("</x:WorksheetOptions>");
        sb.Append("</x:ExcelWorksheet>");
        sb.Append("</x:ExcelWorksheets>");
        sb.Append("</x:ExcelWorkbook>");
        sb.Append("</xml>");
        sb.Append("<![endif]-->");
        sb.Append(" </head>");
        sb.Append("<body>");
        return sb.ToString();


    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值