Free Support Forum - aspose.com

Some problem with AutoFitRows at .Core

Hello.
We use Aspose.Cells library for .Core 18.4.0. We have a some problem with the AutoFitRows feature. It works correctly only for the first worksheet. For all other sheets the option AutoFitMergedCells = true is ignored and program does only standart Excel autofit function with nonmerged cells.
I give an example of our code below:
foreach (var page in documentParametrs.Pages)
{
Worksheet workSheet = workBook.Worksheets[page.Number];

var options = new AutoFitterOptions
{
AutoFitMergedCells = true,
OnlyAuto = true,
MaxRowHeight = 490
};
workSheet.AutoFitRows(options);
}

@Solni,

I have tested your scenario/case a bit using the following sample code in a VS.NET 2017 (.NET Core 2.0) console application, it works fine and auto-fit rows operation works for merged cells in other worksheets similar to first worksheet:
e.g
Sample code:

 //Instantiate a new Workbook
            Workbook wb = new Workbook();
            //Get the first (default) worksheet
            Worksheet _worksheet = wb.Worksheets[0];
            //Create a range A1:B1
            Range range = _worksheet.Cells.CreateRange(0, 0, 1, 2);
            //Merge the cells
            range.Merge();
            //Insert value to the merged cell A1
            _worksheet.Cells[0, 0].Value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end";
            //Create a style object
            Aspose.Cells.Style style = _worksheet.Cells[0, 0].GetStyle();
            //Set wrapping text on
            style.IsTextWrapped = true;
            //Apply the style to the cell
            _worksheet.Cells[0, 0].SetStyle(style);

            //Create an object for AutoFitterOptions
            AutoFitterOptions options = new AutoFitterOptions();
            //Set auto-fit for merged cells
            options.AutoFitMergedCells = true;

            //Autofit rows in the sheet(including the merged cells)
            _worksheet.AutoFitRows(options);


            //Get the second worksheet
            Worksheet _worksheet1 = wb.Worksheets[wb.Worksheets.Add()];
            //Create a range A1:B1
            Range range1 = _worksheet1.Cells.CreateRange(0, 0, 1, 2);
            //Merge the cells
            range1.Merge();
            //Insert value to the merged cell A1
            _worksheet1.Cells[0, 0].Value = "This is test, this is test this is test this is test this is testin this is tests this is testin this is test..........end";
            //Create another style object
            Aspose.Cells.Style style1 = _worksheet1.Cells[0, 0].GetStyle();
            //Set wrapping text on
            style1.IsTextWrapped = true;
            //Apply the style to the cell
            _worksheet1.Cells[0, 0].SetStyle(style);

            //Create another object for AutoFitterOptions
            AutoFitterOptions options1 = new AutoFitterOptions();
            //Set auto-fit for merged cells
            options1.AutoFitMergedCells = true;
            options1.OnlyAuto = true;
            options1.MaxRowHeight = 490;

            //Autofit rows in the second sheet(including the merged cells)
            _worksheet1.AutoFitRows(options1);

            //Save the Excel file
            wb.Save("out1autofitmergedcells1.xlsx");

I have attached the output file for your reference.
Could you provide us the sample code (runnable) same as above with template file(s) to reproduce the issue on our end, we will check it soon.
files1.zip (6.8 KB)

Aspose.zip (22.8 KB)
Good day. Thank you for your attention to my question.
I found that my problem is not in Autofit feature, but at copy/paste formatted worksheet. Rows height do not preserved after copy.
I solve my problem just do autofit formatting after copy worksheets. But if you are interesting, I provide you sample code.

@Solni,

Thanks for the template file and sample code.

Good to know that your have sorted your issue out now. And, yes, in MS Excel, when you copy/paste a range of cells in the worksheet(s), the formattings might not be retained.

Should you have any further queries, feel free to post us, we will be happy to assist you soon.