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