Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

how to autofit columns in excel file

Posted on by 768

Hello,

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)
{

    this.makeDocument();

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


    this.createTMPTable(_caller);
    this.formatTitle(_caller);
    this.formatHeader(taskDaysFactTmp_ICL);
    this.formatBody(taskDaysFactTmp_ICL,taskDaysFactTmp_ICL_additional, _caller);
    
    excelDocument.closeDocument(true);

    comExcelDocument = new ComExcelDocument_RU();
    comExcelDocument.open(excelDocument.fileName(), 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:

sysExcelWorksheet.columns().autoFit();

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

Thanks.

  • 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 768 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.

    Thanks.

  • Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 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

    social.msdn.microsoft.com/.../solution-how-to-autofit-excel-content

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans