转自:https://www.cnblogs.com/zhangzhi19861216/p/3677896.html
最近做报表功能,用到了.net的报表组件rdlc。
其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,
网上看了些资源,做个总结:
参考地址:https://forums.asp.net/t/1556522.aspx?RDLC+Export+directly+to+Excel+or+PDF+from+codebehind
我直接贴出代码:
//自动导出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); } }
作者:智
出处:http://zhangzhi19861216.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,若转载请在文章页面明显位置给出原文连接。