Crystal Report (Quick Start)

Crtstal Report 建置與使用

安裝

下面的連結已經講得很清楚了,拿到安裝檔裝起來就對了

https://www.dotblogs.com.tw/jackbgova/archive/2014/06/13/145525.aspx

建立報表

報表的副檔名(.rpt),你可以使用Crystal Report的IDE建立,也可以從VS中去建立

替報表繫結資料,首先必須先準備好.xsd,你可以從查詢出來的DataTable產出

table.WriteXmlSchema("Data.xsd");

或是直接用VS新增

這是我新增的XSD檔,兩個欄位Number、Name

將報表(.rpt)和(.xsd)通通放到專案中

接下來繫結報表跟.xsd,開啟資料庫專家,建立新連接,展開ADO.NET (XML)

選擇你的.xsd

按下完成後會有一個新增的連線,並加入至報表

之後你會在資料庫中看到你剛剛加入的欄位了

將欄位拖曳到【Section3 (細目)】,展開【特殊欄位】拖曳【列印日期】【列印時間】

在Section1【報表首】空白處,【右鍵】.【插入】.【文字物件】,並打上要顯示的文字

在Section4【報表尾】空白處,【右鍵】.【插入】.【線條】,按下【主報表 預覽】可進行預覽,最後記得儲存檔案

基本上這樣就完成了報表(.rpt)的部分,但我希望報表能出現在輸出目錄中(之後可直接覆蓋.rpt檔完成報表的更新)

對專案中的報表【右鍵】.【屬性】

【建置動作】 無

【複製到輸出目錄】 永遠複製

資料繫結
                using (var cryRpt = new ReportDocument())
                {
                    // 從輸出目錄中載入報表
                    var folder = Directory.GetCurrentDirectory();

                    // 報表檔案
                    var file = Path.Combine(folder, "SimpleReport.rpt");

                    // 載入報表
                    cryRpt.Load(file);

                    // 資料來源
                    var dataSource = this.FakeData();

                    // 設定資料來源
                    cryRpt.SetDataSource(dataSource);

                    // 指定印表機印出
                    //var printer = new PrinterSettings();
                    //printer.PrinterName = "PRP-300";

                    //var printer2 = new PrinterSettings();
                    //printer2.PrinterName = "PRP-300 (第二台)";

                    //cryRpt.PrintToPrinter(printer, printer.DefaultPageSettings, false);

                    //cryRpt.PrintToPrinter(printer2, printer2.DefaultPageSettings, false);

                    // 預設印表機
                    cryRpt.PrintToPrinter(1, false, 0, 0);

                }
        private DataSet FakeData()
        {
            var number = "Number";
            var name = "Name";

            var table = new DataTable();

            // 記得設定名稱要和xsd的資料表名稱一樣
            table.TableName = "Data";

            table.Columns.Add(new DataColumn(number, typeof(int)));
            table.Columns.Add(new DataColumn(name, typeof(string)));

            for (int i = 0; i < 10; i++)
            {
                var row = table.NewRow();

                row[number] = i;

                row[name] = string.Format("第{0}號商品", i);

                table.Rows.Add(row);
            }

            var dataSource = new DataSet();

            dataSource.Tables.Add(table);
            return dataSource;
        }

主要的動作

1. 載入報表

2. 設定 data source

3. 執行動作

ReportDocument 有提供的輸出方法

        public virtual void Export();
        public virtual void Export(ExportOptions options);
        public virtual void ExportToDisk(ExportFormatType formatType, string fileName);
        public virtual void ExportToHttpResponse(ExportFormatType formatType, System.Web.HttpResponse response, bool asAttachment, string attachmentName);
        public virtual void ExportToHttpResponse(ExportOptions options, System.Web.HttpResponse response, bool asAttachment, string attachmentName);
        public virtual Stream ExportToStream(ExportFormatType formatType);

ReportDocument 有提供的列印方法 

        public virtual void PrintToPrinter(PrinterSettings printerSettings, PageSettings pageSettings, bool reformatReportPageSettings);
        public virtual void PrintToPrinter(int nCopies, bool collated, int startPageN, int endPageN);
        public virtual void PrintToPrinter(PrinterSettings printerSettings, PageSettings pageSettings, bool reformatReportPageSettings, PrintLayoutSettings layoutSettings);
問題

1. 無法載入報表

確定報表路徑正不正確,有可能是因為沒有設定至輸出目錄導致找不到報表。

2. An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

http://stackoverflow.com/questions/23774260/filenotfoundexception-occured-while-assign-datasource-in-crystalreport

3. DataTable 有資料印出來卻沒資料

檢查DataName有沒有設定或不一樣。