Click here to Skip to main content
15,913,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I am using C#, asp.net, framework 2, crystal report.
I put an image on crystal report viewer.
When clicking on that image, the crystal report is exported to excel .
My problem is that the crystal report layout is different when exporting to excel.
The column width is very large for some column in the excel.
I put the following code to export to excel in the click event of image .

C#
DataSet rptDS = new DataSet(); 
myReport.Load(Server.MapPath("~/Reports/rptCompOperationalResultsMonthlyFYCY.rpt")); 
//Create Dataset

//here we use single table. if there mutiple table we need to use loop for every table
if (Session["YearType"].ToString() == "0") 
{
this.FillDSWithTable("select * from tmpCalOperationalResultMonthly WHERE CompCode=" + Session["intCompCode"] + " AND mthCode=" + Session["intCurrentMonth"] + " AND torType='COMPANYWISECY'", "tmpOperationalResultMonthly", ref rptDS, con); 
}
else

{
this.FillDSWithTable("select * from tmpCalOperationalResultMonthly WHERE CompCode=" + Session["intCompCode"] + " AND mthCode=" + Session["intCurrentMonth"] + " AND torType='COMPANYWISEFY'", "tmpOperationalResultMonthly", ref rptDS, con); 
}
//Set Report Data Source

//here we use single table. if there mutiple table we need to use loop for every table
myReport.Database.Tables[0].SetDataSource(rptDS.Tables[myReport.Database.Tables[0].Name]);
 
 
myReport.DataDefinition.FormulaFields["Company"].Text = "'" + Session["strCompName"].ToString() + "'";
 
// Stop buffering the response
Response.Buffer = false; 
// Clear the response content and headers

Response.ClearContent();
Response.ClearHeaders();
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "inline;filename=report.pdf");

// Export the Report to Response stream in Excel format and file name Customers

myReport.ExportToHttpResponse(ExportFormatType.Excel, Response, false, "OperationalResult6Months"); 
// myReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false, "");

// There are other format options available such as Word, PDF, CVS, and HTML in the ExportFormatType Enum given by crystal reports


thanks in advance
George


[Edited]Code is Wrapped in "pre" tag[/Edited]
Posted
Updated 24-Jan-11 23:58pm
v3

1 solution

After the export is done, you can programatically autofit the columns of your excel sheet.

Try google[^] to autofit columns.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900