2017年3月23日木曜日

レポート機能でサーバー上にPDFを作成して開く [MicrosoftReport]

ReportViewerの印刷ボタンがIE以外だと出ない問題の解決策!

サーバー上にPDFを作って、それを別タブで開く。

-------------------------------------------------------------------------------------
using Microsoft.Reporting.WebForms;
~~~~~~~~~~~~~~~~~~~

//印刷データ
List<test> Datas = new List<test>();
~~印刷データの生成~~

LocalReport localReport = new LocalReport();

//デザインファイルの指定
localReport.ReportPath = @".\Report.rdlc";

//サブレポートがある場合は設定
localReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessingEventHandler);

//データをセット
ReportDataSource reportDataSource = new ReportDataSource("DataSet_Design", Datas);

localReport.DataSources.Add(reportDataSource);

localReport.Refresh();

//PDFデータをバイト配列に格納
byte[] bytes = localReport.Render("PDF");

//tempフォルダにpdfを作成
string fileName = "Report" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".pdf";

FileStream fs = new FileStream(@".\temp\" + fileName, FileMode.Create);

fs.Write(bytes, 0, bytes.Length);

fs.Close();

//pdfを別タブで開く
string url = @"\\temp\\" + fileName;

ClientScript.RegisterStartupScript(this.Page.GetType(), "OpenNewWindow",
    @"<script language=""javascript"">window.open('" + url + @"', 'TARGET');</script>");
-------------------------------------------------------------------------------------

ローカル環境にAcrobat ReaderなどのPDFを開く機能が必要になるけど、とても良い感じに。

これでASP.netもMicrosoftReportで十分。

0 件のコメント:

コメントを投稿