最近做报表功能,用到了.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); } }