SfDataGrid ExportToExcel using a predefined Excel Template (*.xltx)

I have a predefined Excel template that has column widths already prefined.

In my code I am using the SfDataGrid.ExportToExcel to populate the template. The problem is that the function changes the widths of the columns.

Is there a way to prevent this?


private void ExcelOutput(string templateFile, string outputFilename)
{
    using (ExcelEngine excelEngine = new ExcelEngine())
    {
        IApplication application = excelEngine.Excel;
        application.DefaultVersion = ExcelVersion.Xlsx;
        IWorkbook workbook = application.Workbooks.Open(templateFile, ExcelOpenType.Automatic);


        ExcelExportingOptions exportingOptions = new ExcelExportingOptions
        {
            ExcelVersion = ExcelVersion.Excel2016,
            StartRowIndex = 8,
            StartColumnIndex = 2,
           // ExportColumnWidth = true,
           // ExportRowHeight = false,
           // ExportStyle = false,
           // ExportBorders = false,
           // ExportGroupSummary = false,
           // ExportStackedHeaders = false,
           // ExportTableSummary = false
        };


        IWorksheet wsMisc = workbook.Worksheets["Misc"];
        DgvSummary.ExportToExcel(DgvSummary.View, exportingOptions, wsMisc);
        workbook.SaveAs(outputFilename);
        workbook.Close();


    }
}



1 Reply

VS Vijayarasan Sivanandham Syncfusion Team March 18, 2022 04:25 PM UTC

Hi Bob larranaga,

We have prepared the simple sample and checked the reported issue “changes the widths of the columns while exporting into Excel in SfDataGrid” and unable to replicate the issue from our end. It is working fine as expected. Please find the tested sample from below link,

Sample Link: https://www.syncfusion.com/downloads/support/forum/173734/ze/SfDataGridDemo-307635661

Please have a look at this sample and let us know if we have missed any customization done in your application. Otherwise, try to reproduce the reported issue in this sample and revert to us with the modified sample and steps to replicate the issue. It will be more helpful for us to find the exact cause for the issue and to provide a prompt solution.

Regards,
Vijayarasan S 


Loader.
Up arrow icon