我正在创建一个小应用程序,我只需点击一下即可从水晶报表中生成12个PDF文件。每个PDF文件都有不同的参数。 代码正在运行,但我想缩短它,因为它非常重复。由于我是编程新手,我不知道如何实现这一目标。
任何想法都将不胜感激。
这是代码。
private void button1_Click(object sender, EventArgs e)
{
cryRpt.Load(@"\\klb-lims\Crystal\TrendFailure.rpt");
cryRpt.Load(@"\\Server\Crystal\TrendFailure.rpt");
ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
try
{
//GENCHEM
cryRpt.SetParameterValue("AnalysisType", "GENCHEM");
CrDiskFileDestinationOptions.DiskFileName = (GENCHEM);
CrExportOptions = cryRpt.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
cryRpt.Export();
//IC
cryRpt.SetParameterValue("AnalysisType", "IC");
CrDiskFileDestinationOptions.DiskFileName = (IC);
CrExportOptions = cryRpt.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
cryRpt.Export();
//ICPMS
cryRpt.SetParameterValue("AnalysisType", "ICPMS");
CrDiskFileDestinationOptions.DiskFileName = (ICPMS);
CrExportOptions = cryRpt.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
cryRpt.Export();
//ICPOES
cryRpt.SetParameterValue("AnalysisType", "ICPOES");
CrDiskFileDestinationOptions.DiskFileName = (ICPOES);
CrExportOptions = cryRpt.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
cryRpt.Export();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
我不包括13个街区,但你可以知道我在说什么。
以下是我用于保存PDF文件的字符串。 水晶报告参数是固定的。
string GENCHEM = "c:\\temp\\GENCHEM_TrendFailure.pdf";
string IC = "c:\\temp\\IC_TrendFailure.pdf";
string ICPMS = "c:\\temp\\ICPMS_TrendFailure.pdf";
string ICPOES = "c:\\temp\\ICPOES_TrendFailure.pdf";
string METALS = "c:\\temp\\METALS_TrendFailure.pdf";
string MICRO = "c:\\temp\\MICRO_TrendFailure.pdf";
string ORGANIC = "c:\\temp\\ORGANIC_TrendFailure.pdf";
string SOILPREP = "c:\\temp\\SOILPREP_TrendFailure.pdf";
string SUBOUT = "c:\\temp\\SUBOUT_TrendFailure.pdf";
string SVOC = "c:\\temp\\SVOC_TrendFailure.pdf";
string VOC = "c:\\temp\\VOC_TrendFailure.pdf";
string WASTEWATER = "c:\\temp\\WASTEWATER_TrendFailure.pdf";
答案 0 :(得分:0)
我对你在这里使用的任何库都不是很熟悉,但我认为一个好的方法是将这13个字符串放入tuples的数组中。然后,你会这样做:
List<Tuple<String, String>> entries = new List<Tuple<String, String>>();
entries.Add(new Tuple<String, String>("GENCHEM, "c:\\temp\\GENCHEM_TrendFailure.pdf");
entries.Add(new Tuple<String, String>("IC, ""c:\\temp\\IC_TrendFailure.pdf"");
等等。
然后你会遍历列表并说:
for (Tuple<String, String> entry : entries)
{
cryRpt.SetParameterValue("AnalysisType", "entry.Item1");
CrDiskFileDestinationOptions.DiskFileName = (entry.Item2);
//Run through your export options or whatever here, then export
}
基本上,该列表允许您根据需要循环访问任意数量的条目,同时仍然为您提供文件名和条目名称。