简体   繁体   中英

Set column width dynamically while exporting DATA ONLY to excel in c# using crystal report (ExportFormatType.ExcelRecord)

I am using the below code

// Declare variables and get the export options.
ExportOptions exportOpts = new ExportOptions();
ExcelFormatOptions excelFormatOpts = new ExcelFormatOptions();
DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
exportOpts = rpt.ExportOptions;

// Set the excel format options.
excelFormatOpts.ExcelConstantColumnWidth = 100;
excelFormatOpts.ExcelUseConstantColumnWidth = true;
exportOpts.ExportFormatType = ExportFormatType.ExcelRecord;
exportOpts.FormatOptions = excelFormatOpts;

// Set the disk file options and export.
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
diskOpts.DiskFileName = strFilename;
exportOpts.DestinationOptions = diskOpts;

rpt.Export();

I am getting the data's exported to excel correctly without formating.

Question: Now I need to format. I want to set column width in the excel.

Can I do it?

From my above code the below lines seems not to function

excelFormatOpts.ExcelConstantColumnWidth = 100;
excelFormatOpts.ExcelUseConstantColumnWidth = true;

I appreciate any help in this regard as this issue is eating my time.

i am assuning that you have excel worksheet object with a name esheet.

esheet.Cells.Style.EntireColumn.AutoFit();

try this.

I see two problems in your code.

  1. With ExportFormatType.ExcelRecord you should use ExcelDataOnlyFormatOptions class, not ExcelFormatOptions. ExcelFormatOptions corresponds to ExportFormatType.Excel

  2. ExcelConstantColumnWidth in C# version is in twips and 100 is very small value. You should multiply it by 20.

I managed to get working code for ExportFormatType.Excel case, but not for ExportFormatType.ExcelRecord. Other properties of ExcelDataOnlyFormatOptions work, but ExcelConstantColumnWidth still doesn't work for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM