RDLC - 后台代碼直接導出Excel/PDF/Word格式


最近做報表功能,用到了.net的報表組件rdlc。

其中有個功能就是后台代碼直接輸出Excel/PDF/Word格式的文件,
網上看了些資源,做個總結:

參考地址

我直接貼出代碼:

//自動導出excel/pdf/word
        private void ResponseFile(int oType, string fileName)
        {
            string outType;
            if (oType == 0)
            {
                outType = "Excel";
            }
            else if (oType == 1)
            {
                outType = "Word";
            }
            else
            {
                outType = "Word"; 
            }
            try
            {
                Warning[] warnings;
                string[] streamids;

                string mimeType;
                string encoding;
                string extension;


                byte[] bytes = ReportViewer1.LocalReport.Render(
                        outType, null, out mimeType, out encoding, out extension,
                        out streamids, out warnings);
                    Response.Clear();
                    Response.Buffer = true;
                    Response.ContentType = mimeType;
                    Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "." + extension);
                    Response.BinaryWrite(bytes);

                    Response.Flush();

            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM