ASP 最简单导出excel的方法

来源:互联网 发布:linux 用户提权 编辑:程序博客网 时间:2024/05/17 18:41
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();


    }