c#將ReportViewer數據導出到Excel

// 下載明細
//下載按鈕點擊事件

    protected void downfile_Click(object sender, EventArgs e)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string extension;
        try
        {
            //ReportViewer1 控件名稱
            byte[] bytes = ReportViewer1.LocalReport.Render(
           "Excel", null, out mimeType, out encoding, out extension,
           out streamids, out warnings);
            Response.OutputStream.Write(bytes, 0, bytes.Length);
            //filename  導出文件名稱可自定義
            Response.AppendHeader("Content-Disposition", "attachment; filename=Output.xls");
            Response.ContentType = mimeType;
            Response.Flush();
            Response.End();
        }
        catch (Exception)
        {
            Response.Write("<script>alert('導出失敗,請確認!')</script>");
        }      
    }`
posted @ 2022-05-26 18:16  暮夜秋雨  阅读(127)  评论(0)    收藏  举报