cancel
Showing results for 
Search instead for 
Did you mean: 

CR for VS2010 produces unreadable results when exporting to RTF or XLSX

adrikoopman
Explorer
0 Kudos
99

In my VS2010 (w/ CR2010) windows application, I provide the possibility to export a report in a given format. If the FormatType is PDF, Text or XLS (Excel 97-2003), the output is perfectly readable by the associated program (Acrobat Reader, Notepad or Excel 2003).

However, if I use a (editable)RTF, WordForWindows or Excel Workbook (XLSX) output format, Microsoft Word (2007 / 20010, Excel 207 / 2010) complains about a damaged file.

What am I doing wrong?

Regards,

Adri

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Adri,

Add your report to a test project and then when editing the report in the .NET IDE click the Preview tab and then export it from the viewer. Now try opening those exported files.

What happens?

Thanks

Don

adrikoopman
Explorer
0 Kudos

Hi Don,

I've done an export in several formats (RTF, Word Editable, Word and Excel Workbook) using the VS2010 IDE and they are all readable without any problem.

I've also made a simple project with the possibility to export a report using the CrystalReportViewer: No problems.

However, if I use the

CrystalDecisions.CrystalReports.Engine.ReportDocument.Export(options As CrystalDecisions.Shared.ExportOptions) method

or

CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToDisk(formatType As CrystalDecisions.Shared.ExportFormatType, fileName As String)

the problem appears.

Regards,

Adri

Edited by: Adri Koopman on Aug 4, 2011 1:44 PM

0 Kudos

Hi Adri,

Great, it proves the export does work so it's likely the code you are using, possibly the extended values or simply the API you are using.

Try using the ExportToStream functionality, works much better and is more stable now:

( NOTE: below code is parts and pieces of my test app so you WILL need to alter the code for your types )


        private void ExportToStream_Click(object sender, EventArgs e)
        {
            CrystalDecisions.Shared.MicrosoftMailDestinationOptions mailOpts = CrystalDecisions.Shared.ExportOptions.CreateMicrosoftMailDestinationOptions();
            CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions();
            CrystalDecisions.Shared.ExportOptions exportOpts = new CrystalDecisions.Shared.ExportOptions();
            CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions csvExpOpts = new CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions();
            CrystalDecisions.Shared.HTMLFormatOptions HTMLExpOpts = new CrystalDecisions.Shared.HTMLFormatOptions();

            rpt.Load(@"c:\reports\b.rpt");

            //diskOpts.DiskFileName = "c:\\ReportName.csv";
            //diskOpts.DiskFileName = @"c:\tmp\html\b.html";
            diskOpts.DiskFileName = @"c:\reports\worldsales.pdf";

            exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
            //exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues;
            exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML32;
            exportOpts.ExportDestinationOptions = diskOpts;

            //csvExpOpts = new CharacterSeparatedValuesFormatOptions();
            //csvExpOpts.GroupSectionsOption = CsvExportSectionsOption.Export;
            //csvExpOpts.ReportSectionsOption = CsvExportSectionsOption.Export;
            //csvExpOpts.SeparatorText = ",";
            //csvExpOpts.Delimiter = "|";

            HTMLExpOpts = new HTMLFormatOptions();
            //HTMLExpOpts.HTMLFileName = @"c:\tmp\html\phonebook.html";
            HTMLExpOpts.HTMLBaseFolderName = @"c:\tmp\html\b.html";
            HTMLExpOpts.HTMLEnableSeparatedPages = false;
            HTMLExpOpts.UsePageRange = false;
            HTMLExpOpts.HTMLHasPageNavigator = false;

            System.IO.Stream oStream;
            byte[] byteArray = null;

            //oStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues);
            oStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            byteArray = new byte[oStream.Length];
            oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));

            // this is used to verify the file so I saved it to disk
            System.IO.File.Create(diskOpts.DiskFileName, Convert.ToInt32(oStream.Length - 1)).Close();

            System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
            System.IO.File.SetAttributes(diskOpts.DiskFileName, System.IO.FileAttributes.Directory);
            oStream.Close();

            GC.Collect();
            MessageBox.Show("Export to Stream complete", "RAS", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

Thanks

Don

adrikoopman
Explorer
0 Kudos

Hi Don,

Tried to use the ExportToStream in my VB.NET program, using your C# example code. Same errors. Besides that, the ExportToStream method accepts only am ExportFormatType parameter, no ExportOptions.

Regards,

Adri

0 Kudos

OK, but I also said you had to change the option:

CrystalDecisions.Shared.PdfFormatOptions PDFExpOpts = new CrystalDecisions.Shared.PdfFormatOptions();

PDFExpOpts.CreateBookmarksFromGroupTree = true;

PDFExpOpts.FirstPageNumber = 1;

PDFExpOpts.LastPageNumber = 1;

PDFExpOpts.UsePageRange = true;

Add other options for the other formats also.

Don

adrikoopman
Explorer
0 Kudos

Hi Don,

I know that I need to set that option, but where do I provide the FormatOption parameter to the ExportToStream method, because the ExportToStream method only accepts an ExportFormatType as a parameter?

Regards,

Adri

0 Kudos

Hi Adri,

Sorry I had a closer look, Export To Stream does not have an option off the report object as my code suggests. I assumed it just picked up the options but after a closer look and some testing it doesn't, sorry about that...

So I converted to use the RAS.OutPutController:

In my open dialog box I load the report and set it to the Client Document:

rpt.Load(rptName.ToString(), OpenReportMethod.OpenReportByTempCopy);

OpenReportMethod.OpenReportByTempCopy);

