我使用Telerik Report Designer(Standalone)设计报告(.trdx),如何以C#代码以编程方式将其导出为pdf文件?
答案 0 :(得分:1)
我使用下面的代码段。它反序列化.trdx文件,然后从中创建一个Report(Telerik.Reporting.Report)实例。然后可以将此报表实例转换为pdf。
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.IgnoreWhitespace = true;
//read the .trdx file contents
using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(path_to your_trdx_file, settings))
{
Telerik.Reporting.XmlSerialization.ReportXmlSerializer xmlSerializer =
new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();
//deserialize the .trdx report XML contents
Telerik.Reporting.Report report = (Telerik.Reporting.Report)
xmlSerializer.Deserialize(xmlReader);
string mimType = string.Empty;
string extension = string.Empty;
Encoding encoding = null;
// call Render() and retrieve raw array of bytes
// write the pdf file
byte[] buffer = Telerik.Reporting.Processing.ReportProcessor.Render(
"PDF", report, null, out mimType, out extension, out encoding);
// create a new file on disk and write the byte array to the file
FileStream fs = new FileStream(Path_you_need_to_save_the_pdf_file, FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
}
答案 1 :(得分:0)
如果您使用Telerik报告2012或更新版本,则需要将上述代码更改为此
enter code here
XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true;
//read the .trdx file contents
using (
XmlReader xmlReader =
XmlReader.Create(you trdx file path,
settings))
{
ReportXmlSerializer xmlSerializer =
new ReportXmlSerializer();
//deserialize the .trdx report XML contents
Report report = (Report)xmlSerializer.Deserialize(xmlReader);
Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource
{
ReportDocument = report
};
string mimType = string.Empty;
string extension = string.Empty;
//Encoding encoding = null;
// call Render() and retrieve raw array of bytes
// write the pdf file
ReportProcessor reportProcessor = new ReportProcessor();
RenderingResult renderingResult = reportProcessor.RenderReport("DOCX", instanceReportSource, null);
// create a new file on disk and write the byte array to the file
FileStream fs = new FileStream(@"D:\test\Dashboard.DOCX", FileMode.Create);
fs.Write(renderingResult.DocumentBytes, 0, renderingResult.DocumentBytes.Length);
fs.Flush();
fs.Close();
}