Transposing the cells of a worksheet

Hi,

is there a simple way to transpose all the cells of a worksheet? By that I mean that every column becomes a row and viceversa (column A becomes row 1, column B becomes row 2, etc.). Of course, I would expect that transposing adjusts formulas accordingly.

Thanks for any help.

Hi Mario,

Thanks for your posting and considering Aspose.Cells.

You can transpose your range while copying it using the Range.copy() method and PastOptions.Transpose property.

Please see the following sample code. I have attached the source Excel file used in this code and the output Excel file generated by it for your reference.

The code is fully commented, so you will not have any problem understanding it. Let us know if you still have any questions, we will look into it and help you asap.

Java


//Create workbook object from source excel file

Workbook workbook = new Workbook(“source.xlsx”);


//Access first worksheet

Worksheet worksheet = workbook.getWorksheets().get(0);


//This is the source range you want to transpose

Range srcRange = worksheet.getCells().createRange(“B3:E5”);


//This is the destination range where you want to copy your range

Range dstRange = worksheet.getCells().createRange(“J4:L7”);


//PasteOptions to specify transpose

PasteOptions opts = new PasteOptions();

opts.setTranspose(true);


//Copy source range to destination range

dstRange.copy(srcRange, opts);


//Save the output

workbook.save(“output.xlsx”);



Hi Shakeel,

this is exactly what I need!

Thanks to the Aspose Support Team for the excellent support.