///
/// 簡易Crystal Report範例 For ASP.NET C#
///
public void buildReport(String expType)
{
SqlDataAdapter da;
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connStr);
try
{
conn.Open();
da = new SqlDataAdapter("SELECT * FROM REPORT_DATA", conn);
da.Fill(dt);
String strFilePath = Server.MapPath("CR001.rpt"); //Application.StartupPath + @"\CR002.rpt";
String strOutFileName = String.Format("CR{0}.PDF", DateTime.Now.ToString("yyyyMMddHHmmss"));
ReportDocument Crpt = new ReportDocument();
// 取得Report檔案
Crpt.Load(strFilePath);
// 設定Report資料來源
Crpt.SetDataSource(dt);
// 設定Report參數
Crpt.SetParameterValue("ParamName, ","Value");
// 設定CrystalReportViewer的Report物件來源
//CrystalReportViewer1.ReportSource = Crpt;
if(expType.Equals("DISK"))
{
// 寫入實體檔案
Crpt.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath(@"PDF_FILES\" + strOutFileName);
}
else if(expType.Equals("RESPONSE"))
{
// 輸出串流檔案
Crpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, strOutFileName);
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JS001", "alert('轉檔完成!!')", true);
Crpt.Close();
da.Dispose();
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JS001", "alert('" + ex.Message + "')",true);
}
finally
{
dt.Dispose();
conn.Close();
conn.Dispose();
}
}
張貼留言