gpt4 book ai didi

c# - 我可以从 SQL Reporting Services (.rdlc) 生成基于图像的 PDF 报告而不将图像保存在任何地方吗?

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:18 25 4
gpt4 key购买 nike

场景:

我有一个包含多个图表的网页,我有一个“导出为 PDF”按钮,用户应该可以单击该按钮,它会生成一个用户可以保存的 PDF。

给定:

可以将自身保存到内存流中的 Telerik RadChart:

MemoryStream chartStream = new MemoryStream();
RadChart1.Save(chartStream, ImageFormat.Png);

使用此内存流,是否可以使用 SQL 报告服务构建 PDF WITH OUT 首先将其保存到文件必须将其插入 MSSQL先表?

我也会投票和/或接受任何开源或免费解决此问题的答案。

谢谢。

最佳答案

好吧,刚刚弄明白了。这个答案包含三个要素,两个屏幕截图,然后是一些代码:

  1. 创建类型化数据集以保存图表图像

    Step 1

  2. 将类型化的数据集连接到 RDLC 报告

    Step 2

  3. 用于生成 PDF 并将其流式传输到浏览器的按钮单击代码。

    protected void btnPDF_Click(object sender, EventArgs e)
    {
    //Put the image you want to display in a MemoryStream. You can read an image from the file system
    //or generate an image, etc. This example renders an image to a memory stream using a custom charting control.
    MemoryStream chtLoginsByMonthStream = new MemoryStream();
    this.chtLoginsByMonth.Save(chtLoginsByMonthStream, ImageFormat.Png);


    //Setup the datatable you will pass into the RDLC
    dsStudentUsage.dtUsageChartsDataTable dt = new dsStudentUsage.dtUsageChartsDataTable();
    dsStudentUsage.dtUsageChartsRow dr = dt.NewdtUsageChartsRow();

    //create new Byte Array, this will hold the image data from the memory stream
    byte[] chtLoginsByMonthArray = new byte[chtLoginsByMonthStream.Length];

    //Set pointer to the beginning of the stream
    chtLoginsByMonthStream.Position = 0;

    //Read the entire stream
    chtLoginsByMonthStream.Read(chtLoginsByMonthArray, 0, (int)chtLoginsByMonthStream.Length);

    //Put the byte array into the new datarow in the appropriate column
    dr["imgLoginsByMonth"] = chtLoginsByMonthArray;
    dt.Rows.Add(dr);

    //Add the data source to the report.
    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dsStudentUsage_dtUsageCharts", dt));

    //Setup objects for streaming the PDF to the browser
    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;
    byte[] bytes;


    //Make a container for all of your report parameters
    var rptList = new List<KeyValuePair<string, string>>();
    rptList.Add(new KeyValuePair<string, string>("rpTotalLogins", "2,000"));
    //more parameters go here

    //Feed the report parameters into the actual "ReportParameters" class
    ReportParameter[] rptParams = new ReportParameter[rptList.Count];
    for (int i = 0; i < rptList.Count; i++)
    {
    rptParams[i] = new ReportParameter(rptList[i].Key, rptList[i].Value);
    }

    //Set parameters for the report.
    ReportViewer1.LocalReport.SetParameters(rptParams);

    //Render the report to a byte array in PDF format
    bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);

    //Set the stream to either prompt user as file download or "inline" to stream
    //PDF directly into the browser window.

    HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=StudentUsageReport.pdf");
    //HttpContext.Current.Response.AddHeader("Content-disposition", "inline;");
    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.BinaryWrite(bytes);
    HttpContext.Current.Response.End();

    }

关于c# - 我可以从 SQL Reporting Services (.rdlc) 生成基于图像的 PDF 报告而不将图像保存在任何地方吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4868126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com