For web export excel

1. Import the data collection of datatable

2. Export function

 /// <summary>   
    /// For Web export   
    /// </summary>   
    /// <param name="dtSource">Source DataTable</param>   
    /// <param name="strHeaderText">Header Text</param>   
    /// <param name="strFileName">File name</param>   
    public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
    {

        HttpContext curContext = HttpContext.Current;     
        // 设置编码和附件格式   
        curContext.Response.ContentType = "application/vnd.ms-excel";
        curContext.Response.ContentEncoding = Encoding.UTF8;
        curContext.Response.Charset = "";
        curContext.Response.AppendHeader("Content-Disposition",
            "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

        curContext.Response.BinaryWrite(Export(dtSource, strHeaderText).GetBuffer());
        curContext.Response.End();

    }

3. Call

dt is the data set that needs to be exported

ExportByWeb(dt, "excel export", "report.xls");

Guess you like

Origin blog.csdn.net/vbloveshllm/article/details/135978801