Merge and Autofit cells from any positions that means any row

How to Merge and Auto fit cells(columns) from any positions that means any row?
like i want to merge column 1 and 2 from row 30 and want to set auto fit for this merged columns

image.png (6.3 KB)

@Jerald5588,

Thanks for your query with screenshot.

Well, you may try using Merge feature of MS Excel for your requirements, see the document on how to merge cells (in rows/cols) via Aspose.Cells APIs for your reference:

Please note, when cells are merged, only the data in the top left cell is retained. If there is data in the other cells in the range, this data is deleted.

Also, as you need to auto-fit merged cell column, see the document on how to auto-fit row(s)/column(s) for your reference:

Hope, this helps a bit.

Hi Amjad,

thanks for your update. my question is, is that possible to set auto fit for the merged columns?

@Jerald5588

Please use the following code for your needs. You will have to use AutoFitterOptions.AutoFitMergedCells property to autofit merged cells.

C#

// 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);

Reference Article

HI,
Actually i have merged columns A12 and B12 and the value is not fully displayed because i have did autofit column for B for the above rows
same thing i want to do from row 12 but its merged with column A and B

image.png (4.7 KB)

image.png (13.7 KB)

@Jerald5588,

Thanks for the screenshots.

I think you did not check the note, i.e., “Please note, when cells are merged, only the data in the top left cell is retained. If there is data in the other cells in the range, this data is deleted”. For your information, when you merge A12 and B12 cells, it will give you A12 only, so data in the B12 cell would be removed (retaining only A12 cell’s data). In short, there would be no B12 cell any more. So, you will need to auto-fit A column and not B column to fit the contents in A12 cell accordingly.

Let us know if you still have any confusion. If this is the case, kindly do provide a sample Excel file here. Also, perform your desired operation for your underlying cell(s) in MS Excel manually and save another file to provide us as well, This way, we will be able to understand you better and help you through.

Hi
i have tried your comments but the auto fit column is not working properly means auto fit options does not work on column A. still the auto fit option only work on Column B based on the top rows.
Could you please see the image
image.png (73.6 KB)

@Jerald5588,

Thanks for the screenshot.

As requested earlier, kindly do provide us sample Excel file(s) (input (if any) and output by Aspose.Cells APIs) and paste your sample code (runnable) to reproduce the issue on our end. Also, provide your expected file by performing your steps manually for your underlying cell(s) in MS Excel to save/resave the file, we will check it soon.

Hi

please find the sample report
sample.zip (5.9 KB)

@Jerald5588,

Thanks for the template file.

I have used your template file to auto-fit A column with the following sample code, it works fine. I have attached the output file for your reference:
e.g
Sample code:

 Workbook workbook = new Workbook("E:\\test2\\sample15425.xlsx");

            Worksheet worksheet = workbook.Worksheets[0];

            //Autofit A column
            worksheet.AutoFitColumn(0);
            //Or Autofit all the columns.
            //worksheet.AutoFitColumns();

            workbook.Save("e:\\test2\\out1.xlsx");

Please open the output file into MS Excel and you will see A column is extended fine.
file1.zip (7.0 KB)