using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Reporting.WebForms;
using System.IO;
using System.Net;
using System.Security;
using System.Configuration;
namespace ReportPdfGenerater
{
///
///Service1 的摘要描述
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string ReportPdfGenerater()
{
try
{
ReportViewer ReportViewer1 = new ReportViewer();
ServerReport SR = ReportViewer1.ServerReport;//Server Report
ReportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportServerUrl"]);
IReportServerCredentials rsnc = new ReportCredential(ConfigurationManager.AppSettings["ReportServerUser"], ConfigurationManager.AppSettings["ReportServerPassword"], ConfigurationManager.AppSettings["ReportServerDomain"]);
ReportViewer1.ServerReport.ReportServerCredentials = rsnc;
ReportViewer1.ServerReport.ReportPath = @"/報表資料夾名稱/TEST";
ReportViewer1.Height = 700;
ReportViewer1.Width = 1000;
ReportViewer1.ShowParameterPrompts = true;
ReportParameter[] para = new ReportParameter[1];
para[0] = new ReportParameter("EMPLOYEE_NUMBER", "999008");
ReportViewer1.ServerReport.SetParameters(para);
Microsoft.Reporting.WebForms.Warning[] tWarnings;
string[] tStreamids;
string tMimeType;
string tEncoding;
string tExtension;
string[] streamids;
byte[] bytes = ReportViewer1.ServerReport.Render("PDF", null, out tMimeType, out tEncoding, out tExtension, out streamids, out tWarnings);
//設定匯出的路徑
System.IO.FileStream fs = System.IO.File.Create(ConfigurationManager.AppSettings["ReportServerPDFLocation"] + "report.pdf");
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
catch (Exception ex)
{
return ex.Message;
}
return "OK";
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Web.config設定
<appSettings>
<add key="ReportServerUser" value="帳號"/>
<add key="ReportServerPassword" value="密碼"/>
<add key="ReportServerDomain" value=""/>
<add key="ReportServerPDFLocation" value="路徑"/>
<add key="ReportServerUrl" value="ip位置/reportserver"/>
</appSettings>
<add key="ReportServerUser" value="帳號"/>
<add key="ReportServerPassword" value="密碼"/>
<add key="ReportServerDomain" value=""/>
<add key="ReportServerPDFLocation" value="路徑"/>
<add key="ReportServerUrl" value="ip位置/reportserver"/>
</appSettings>