protected void Page_Load(object sender, EventArgs e) { try { ReportDocument RepDocLiq = new ReportDocument(); RepDocLiq = (ReportDocument)(Session["Reporte"]); System.IO.MemoryStream m_stream = new System.IO.MemoryStream(); ExportOptions expOptions = RepDocLiq.ExportOptions; expOptions.ExportFormatType = ExportFormatType.PortableDocFormat; PageMargins mar = new PageMargins(1, 1, 1, 1); RepDocLiq.FormatEngine.PrintOptions.ApplyPageMargins(mar); RepDocLiq.FormatEngine.PrintOptions.PaperSize = PaperSize.PaperLetter; ExportRequestContext req = new ExportRequestContext(); req.ExportInfo = expOptions; // Get the export stream m_stream = (System.IO.MemoryStream)RepDocLiq.FormatEngine.ExportToStream(req); m_stream.Position = 0; Response.ContentType = "application/pdf"; Response.BinaryWrite(m_stream.ToArray()); m_stream.Dispose(); m_stream.Close(); RepDocLiq.Dispose(); RepDocLiq.Close(); RepDocLiq = null; ((ReportDocument)(Session["Reporte"])).Close(); ((ReportDocument)(Session["Reporte"])).Dispose(); Session["Reporte"] = null; Session["dsLiquidacion"] = null; Response.End(); } catch (Exception ex) { } }
private void ExportToStream(object oReport, string sFileName, StringBuilder sb, exportTo eExport) { System.IO.Stream oStream = null; StreamWriter sw = null; ExportOptions oOptions = new ExportOptions(); ExportRequestContext oRequest = new ExportRequestContext(); Response.Buffer = true; Response.ClearHeaders(); Response.ClearContent(); if (eExport == exportTo.TXT) { oStream = new MemoryStream(); sw = new StreamWriter(oStream, System.Text.Encoding.Default); sw.WriteLine(sb); sw.Flush(); oStream.Seek(0, SeekOrigin.Begin); Response.ContentType = "application/vnd.ms-excel"; } else if (eExport == exportTo.XLS) { oOptions.ExportFormatType = ExportFormatType.Excel; oOptions.FormatOptions = new ExcelFormatOptions(); oRequest.ExportInfo = oOptions; oStream = ((ReportDocument)oReport).ExportToStream(ExportFormatType.Excel); Response.ContentType = "application/vnd.ms-excel"; } else if (eExport == exportTo.PDF) { oOptions.ExportFormatType = ExportFormatType.PortableDocFormat; oOptions.FormatOptions = new PdfRtfWordFormatOptions(); oRequest.ExportInfo = oOptions; oStream = ((ReportDocument)oReport).ExportToStream(ExportFormatType.PortableDocFormat); Response.ContentType = "application/pdf"; } else if (eExport == exportTo.DOC) { oOptions.ExportFormatType = ExportFormatType.WordForWindows; oOptions.FormatOptions = new PdfRtfWordFormatOptions(); oRequest.ExportInfo = oOptions; oStream = ((ReportDocument)oReport).ExportToStream(ExportFormatType.WordForWindows); Response.ContentType = "application/msword"; } Response.AppendHeader("Content-Disposition", "Attachment; Filename=\"" + sFileName.Replace(" ", "_") + "\""); byte[] streamByte = new byte[oStream.Length]; oStream.Read(streamByte, 0, (int)oStream.Length); Response.BinaryWrite(streamByte); Response.End(); if (oStream != null) { oStream.Close(); } if (sw != null) { sw.Close(); } }
protected void Page_Load(object sender, EventArgs e) { ReportDocument rpt = new ReportDocument(); string type = null; try { type = Request.QueryString["type"].ToString(); } catch { } try{ if (type != null) switch (type.ToLower()) { case "narrativeplan": rpt.Load(Server.MapPath("rpt_narrative.rpt")); rpt.SetParameterValue("@key_local_plan_narrative_id", Convert.ToInt32(Request.QueryString["Keyid"].ToString())); //rpt.SetParameterValue("@p_key_local_plan_id", "-1"); rpt.SetDatabaseLogon(WebConfigurationManager.AppSettings["CrystalUserName"].ToString(), WebConfigurationManager.AppSettings["CrystalUserPassWord"].ToString()); //rpt.SetDatabaseLogon("sa", "666ming"); break; case "rpt_multi_narratives": rpt.Load(Server.MapPath("rpt_multi_narratives.rpt")); //rpt.SetParameterValue("@key_local_plan_narrative_id", Convert.ToInt32(Request.QueryString["rcid"].ToString())); rpt.SetParameterValue("@p_key_local_plan_id", Convert.ToInt32(Request.QueryString["PlanId"].ToString())); rpt.SetDatabaseLogon(WebConfigurationManager.AppSettings["CrystalUserName"].ToString(), WebConfigurationManager.AppSettings["CrystalUserPassWord"].ToString()); // rpt.SetDatabaseLogon("sa", "666ming"); break; default: break; } ExportOptions exp = new ExportOptions(); //exp.ExportFormatType = ExportFormatType.PortableDocFormat; //exp.FormatOptions = new PdfRtfWordFormatOptions(); // DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions(); //diskOpts.DiskFileName = AppDomain.CurrentDomain.BaseDirectory.Trim() + ".pdf"; //exp.ExportDestinationOptions = diskOpts; //rpt.Export(exp); //CrystalReportViewer1.ReportSource = rpt; // Export to PDF exp.ExportFormatType = ExportFormatType.PortableDocFormat; exp.FormatOptions = new PdfRtfWordFormatOptions(); ExportRequestContext req = new ExportRequestContext(); req.ExportInfo = exp; System.IO.Stream st; //rpt.FormatEngine.PrintOptions.PaperSize = PaperSize.PaperA4; //rpt.FormatEngine.PrintOptions.PaperOrientation = PaperOrientation.Portrait; st = rpt.FormatEngine.ExportToStream(req); Response.ClearHeaders(); Response.ClearContent(); Response.Buffer = true; Response.ContentType = "application/pdf"; byte[] b = new byte[st.Length + 1]; st.Read(b, 0, Convert.ToInt32(st.Length)); Response.BinaryWrite(b); Response.Flush(); Response.End(); } catch (Exception ex) { Response.Write(ex); } }