How to create excel type tables in crystal report?

1.3k Views Asked by At

I am generating crystal report from c# code. My dataset has 10 columns. The data is dynamic. The report is generating fine with the data at runtime. But the format is the default crystal format without the tabular format. I want to display the data in table format like the tables in excel. How do I go about it?

Following is the code block I am using to generate crystal report.

using (ReportDocument rd = new ReportDocument())
        {
            CrystalReportFilePath = ConfigurationManager.AppSettings["crystal_report_file_path"];
            crystalReportFileName = "client_Details.rpt";
            pdfFileName = clientName + " - " + reportMonth + ".pdf";
            string rptFilePath = string.Format(@"{0}\{1}", CrystalReportFilePath, crystalReportFileName);
            rd.Load(rptFilePath);
            rd.SetDataSource(tempds);
            rd.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
            DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions();

            rd.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;

            crDiskFileDestinationOptions.DiskFileName = string.Format(@"{0}\{1}", CrystalReportFilePath, pdfFileName);
            rd.ExportOptions.ExportDestinationOptions = crDiskFileDestinationOptions;
            rd.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            rd.VerifyDatabase();
            rd.Export();

        }
2

There are 2 best solutions below

0
On BEST ANSWER

We are using crystal report 2008. In this version we don't have the tabular format. Instead we have to select the fields, assign borders to each field and arrange them in a tabular format. Its a pain but that's how it is.

And for different colors for alternate rows as in Excel, we have to write a formula in section expert -> details -> color tab

if RecordNumber mod 2 = 0 then Color (234, 234, 234) else crNoColor
1
On
Public Shared Sub ExportDataSetToExcel(ByVal ds As DataTable, ByVal filename As String)
        Dim response As HttpResponse = HttpContext.Current.Response
        response.Clear()
        response.Buffer = True
        response.Charset = ""
        'response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        response.ContentType = "application/vnd.ms-excel"
        'response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & ".xls")

        Using sw As New StringWriter()
            Using htw As New HtmlTextWriter(sw)
                Dim dg As New DataGrid()
                dg.DataSource = ds
                dg.DataBind()
                dg.RenderControl(htw)
                response.Charset = "UTF-8"
                response.ContentEncoding = System.Text.Encoding.UTF8
                response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble())
                response.Output.Write(sw.ToString())
                response.[End]()
            End Using
        End Using
    End Sub