How to export crystal report to excel in .net 2002
Hi All,
Advance thanks for ur solutions...
I am getting problem while trying to export crystal report(version 9) to excel in .net 2002.
Here crystal report is generated basing on 3 parameters. i am trying to export the data while clicking on image button.
Here is the code i written:
//########### RPT file ###########
RPT file is connected to database through UDL connection
//######## Page load
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
PrepareReport();
}
}
// ########### Preparing report by passing parameters
private void PrepareReport()
{
CrystalReportViewer1.Visible = true;
sStartDate = "2004-10-11 13:15:39.967" + " 12:00:00 AM";
sEndDate = "2004-10-11 13:23:20.513" + " 12:00:00 AM";
sReportName = "Revenue By Game.rpt";
sTableName = "vwUploadRevenueByGame";
sLocation = "83";
try
{
oRpt.Load(Server.MapPath(sReportName));
CrystalReportViewer1.ReportSource = oRpt;
TableLogOnInfo logOnInfo = new TableLogOnInfo();
logOnInfo = oRpt.Database.Tables[sTableName].LogOnInfo;
ConnectionInfo connectionInfo = new ConnectionInfo ();
connectionInfo = logOnInfo.ConnectionInfo;
connectionInfo.UserID = "sa";
connectionInfo.Password = "sa";
oRpt.Database.Tables [sTableName].ApplyLogOnInfo(logOnInfo);
// Removed declaration of Parameters field
paramFields = AddParameter("StartDate",sStartDate,paramFields);
paramFields = AddParameter("EndDate",sEndDate,paramFields);
paramFields = AddParameter("LocationID",sLocation,paramFields);
//associate param fields with report
CrystalReportViewer1.ParameterFieldInfo = paramFields;
// Put user code to initialize the page here
//oRpt.Close();
}
catch(Exception ex)
{
lblmsg.Text="Exception Caught:"+ex.Message;
}
}
//################# Adding Parameters ##########
public ParameterFields AddParameter(string paramName, string paramValue,ParameterFields paramFields)
{
ParameterField paramField= new ParameterField ();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue ();
ParameterValues paramValues = new ParameterValues ();
// Set the name of the parameter to modify.
paramField.ParameterFieldName = paramName;
// Set a value to the parameter.
paramDiscreteValue.Value = paramValue;
paramValues.Add (paramDiscreteValue);
paramField.CurrentValues = paramValues;
paramFields.Add (paramField);
return paramFields;
}
//########## Export to Excel function ##########
// This function is called when image button is clicked
private void ExportReport()
{
try
{
if (System.IO.File.Exists(Server.MapPath("test1.xls")))
{
System.IO.File.Delete(Server.MapPath("test1.xls"));
DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions();
ExportOptions crExportOptions = new ExportOptions();
crExportOptions = oRpt.ExportOptions;
crDiskFileDestinationOptions.DiskFileName ="test1.xls";
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.Excel;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
this.oRpt.Export();
lblmsg.Text="Exported to the destination Successfully";
lblmsg.Font.Bold=true;
lblmsg.ForeColor = System.Drawing.Color.Green;
this.oRpt.Close();
}
}
catch(Exception ex)
{
lblmsg.Text=ex.Message;
}
}
//############### Result
// We are getting following exception
Invalid report file path.
if remove isPostback condition, then i am getting following exception
Missing parameter field current value