如果您不介意在頁面上某處添加您自己的按鈕。這隻有在您生成報告的方式與我的相似時纔有效。基本上我拿報告,把它渲染成字節,並以pdf格式發送這些字節作爲響應。這將打開大多數瀏覽器如Chrome支持的PDF文件。這要求用戶採取額外步驟並點擊打印。
ServerReport sr = new ServerReport();
ReportViewer.ProcessingMode = ProcessingMode.Remote;
sr = ReportViewer.ServerReport;
sr.ReportServerUrl = new Uri("http://****/****");
sr.ReportPath = "/Report";
ReportParameter paramDateFrom = new ReportParameter();
ReportParameter paramDateTo = new ReportParameter();
ReportParameter paramState = new ReportParameter();
ReportParameter paramCounty = new ReportParameter();
string dateFrom = TB_Date_From.Text;
string dateTo = TB_Date_To.Text;
string state = DDL_State.SelectedValue;
string county = DDL_County.SelectedValue;
paramDateFrom.Name = "DateFrom";
paramDateFrom.Values.Add((dateFrom != "" ? dateFrom : null));
paramDateTo.Name = "DateTo";
paramDateTo.Values.Add((dateTo != "" ? dateTo : null));
paramState.Name = "State";
paramState.Values.Add((state != "" ? Common_Functions.resolveStateID(state) : null));
paramCounty.Name = "County";
paramCounty.Values.Add((county != "" ? Common_Functions.resolveCountyID(county) : null));
ReportViewer.ServerReport.SetParameters(new ReportParameter[] { paramDateFrom, paramDateTo, paramState, paramCounty });
// DUMP PDF TO BROWSER
Warning[] warnings;
string[] streamids;
string mimeType, encoding, extension;
byte[] bytes = ReportViewer.ServerReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "inline; filename=myfile." + extension);
Response.BinaryWrite(bytes);
string pdfPath = Server.MapPath("~") + "pdf." + extension;
FileStream pdfFile = new FileStream(pdfPath, FileMode.Create);
pdfFile.Write(bytes, 0, bytes.Length);
pdfFile.Close();
Response.Flush();
Response.End();