我們的接口需要返回一個gird++生成PDF文件的二進制數據,在本地測試都很好,發布到服務器上一直出現“Retrieving the COM class factory for component with CLSID”問題。
最后終於找到問題的解決方法:把程序池里的Enable 32-Bit Applications 設置為True
(ps: 服務器上要安裝Grid++的客戶端)
另外 附上代碼
public struct MatchFieldPairType { public IGRField grField; public int MatchColumnIndex; } public class PrintHelper { // 將 DataTable 的數據轉儲到 Grid++Report 的數據集中 public static void FillRecordToReport(IGridppReport Report, DataTable dt) { MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, dt.Columns.Count)]; //根據字段名稱與列名稱進行匹配,建立DataReader字段與Grid++Report記錄集的字段之間的對應關系 int MatchFieldCount = 0; for (int i = 0; i < dt.Columns.Count; ++i) { foreach (IGRField fld in Report.DetailGrid.Recordset.Fields) { if (String.Compare(fld.Name, dt.Columns[i].ColumnName, true) == 0) { MatchFieldPairs[MatchFieldCount].grField = fld; MatchFieldPairs[MatchFieldCount].MatchColumnIndex = i; ++MatchFieldCount; break; } } } // 將 DataTable 中的每一條記錄轉儲到 Grid++Report 的數據集中去 Report.PrepareRecordset(); foreach (DataRow dr in dt.Rows) { //Report.PrepareRecordset(); Report.DetailGrid.Recordset.Append(); for (int i = 0; i < MatchFieldCount; ++i) { if (!dr.IsNull(MatchFieldPairs[i].MatchColumnIndex)) MatchFieldPairs[i].grField.Value = dr[MatchFieldPairs[i].MatchColumnIndex]; } Report.DetailGrid.Recordset.Post(); } } }
var report = new GridppReport();
report.LoadFromFile(Server.MapPath("~/eExpressReportbulk.grf"));
PrintHelper.FillRecordToReport(report, printDt); string fileName = ConfigurationSettings.AppSettings["pdfPath"] + shipmentNumber + ".pdf"; //直接調用ExportDirect方法執行導出任務 report.ExportDirect(GRExportType.gretPDF, fileName, false, false); FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate); byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, Convert.ToInt32(stream.Length)); stream.Close();
附上Grid++破解dll: 下載