exporting to Excel at runtime using System.IO.MemoryStream

Posted on 2013-01-15
Last Modified: 2013-01-22
I have the following code below that I pulled from export to PDF at runtime, and just changed the memorystream to excel from PDF.
I do not see anywhere to put any options like autofit columns.

This works when a user clicks on the open report in excel button, but no format options.
Is there a better approach to this or is there format options I can do with the below code?

  Dim report As New Screen_CostsSummarySheetNewNoDates
                            Dim connectionInfo As New ConnectionInfo()

                            connectionInfo.DatabaseName = "LaborSheets"
                            connectionInfo.UserID =
                            connectionInfo.Password =
                            SetDBLogonForReport(connectionInfo, report)
                            Dim JobHolder = report.ParameterFields("JobNumberHolder")
                            Dim InvoiceNumberHold = report.ParameterFields("TAXBRACKET")

                            Dim s As System.IO.MemoryStream = report.ExportToStream(ExportFormatType.ExcelWorkbook)
                            Dim fs As System.IO.FileStream
                            Dim w As System.IO.BinaryWriter

                            fs = New System.IO.FileStream("C:\Temp\Cost Summary Sheet ALL Dates for " & JLabel.Text & ".xlsx", IO.FileMode.OpenOrCreate)
                            w = New System.IO.BinaryWriter(fs)
                            w.Seek(0, System.IO.SeekOrigin.Begin)
                            s = Nothing
                            fs = Nothing
                            w = Nothing
Question by:BKennedy2008
  • 3
  • 3
LVL 100

Assisted Solution

mlmcc earned 250 total points
ID: 38779359
Is this a Crystal Reports question?

If so, why are you exporting that way rather than using the built in Crystal exporting methods?

LVL 83

Accepted Solution

CodeCruiser earned 250 total points
ID: 38779410
To be able to format the Excel file, you will need to open it using Excel interop and set those properties.

Author Comment

ID: 38779977
using the CR Built in options, what did I mess up?

   Dim CrExportOptions As ExportOptions
                            Dim CrDiskFileDestinationOptions As New  _
                            Dim CrFormatTypeOptions As ExcelFormatOptions = ExportOptions.CreateExcelFormatOptions()
                            CrDiskFileDestinationOptions.DiskFileName = "C:\Temp\Cost Summary Sheet ALL Dates for " & JLabel.Text & ".xlsx"
                            CrFormatTypeOptions.ExcelTabHasColumnHeadings = True
                            CrFormatTypeOptions.ExcelUseConstantColumnWidth = True
                            CrFormatTypeOptions.ExcelConstantColumnWidth = "15"
                            CrExportOptions = report.ExportOptions
                            With CrExportOptions
                                .ExportDestinationType = ExportDestinationType.DiskFile
                                .ExportFormatType = ExportFormatType.ExcelWorkbook
                                .DestinationOptions = CrDiskFileDestinationOptions
                                .FormatOptions = CrFormatTypeOptions
                            End With
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

LVL 100

Expert Comment

ID: 38780742
WHat is the result?


Author Comment

ID: 38782137
none of the format is being applied
LVL 100

Expert Comment

ID: 38785558
What version of Crystal are you using?


Author Closing Comment

ID: 38805401
My bad, got caught up in the end of the year reports.
Kudos to both,
I used the built in CR to create the report, then used the interop to open the file.

Works like a champ.
Thanks again

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 48
VB.Net - KeyPress Event 4 36
VB.Net Report Printing Issue 3 34
RLDC Reporting in Visual studio 11 16
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question