Skip to main content



No record found.

Microsoft Dynamics AX forum

how to autofit columns in excel file

Posted on by 763


I've written a class that inherits from runbasebatch and I'm using it to fill up excel file. I use two classes  -     OXMLWorkbook_RU, OXMLWorksheet_RU. Here I open and close Excel document:

protected void printDocument(Common   _caller)


    workbook = excelDocument.workbook();
    worksheet = workbook.getWorksheet();
    bookmark = '';

    this.formatBody(taskDaysFactTmp_ICL,taskDaysFactTmp_ICL_additional, _caller);

    comExcelDocument = new ComExcelDocument_RU();, true);

BUt excel file opens with width of the columns not according to the contains of the columns. HOw to autofeet columns? I didn't find anything useful in classes OXMLWorkbook_RU, OXMLWorksheet_RU. I found googling that it can be done like that:


But I'm not using SysExcelWorksheet class at all. How to do that in my case?


  • Tony Mok Profile Picture
    Tony Mok 577 on at
    RE: how to autofit columns in excel file

    If it is OfficeOpenXml, you can use cells.get_item(1,1,worksheet.Dimension.End.Row,worksheet.Dimension.End.Column).AutoFitColumns();

  • dark_knight Profile Picture
    dark_knight 763 on at
    RE: how to autofit columns in excel file

    Hi Sergei,

    I use two classes  -     OXMLWorkbook_RU, OXMLWorksheet_RU but class OXMLWorksheet_RU doesn't contain any autofit() methods for columns.


  • Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,083 on at
    RE: how to autofit columns in excel file

    Hi Azat,

    If I understood correctly OpenXML doesn't support autofit option and you need to calculate the width for each column based on cell content

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .


André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,914 Super User

Martin Dráb Profile Picture

Martin Dráb 225,549 Super User

nmaenpaa Profile Picture

nmaenpaa 101,148


Featured topics

Product updates

Dynamics 365 release plans