Autofitcolums not working for calculated column?

After I create the spreadsheet, I applied autofitcolumn: populateWorksheet.AutoFitColumns(), it seems not work for calculated column. Thanks in advance.



Hi William,

Thanks for your posting and using Aspose.Cells.

Please download and try the latest version: Aspose.Cells for .NET 8.1.2 and see if it makes any difference.

If your problem still persists, then please provide us your source Excel file and sample code replicating this issue with the latest version. We will look into it and help you asap.

Yes. I downloaded the latest verison. I attached the code and source file. Thanks

Hi William,

Thanks for your sample file, sample code and using Aspose.Cells.

You need to call Workbook.CalculateFormula() before calling Worksheet.AutoFitColumns() method.

Please see the following code, this is same code as yours except I am calling Workbook.CalculateFormula() before auto fitting columns. Changes are highlighted in red color.

I have also attached the output xlsm generated by the following code for your reference.

VB.NET

  Private Sub PopulateTemplate()
        'Create a workbook
        Dim workbook As New Workbook()

        workbook = New Workbook("C:\Test.xlsm")

        ' CreateStyleAspose()

     
        'Maps data from Populated tab
        'duplication starting at row,column
        Dim dupStartRow
        dupStartRow = 4
      

        Dim mappedRow, mappedCol As Integer

        Dim mappedWorksheet As Worksheet = workbook.Worksheets(0)

       
        mappedRow = dupStartRow
        mappedCol = 9 - 1

        'Duplication the nubmer of rows
        Dim dupTotalRows As Integer
        dupTotalRows = 133

        Dim rangString As String = "I4:M4"
        Dim sourceRange As Range = mappedWorksheet.Cells.CreateRange(rangString)

        'Data
        For dataRowCounter As Integer = 1 To dupTotalRows - 1
            Dim startCell = mappedWorksheet.Cells(mappedRow, mappedCol)
            Dim destRange As Range = mappedWorksheet.Cells.CreateRange(startCell.Row, startCell.Column, sourceRange.RowCount, sourceRange.ColumnCount)
            destRange.Copy(sourceRange)

            mappedRow = mappedRow + 1
        Next


workbook.CalculateFormula()
mappedWorksheet.AutoFitColumns()

        workbook.Save("C:\Test11.xlsm")

    End Sub</pre><br>

Great! Works. Thanks