我想从使用向导创建的rdlc文件生成pdf报告。为此我尝试编写一个简单的控制台应用程序。当我搜索时,我发现这个任务可以在控制台/获胜表格/网络应用程序中完成。我每次都在下面的线上遇到错误。
byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
查看者对象来自Microsoft.Reporting.WinForms
。 PLS。救命。我已将此dll文件与控制台应用程序一起使用。以下是完整的Main()
方法:
// Setup DataSet
mybookstoreDataSetTableAdapters.authorsTableAdapter ds = new mybookstoreDataSetTableAdapters.authorsTableAdapter();
// Create Report DataSource
ReportDataSource rds = new ReportDataSource("DataSet1", (DataTable)ds.GetData());
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
// Setup the report viewer object and get the array of bytes
ReportViewer viewer = new ReportViewer();
viewer.LocalReport.ReportEmbeddedResource = @"F:\TFS test\ConsoleApplication\ConsoleApplication\Report1.rdlc";
viewer.ProcessingMode = ProcessingMode.Local;
byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
using (FileStream fs = new FileStream(@"F:\TFS test\ConsoleApplication\ConsoleApplication\Authors.pdf", FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
我也尝试了viewer.LocalReport.ReportPath
而不是viewer.LocalReport.ReportEmbeddedResource
,viewer.LocalReport.LocalReportDefinition(TextReader)
,将.rdlc文件移动到.exe文件夹等。
以下是异常的样子:
在调试 - >
Microsoft.Reporting.WinForms.LocalProcessingException was unhandled
HResult=-2146233088
Message=An error occurred during local report processing.
Source=Microsoft.ReportViewer.WinForms
StackTrace:
at Microsoft.Reporting.WinForms.LocalReport.EnsureExecutionSession()
at Microsoft.Reporting.WinForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, CreateAndRegisterStream createStreamCallback, Warning[]& warnings)
at Microsoft.Reporting.WinForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
at Microsoft.Reporting.WinForms.LocalReport.Render(String format, String deviceInfo, PageCountMode pageCountMode, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
at Microsoft.Reporting.WinForms.Report.Render(String format, String deviceInfo, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
at ConsoleApplication.Program.Main(String[] args) in f:\TFS test\ConsoleApplication\ConsoleApplication\Program.cs:line 38
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.ApplicationException
HResult=-2146232832
Message=The report definition for report 'F:\TFS test\ConsoleApplication\ConsoleApplication\Report1.rdlc' has not been specified
Source=Microsoft.ReportViewer.Common
StackTrace:
at Microsoft.Reporting.PreviewStore.GetCompiledReport(CatalogItemContextBase context, Boolean rebuild, Byte[]& reportDefinition, ControlSnapshot& snapshot)
at Microsoft.Reporting.PreviewStore.GetCompiledReport(CatalogItemContextBase context, Boolean rebuild, ControlSnapshot& snapshot)
at Microsoft.Reporting.LocalService.GetCompiledReport(CatalogItemContextBase itemContext, Boolean rebuild, ControlSnapshot& snapshot)
at Microsoft.Reporting.LocalService.CompileReport(CatalogItemContextBase itemContext, Boolean rebuild)
at Microsoft.Reporting.WinForms.LocalReport.EnsureExecutionSession()
InnerException:
在跑步 - >
Unhandled Exception: Microsoft.Reporting.WinForms.LocalProcessingException: An error occurred during local report processing. ---> System.ApplicationException: The report definition for report 'F:\TFS test\ConsoleApplication\ConsoleApplication\bin\Debug\Report1.rdlc' has not been specified
at Microsoft.Reporting.PreviewStore.GetCompiledReport(CatalogItemContextBase context, Boolean rebuild, Byte[]& reportDefinition, ControlSnapshot& snapshot)
at Microsoft.Reporting.LocalService.GetCompiledReport(CatalogItemContextBase itemContext, Boolean rebuild, ControlSnapshot& snapshot)
at Microsoft.Reporting.WinForms.LocalReport.EnsureExecutionSession()
--- End of inner exception stack trace ---
at Microsoft.Reporting.WinForms.LocalReport.EnsureExecutionSession()
at Microsoft.Reporting.WinForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, CreateAndRegisterStream createStreamCallback, Warning[]& warnings)
at Microsoft.Reporting.WinForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
at Microsoft.Reporting.WinForms.LocalReport.Render(String format, String deviceInfo, PageCountMode pageCountMode, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
at Microsoft.Reporting.WinForms.Report.Render(String format, String deviceInfo, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
at ConsoleApplication.Program.Main(String[] args) in f:\TFS test\ConsoleApplication\ConsoleApplication\Program.cs:line 38
Press any key to continue . . .
请帮忙。