可能有点旧,但这是 C# 和 Crystal Report 13.0.28 的有效解决方案。无法判断是否真的需要修改 InitTable 中的其他属性。在未来的 CR 版本中,它们可能不会被设置为这些默认值。
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
private void PrintMe()
{
using (ReportDocument rep = new ReportDocument())
{
//Open report
rep.Load(sReport, OpenReportMethod.OpenReportByTempCopy);
NameValuePair2 nvp2 = new NameValuePair2("File Path ", sFullFileName); //Space after "File Path " is important! The LogonProperties have defined it as such
InitTables(rep, nvp2);
rep.Refresh();
if (sReportPdf == null)
{
rep.PrintOptions.PaperSource = printerSettings.PaperSource;
rep.PrintOptions.PaperOrientation = printerSettings.PaperOrientation;
rep.PrintOptions.PaperSize = printerSettings.PaperSize;
rep.PrintOptions.PrinterName = printerSettings.PrinterName;
rep.PrintOptions.ApplyPageMargins(printerSettings.PageMargins);
rep.PrintToPrinter(printerSettings.NumCopies, true, 1, 9999);
}
else
{
rep.ExportToDisk(ExportFormatType.PortableDocFormat, sReportPdf);
}
}
}
private void InitTables(ReportDocument rep, NameValuePair2 filePath)
{
foreach (Table table in rep.Database.Tables)
{
var collection = table.LogOnInfo.ConnectionInfo.Attributes.Collection;
InitTableCollections(collection, "Database DLL", "crdb_adoplus.dll"); //same as default
InitTableCollections(collection, "QE_DatabaseName", ""); //same as default
InitTableCollections(collection, "QE_DatabaseType", "ADO.NET (XML)"); //same as default
InitTableCollections(collection, "QE_ServerDescription", "items"); //default: invoice
InitTableCollections(collection, "QE_SQLDB", false); //same as default
InitTableCollections(collection, "SSO_Enabled", false); //same as default
InitTableCollections(table.LogOnInfo.ConnectionInfo.LogonProperties, filePath);
table.ApplyLogOnInfo(table.LogOnInfo);
}
try
{
foreach (ReportDocument subReportDocument in rep.Subreports)
InitTables(subReportDocument, filePath);
}
catch (NotSupportedException)
{
//thrown when trying to enumerate rep.Subreports at a certain depth
}
}
private void InitTableCollections(NameValuePairs2 collection, string key, object value)
{
if (collection.ContainsKey(key))
collection.Set(key, value);
else
collection.Add(new NameValuePair2(key, value));
}
private void InitTableCollections(NameValuePairs2 collection, NameValuePair2 nvp)
{
if (collection.ContainsKey(nvp.Name))
collection.Set(nvp.Name, nvp.Value);
else
collection.Add(new NameValuePair2(nvp.Name, nvp.Value));
}