透過Crystal Report的報表檔產出PDF、Image等檔案格式,方便做檔案的搬遷或者mail。
以下兩個function分別做了不同的事情,第一個function會將報表檔產生到特定路徑下,方便進行檔案管理,
01
/// <summary>
02
/// 透過crystal report產出報表結果檔
03
/// </summary>
04
/// <param name="pReportPath">報表rpt檔的實體路徑</param>
05
/// <param name="pExportFileName">要輸出的檔案路徑</param>
06
/// <param name="pReportData">要餵進去的資料</param>
07
/// <param name="pExpertFormatType">輸出的檔案類型(PortableDocFormat=PDF,不支援XML與IMAGE)</param>
08
public void getCrystalReportFile(string pReportPath, string pExportFileName, DataTable pReportData, ExportFormatType pExpertFormatType)
09
{
10
ReportDocument tCRReportDocument = new ReportDocument();
11
12
//載入該報表
13
tCRReportDocument.Load(pReportPath);
14
//設定資料
15
tCRReportDocument.SetDataSource(pReportData);
16
17
switch (pExpertFormatType.ToString())
18
{
19
case "EXCEL":
20
tCRReportDocument.ExportToDisk(ExportFormatType.Excel, pExportFileName);
21
break;
22
case "HTML3.2":
23
tCRReportDocument.ExportToDisk(ExportFormatType.HTML32, pExportFileName);
24
break;
25
case "HTML4.0":
26
tCRReportDocument.ExportToDisk(ExportFormatType.HTML40, pExportFileName);
27
break;
28
//case "XML":
29
// tResult = getCrystalReportFile(tPath, tSQLTable, ExportFormatType.PortableDocFormat);
30
// break;
31
case "CSV":
32
tCRReportDocument.ExportToDisk(ExportFormatType.ExcelRecord, pExportFileName);
33
break;
34
case "PDF":
35
tCRReportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, pExportFileName);
36
break;
37
//case "IMAGE":
38
// tResult = getCrystalReportFile(tPath, tSQLTable, ExportFormatType.);
39
// break;
40
default:
41
tCRReportDocument.ExportToDisk(ExportFormatType.Excel, pExportFileName);
42
break;
43
44
}
45
}

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

第二個function則會將要產出的報表檔轉成二進位的型態,方便存到資料庫中,
01
/// <summary>
02
/// 傳入報表路徑、資料與要轉的檔案類型,回傳CR的報表陣列
03
/// </summary>
04
/// <param name="pReportPath">報表路徑(在網站中可用相對路徑,在類別中需要指定實體路徑)</param>
05
/// <param name="pReportData">資料內容</param>
06
/// <param name="pExpertFormatType">要轉出的檔案類型</param>
07
/// <returns>回傳結果的陣列</returns>
08
public byte[] getCrystalReportByte(string pReportPath, DataTable pReportData, ExportFormatType pExpertFormatType)
09
{
10
ReportDocument tCRReportDocument = new ReportDocument();
11
12
//載入該報表
13
tCRReportDocument.Load(pReportPath);
14
//設定資料
15
tCRReportDocument.SetDataSource(pReportData);
16
//將資料內容轉成串流
17
Stream tStream = tCRReportDocument.ExportToStream(pExpertFormatType);
18
byte[] tResult = new byte[tStream.Length];
19
//將串流資料轉成byte[]
20
tStream.Read(tResult, 0, Convert.ToInt32(tStream.Length));
21
tStream.Close();
22
23
return tResult;
24
}

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

兩種方式都可利用,不過我比較常使用第二種,轉成二進位,一方面方便存到資料庫保存,另一方面也方便直接轉成stream下載到client端來。
![]() |
游舒帆 (gipi) 探索原力Co-founder,曾任TutorABC協理與鼎新電腦總監,並曾獲選兩屆微軟最有價值專家 ( MVP ),離開職場後創辦探索原力,致力於協助青少年培養面對未來的能力。認為教育與組織育才其實息息相關,都是在為未來儲備能量,2018年起成立為期一年的專題課程《職涯躍升的關鍵24堂課》,為培養台灣未來的領袖而努力。 |