[C #] through ReportViewer save the report to file

[SQL Server] through ReportViewer save the report to file


Reporting Service to report as a scheme of the system, there will be need of you should turn into a chance to report instances of files, in the face of the article, we have to see action on the screen for report can be exported directly in Report, but this kind of action after all reports need to open the file and press export, if today we want the program to perform the half, you can automatically export the report to the Client after the end turn into a file, or as Mail attachments sent directly to a supervisor as a weekly report or monthly report reference data, is it more convenient.

Reporting Service report deployment, there are two, one is deployed to the ReportServer, management and reporting can enjoy security mechanism ReportServer; the other is deployed at our site, specify the file directly through good report and ReportViewer data sources, do appear on the page itself does not have report management functions, but this way the site is simple to deploy, as can be deployed along with the web site.

In this paper, after first mentioning how good rdlc ReportViewer specify the data source, the report threw Client go, we see the following very simple Code:


/// 
/// 调用此function将rdlc档赋予数据后进行导出
/// 
/// 支持PDF、Excel、Image等格式
public void ExportReport(string pType)
{

    //取得数据
    DBCommand tCommand = new DBCommand(@"server=gipi;database=TEST;uid=sa;pwd=sa");
    DataTable tDt = tCommand.Query("Select * from TestTable");
    //清除数据来源
    ReportViewer1.LocalReport.DataSources.Clear();
    //指定报表档路径
    ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
    //设定数据来源
    ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("MyData", tDt));
    ReportViewer1.LocalReport.Refresh();

    Microsoft.Reporting.WebForms.Warning[] tWarnings;
    string[] tStreamids;
    string tMimeType;
    string tEncoding;
    string tExtension;

    //调用ReportViewer.LoadReport的Render function,将数据转成想要转换的格式,并产生成Byte数据
    byte[] tBytes = ReportViewer1.LocalReport.Render(pType, null, out tMimeType, out tEncoding, out tExtension, out tStreamids, out tWarnings);

    //将Byte内容写到Client
    Response.Clear();
    Response.ContentType = tMimeType;
    Response.AppendHeader("Content-Disposition", String.Format("attachment; filename=report.{0}", tExtension));
    Response.BinaryWrite(tBytes);
    Response.End();
} 

Shu tour sail (gipi)

Exploring Force Co-founder, former TutorABC associate director of computer and took the lead, and won the election two-time Microsoft Most Valuable Professional (MVP), founded after leaving the workplace to explore the Force, dedicated to helping young people develop the ability to face the future. Yucai fact that education is closely related with the organization, are in the reserve energy for the future, since 2018, established a one-year special courses "career jump of 24 key lesson" for the training of future leaders of China Taiwan and work.

Original: Large column  [C #] through ReportViewer save the report to file


Guess you like

Origin www.cnblogs.com/petewell/p/11489735.html