rptClientDoc = rpt.ReportClientDocument;


        private void ExportToStream_Click(object sender, EventArgs e)
        {
            rptClientDoc = rpt.ReportClientDocument;

            CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions();
            CrystalDecisions.Shared.ExportOptions exportOpts = new CrystalDecisions.Shared.ExportOptions();
            CrystalDecisions.Shared.PdfFormatOptions PDFExpOpts = new CrystalDecisions.Shared.PdfFormatOptions();

            diskOpts.DiskFileName = @"c:\reports\MyOutput.pdf";

            exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
            exportOpts.ExportDestinationOptions = diskOpts;

            CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions exportOptions = new CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions();
            //This sets which format we will export to.
            exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatPDF;
            //Create the FormatOptions class for PDF.
            PDFExportFormatOptions PDFexportOptions = new PDFExportFormatOptions();
            PDFexportOptions.StartPageNumber = 1;
            PDFexportOptions.EndPageNumber = 1;
            PDFexportOptions.CreateBookmarksFromGroupTree = true;

            //Set the export format options with the page range we just set.
            exportOptions.FormatOptions = PDFexportOptions;

            //This exports the report to a byte() that we will stream out.
            Byte[] oByte = (Byte[])rptClientDoc.PrintOutputController.ExportEx(exportOptions).ByteArray;

            // this is used to verify the file so I saved it to disk
            System.IO.File.Create(diskOpts.DiskFileName, Convert.ToInt32(oByte.Length - 1)).Close();

            System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(oByte, 0, Convert.ToInt32(oByte.Length - 1));
            System.IO.File.SetAttributes(diskOpts.DiskFileName, System.IO.FileAttributes.Directory);

            GC.Collect();
            MessageBox.Show("Export to Stream complete", "RAS", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

Thanks again

Don

adrikoopman
Explorer
0 Kudos

Hi Don,

sorry for the late response, but I was a few days off due to an external training.

Anyway, I've modified my code to use the RAS.OutputController as in your example, but I still have the same problems. Export to Word or (Editable) RTF produces a file with a problematic RTF content.

However, export to Excel(Record) (.XLS) is (after repair by MS EXCEL 2010) readable.

Regards,

Adri

0 Kudos

Hi Adri,

I don't know then, possibly Word is corrupt also? Try doing a repair for it also. It works for me so it must something on your side and since Excel worked after you repaired it I can only assume something else is corrupt on your PC....

Can anyone else open that RTF output file in your office?

Don

adrikoopman
Explorer
0 Kudos

Hi Don,

To be sure, I tested export to EXCEL (XLS and XLSX) once again using as well as RAS as ReportDocument.ExportToStream method.

Export to XLS works OK on both methods

Export to XLSX (Excel Workbook) produces on both methods a file that can be read after repair.

The Export to (Editable)RTF or WordForWindows still produces invalid RTF files (according to Word 2003, Word 2007 and Word 2010 as tested on different PC's) but the RTF files are readable by using Wordpad.

Does it help for you if I send you the produced RTF files?

Regards,

Adri

0 Kudos

Hi Adri,

I found the cause, actually Ted did....

For some reason the -1 from the file length is the cause. Dragging and dropping the RTF file into notepad shows the last "}" is missing from the file so here's what I used:


           exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatEditableRTF;
            //Create the FormatOptions class for Editable RTF.
            RPTExportFormatOptions RTFExpOptions = new RPTExportFormatOptions();
            //Set the export format options with the page range we just set.
            exportOptions.FormatOptions = RTFExpOptions;

            //This sets which format we will export to.
            exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatEditableRTF;
            //RTFExpOptions.StartPageNumber = 1;
            //RTFExpOptions.EndPageNumber = 2;
            //RTFExpOptions.DiscardUnselectedRecords = false;

            //This exports the report to a byte() that we will stream out.
            Byte[] oByte = (Byte[])rptClientDoc.PrintOutputController.ExportEx(exportOptions).ByteArray;

            // this is used to verify the file so I saved it to disk
            System.IO.File.Create(diskOpts.DiskFileName, Convert.ToInt32(oByte.Length)).Close();

            System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(oByte, 0, Convert.ToInt32(oByte.Length));
            System.IO.File.SetAttributes(diskOpts.DiskFileName, System.IO.FileAttributes.Directory);

This is the line that changed:

System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(oByte, 0, Convert.ToInt32(oByte.Length));

I removed the -1 from the oByte.Length and now it opens fine....

Seems the boys have altered the export array...

Let me know if this works for you also?

Thanks again

Don

adrikoopman
Explorer
0 Kudos

Hi Don,

I've modified the code to add the missing last byte and now the results are OK. Tested using (unmanaged) RAS as wel ExportToStream, no problems.

However, because I wanted to use the .Export method because that one gave me the possibility to modify the export options (Create Bookmaks from Group Tree, Page Range, etc.) I once again ran through my possible output formats (PDF, WordForWindows, RTF, EditableRTF, EXcelRecord and ExcelWorkbook). All of them worked fine when used with the .ExportToStream method, but ExcelWorkBook (XLSX) gave a problem when I opened it in EXCEL 2010 (It was readable after repair). Excel complained about troubles in the Sheet1.xml file. Because I knew the XLSX format is in fact a ZIP file, I unzipped the Sheet1.xml from as wel as the correct as the erroneous file and compared both files for differences and in fact there were:

Here is a sample difference line:

OK

<sheetFormatPr defaultColWidth="10.00" defaultRowHeight="12.75" customHeight="1" />

Err

<sheetFormatPr defaultColWidth="10,00" defaultRowHeight="12,75" customHeight="1" />

Note the use of a decimal comma instead of a decimal point in the faulty line.

You know, here in Holland (and lot of other European countries) we use the decimal comma instead of the decimal point.

Maybe this helps finding the cause of the trouble.

Regards,

Adri

0 Kudos

Hi Adri,

I don't see the same error... Looking the workbook.xml file...

In the Report Designer are you setting the location to Dutch and English or ????

Thanks again

Don

adrikoopman
Explorer
0 Kudos

Hi Don,

I think you're doing the wrong approach... In the windows Control panel, Regional settings, set the comma as decimal separator and the dot as digit grouping symbol.

Then within your .NET code do an export to ExcelWorkbook (XLSX) using the .Export method and the .ExportToStream method of the same report, producing 2 different outputfiles. Rename both .XLSX files to .ZIP and extract from both archives the Sheet1.xml file.

If you'll take a closer look at the differences of both .xml files you will see that one of them uses a dot/point as decimal separator, whereas the other one uses the comma as decimal separator.

In fact, either if you're doing an Export or ExportToStream, one should expect the same results, but that does not happen.

Regards,

Adri

Edited by: Adri Koopman on Aug 23, 2011 8:15 PM

0 Kudos

Hi Adri,

I still see the correct character separators.... mmmm

What version of Windows are you using and how exactly do you have your Language and Regional setting configure?

Can I suggest you log a case in Service Market Place if you have a support contract or buy a single case. Someone in EMEA office will pick up your case who likely has your configuration and may be able to duplicate the issue.

All of my attempts continue to show the NA format. I even tried setting the default format in Excel 2010 to Dutch and it did not make any difference.

Thanks

Don

adrikoopman
Explorer
0 Kudos

Hi Don,

I don't know what happened, but exporting to Excel Workbook (XLSX) usting the .Export method suddenly works OK by now. I really don't know what happened, but everything works OK by now

Regards,

Adri

0 Kudos

Hi Adri,

That's great... I've seen this happen to other, they reboot or go off doing something else or whatever and come and the issue they suddenly starts to work....

please mark as answered.

Have a great day

Don

Answers (0)