SAP Knowledge Base Article - Public

1455247 - How to set Excel export "Constant column width" in a Visual Studio .NET application

Symptom

  • In the Crystal Reports designer, setting "Constant column width" to 200 points produces correct column width
  • In a Visual Studio .NET application, setting hte property .ExcelUseConstantColumnWidth to 200, results in incorrect column width

Reproducing the Issue

  • Use the code below to export a report to Excel
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Dim expformat As New ExcelDataOnlyFormatOptions
        Dim expformat As New ExcelFormatOptions
        Dim expdest As New DiskFileDestinationOptions
        Dim expoptions As New ExportOptions
        'append a filename to the export path and set this file as the filename property for
 
        Dim crReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
        crReportDocument.Load("c:\crystal\test.rpt")
        expdest.DiskFileName = "C:\crystal\test.xls"

        'set the required report ExportOptions properties
        expformat.ExcelUseConstantColumnWidth = True
     
        expformat.ExcelConstantColumnWidth = 200
        expoptions.ExportDestinationType = ExportDestinationType.DiskFile
        expoptions.ExportDestinationOptions = expdest
        expoptions.ExportFormatOptions = expformat
        expoptions.ExportFormatType = ExportFormatType.Excel

        crReportDocument.Export(expoptions)
      
        MessageBox.Show("Done")
    End Sub

Cause

  • Crystal Reports designer uses "points" to set column width
  • Crystal Reports SDK for VS .NET uses "twips"

Resolution

  • There are 20 twips in one point
  • To get the same column width at runtime as in the Crystal Reports designer, multiply the value by 20 

Keywords

KBA , BI-DEV-NET-SDK , .NET SDK / COM SDK , How To

Product

Crystal Reports 2008 V1