Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.NET Export to Excel Problem

Status
Not open for further replies.

angrymeeow

Programmer
Oct 17, 2002
24
0
0
ZA
Hi there,

I posted this question in the ASP.NET group but I think I might get more help here. I hope this reposting doesn't irritate anyone.

I have a project (written in asp.net 2003 c#, using the bundled version of crystal) that is primarily used to view, print and export (to PDF and Excel) reports . Everything works 100% on my development machine and on a test machine at my client, but some people are getting errors when trying to export to excel (but not to PDF). They get the following error "Object reference not set to an instance of an object".

Below is the event that exports the report to Excel. Please note that I am exporting to the browser.

Code:
private void cmdExcel_Click(object sender, System.EventArgs e)
{
  try
  {
     string ReportName = ReportServer.Business.Settings.GetReportName (this.Session) + " " + DateTime.Today.ToString("dd-MMM-yyyy");

     ExportOptions exp = new ExportOptions(); 

     exp.ExportFormatType = ExportFormatType.Excel;

     exp.FormatOptions = new ExcelFormatOptions();

     ExportRequestContext req = new ExportRequestContex(); 

     req.ExportInfo = exp; 

     System.IO.Stream st; 

     st = this.reportDocument1.FormatEngine.ExportToStream(req);

     Response.ClearHeaders(); 

     Response.ClearContent(); 

     Response.ContentType = "application/xls"; 

     Response.AddHeader("Content-Disposition", "filename="+ ReportName + ".xls");

     byte[] b = new byte[st.Length]; 

     st.Read(b,0,(int) st.Length); 

     Response.BinaryWrite(b); 

     Response.End(); 
  }
  catch (System.Exception excep)
  {
     this.lblErrorMessage.Text = excep.Message.ToString();
  }
}

I failed to mention that when the excel export gets called, the page that originated the request and all it's controls gets exported to excel and the error message is contained within a label from the page. The report viewer appears (as it does on my page in design view) as a grey block with the text "CrystalReportViewer - CrystalReportViewer1 Use ReportSource or DataBindings property to specify a report source."

Also my code for exporting to pdf is identical to the excel export except for the following four lines, that have the relevant pdf stuff instead.

Code:
exp.ExportFormatType = ExportFormatType.Excel;
     
     exp.FormatOptions = new ExcelFormatOptions();

     Response.ContentType = "application/xls"; 

     Response.AddHeader("Content-Disposition", "filename="+ ReportName + ".xls");

Can anyone help?


[red]G[/red][purple]r[/purple][blue]r[/blue][green]r[/green][yellow].[/yellow][white]..[/white][yellow]m[/yellow][green]e[/green][blue]e[/blue][purple]o[/purple][red]w[/red] [cat2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top