Crystal Reports - 根据模板导出PDF文件
生活随笔
收集整理的這篇文章主要介紹了
Crystal Reports - 根据模板导出PDF文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在NuGet添加下面幾個包:
前面5個是Crystal Reports在.Net環境下操作的程序包,第6個是生成PDF的包。??
| 1 | CrystalDecisions.CrystalReports.Engine |
| 2 | CrystalDecisions.ReportSource |
| 3 | CrystalDecisions.Shared |
| 4 | CrystalReports.ReportAppServer.CommLayer |
| 5 | CrystalReports.ReportAppServer.CommonObjectModel |
| 6 | PDFsharp |
代碼也很簡單,就是用ReportDocument打開Crystal Reports模板 → 然后連接數據庫把數據裝進去 → 導出PDF。
具體代碼如下,每個步驟都有說明,相應的參數改成你的就可以了。?
using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; using PdfSharp.Pdf.Security; using System;namespace ConsoleApp1 {class Program{static void Main(string[] args){string path = @"C:\Template\Temp2021-5.rpt";using (ReportDocument reportDocument = new ReportDocument()){//根據路徑加載模板reportDocument.Load(path);//寫入Crystal Reports模板所需參數reportDocument.SetParameterValue("Id", 101);//創建數據庫連接對象ConnectionInfo connectInfo = new ConnectionInfo(){ServerName = "10.10.10.10",DatabaseName = "DBTest",UserID = "sa",Password = "abc123"};//登錄到數據庫reportDocument.SetDatabaseLogon(connectInfo.UserID, connectInfo.Password);foreach (Table tbl in reportDocument.Database.Tables){tbl.LogOnInfo.ConnectionInfo = connectInfo;tbl.ApplyLogOnInfo(tbl.LogOnInfo);}//導出數據System.IO.Stream stream = reportDocument.ExportToStream(ExportFormatType.PortableDocFormat);byte[] bytes = new byte[stream.Length];stream.Read(bytes, 0, bytes.Length);stream.Seek(0, System.IO.SeekOrigin.Begin);//把數據裝到PDFPdfDocument document = PdfReader.Open(stream);PdfSecuritySettings securitySettings = document.SecuritySettings;securitySettings.UserPassword = "user";securitySettings.OwnerPassword = "owner";securitySettings.PermitAccessibilityExtractContent = false;securitySettings.PermitAnnotations = false;securitySettings.PermitAssembleDocument = false;securitySettings.PermitExtractContent = false;securitySettings.PermitFormsFill = true;securitySettings.PermitFullQualityPrint = false;securitySettings.PermitModifyDocument = true;securitySettings.PermitPrint = false;//保存文件string savePath = @"C:\test file\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf";document.Save(savePath);document.Close();}}} }有一點,如果你報錯了,說找不到某個版本的程序集,可能是你引用的跟實際的包的版本不一致,在引用里面刪掉,然后再添加你實際用的包就行了。
Could not load file or assembly 'CrystalDecisions.CrystalReports.Engine, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The system cannot find the file specified.'
總結
以上是生活随笔為你收集整理的Crystal Reports - 根据模板导出PDF文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux文件查找和文件内容关键字查找
- 下一篇: 用JavaScript+HTML实现双色