导出实现代码:- public class ReportExport : IHttpHandler, IReadOnlySessionState
- {
- private string filename = string.Empty;
-
- public void ProcessRequest (HttpContext context)
- {
- var key = context.Request["key"];
- var act = context.Request["act"];
- filename = context.Request["filename"];
- if (string.IsNullOrEmpty(key))
- {
- context.Response.Write("缺少参数key");
- context.Response.End();
- }
- if (string.IsNullOrEmpty(act))
- {
- context.Response.Write("缺少参数act");
- context.Response.End();
- }
- var report = Report(key);
- switch (act)
- {
- case "PDF":
- ExportPdf(context, report);
- break;
- case "WORD":
- ExportWord(context, report);
- break;
- case "EXCEL":
- ExportExcel(context, report);
- break;
- }
- }
- private SectionReport Report(string key)
- {
- return UserGrade.GetData(CacheDataTypes.Other, key) as SectionReport;
- }
- /// <summary>
- /// PDF导出
- /// </summary>
- /// <param name="context"></param>
- /// <param name="report"></param>
- private void ExportPdf(HttpContext context,SectionReport report)
- {
-
-
-
- if (string.IsNullOrEmpty(filename))
- {
- filename = "MyPDF";
- }
- filename = AttachFileName(context, filename + ".PDF");
- context.Response.ContentType = "application/pdf";
- context.Response.Clear();
- context.Response.HeaderEncoding = System.Text.Encoding.Default;
- context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
-
-
- var pdf = new PdfExport();
- var memStream = new System.IO.MemoryStream();
- pdf.Export(report.Document, memStream);
- context.Response.BinaryWrite(memStream.ToArray());
- context.Response.Flush();
- context.Response.End();
- }
- private void ExportWord(HttpContext context, SectionReport report)
- {
- if (string.IsNullOrEmpty(filename))
- {
- filename = "MyWord";
- }
- filename = AttachFileName(context, filename + ".doc");
- context.Response.ContentType = "application/rtf";
- context.Response.Clear();
- context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
- var rtf = new RtfExport();
- var memStream = new System.IO.MemoryStream();
- rtf.Export(report.Document, memStream);
- context.Response.BinaryWrite(memStream.ToArray());
- context.Response.End();
- }
- private void ExportExcel(HttpContext context, SectionReport report)
- {
- if (string.IsNullOrEmpty(filename))
- {
- filename = "MyExcel";
- }
- filename = AttachFileName(context,filename + ".xlsx");
- context.Response.ContentType = "application/excel";
- context.Response.Clear();
- context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
- XlsExport xls = new XlsExport();
- System.IO.MemoryStream memStream = new System.IO.MemoryStream();
- xls.FileFormat = FileFormat.Xlsx;
- xls.Export(report.Document, memStream);
- context.Response.BinaryWrite(memStream.ToArray());
- context.Response.End();
- }
复制代码 |