I am developing a WPF application (Visual Studio 2019 V16.11.17) and use CrystalReportsViewer to preview the report with a picture in it. When I click at built-in export button in CrystalReportsViewer and select type "Microsoft Excel (XLSX) (*.xlsx)" and save, the application closed unexpectedly without any error or exception even I already tried to caught unhandled exception. How do I fix this? and how to get error info?
In App.xaml file:
DispatcherUnhandledException="Application_DispatcherUnhandledException"
In App.xaml.cs file:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("Error" + Environment.NewLine + e.Exception.Message, "Error");
e.Handled = true;
}
In MainWindow.xaml.cs file:
public MainWindow()
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Debug.Print(e.ToString());
}
Preview crystal report code:
_report = new ReportDocument();
_report.Load("Path to CrystalReport.rpt");
_report.SetDataSource(dataTable);
ViewerCore view = crystalReportViewer1.ViewerCore;
view.ReportSource = _report;
More information:
- Installed SAP Crystal Reports, version for Microsoft Visual Studio V13.0.32.4286.
- Installed SAP Crystal Reports runtime engine for .NET framework (32 bit) V13.0.32.4286.
- Installed SAP Crystal Reports runtime engine for .NET framework (64 bit) V13.0.32.4286.
- Picture info: JPEG file, 50.4 KB, 710 x 400 px.
Thanks in advance.
- I can export successfully if select type "Microsoft Excel (XLSX) (*.xlsx)" without the picture in the report.
- I can export successfully if select type "Microsoft Excel – Data Only (XLSX) (*.xlsx)" with the picture in the report.
- I can export successfully if select type "Microsoft Excel (XLS) (*.xls)" with the picture in the report.
- I cannot export file if select type "Microsoft Excel (XLSX) (*.xlsx)" with the picture
2
Answers
It’s a limitation of crystal reports that you can only export data only to excel. Pictures, not being data, will result in unpredictable results.
Options include:
Choose one of the export formats that supports pictures, such as pdf
Remove the picture
Use a different reporting provider.
Since crystal reports is not supported in recent versions of visual studio, you might want to consider a different reporting approach now for longer term support.
Please see:
https://help.sap.com/docs/SAP_CRYSTAL_REPORTS,_DEVELOPER_VERSION_FOR_MICROSOFT_VISUAL_STUDIO/0d6684e153174710b8b2eb114bb7f843/45b016cb6e041014910aba7db0e91070.html?locale=en-US
Edit
If your users particularly like their data in reports you could maybe consider excel based reporting and xslt. I had to do this one place I worked back in 2009 to give users spreadsheets with calculations in them. It was easier than I thought ( start with a formatted xslx spreadsheet ).
The user departments were very pleased and it’s been an approach I’ve used successfully a few times since.
No need to use a different reporting provider.
At least one of the 3rd-party Crystal Reports Viewers listed here supports exporting reports to .xlsx as a regular export rather than as a Data Only export.