AutoFit functionality not working

Hi,

I am using aspose-cells-8.1.2.jar file to generate excel report.
If my text size is big then i am trying to use autofit methods in the sheet object. but its not working for me.

Please do the needful.
Thanks in advance.


Hi Nagesh,


Thank you for contacting Aspose support.

We would request you to please give this scenario another try while using the latest version of Aspose.Cells for Java 8.3.1 on your end. We have recently tested similar cases with this release and could not find any problem. In case the problem persists, please post your sample and code snippet for further investigation.

Hi,

Thanks for the immediate reply.

Code snippet:
String temp = "He had said there was evidence against the accused and if the court feels, "
+ "it can take cognisance on closure report in which the agency said that ‘no prosecutable evidence’ "
+ “was found against the company and others, arrayed as accused in its FIR.”;

Range range = worksheets.getRangeByName(“ABC”);
range.setValue(temp);

Style cellStyle = range.getCellOrNull(0, 0).getStyle();
cellStyle.setTextWrapped(true);

AutoFitterOptions autoOptions = new AutoFitterOptions();
options.setAutoFitMergedCells(true);

try {
sheetForm0.autoFitRow(1, 3, 1, 3); // here it is corresponds to cell name given as ‘ABC’

/******** I have tried the following methods also *******/
// sheetForm0.autoFitRows();
// sheetForm0.autoFitRows(options);
// sheetForm0.autoFitRow(2);
// sheetForm0.autoFitRow(2, 1, 3, autoOptions);
// sheetForm0.autoFitRow(2, 1, 3);
// sheetForm0.autoFitRows(false);
// sheetForm0.autoFitRows(true);
// sheetForm0.autoFitRows();
// sheetForm0.autoFitRows(1, 3);
} catch (Exception e) {
e.printStackTrace();
}

Still, I am not able to view the autofit cell in the merged cell(cell name given as ABC).
I tried with aspose-cells-8.3.1.jar also,but not result.
Please help me.

Thanks,
Nageswararao

Hi Nageswararao,


Thank you for providing the code snippet for further investigation.

We have noticed a few problems in your code snippet and have fixed them for you. You can find the updated code as follow. Please note, you have to apply the style (with text wrapping) onto the range using the applyStyle method otherwise the style changes will not take effect. Once you have applied the style you can call any auto fit routine that suits your need, however, an object of AutoFitterOptions with AutoFitMergedCells property set to true should also be passed to the desired auto fit method because your range has merged cells. Please check the input & output spreadsheets as attached.

Java

String temp = "He had said there was evidence against the accused and if the court feels, "
+ "it can take cognisance on closure report in which the agency said that ‘no prosecutable evidence’ "
+ “was found against the company and others, arrayed as accused in its FIR.”;

Workbook book = new Workbook(“D:/book1.xlsx”);
WorksheetCollection worksheets = book.getWorksheets();
Range range = worksheets.getRangeByName(“ABC”);
range.setValue(temp);

Style style = book.createStyle();
style.setTextWrapped(true);

StyleFlag flag = new StyleFlag();
flag.setWrapText(true);

range.applyStyle(style, flag);

AutoFitterOptions autoOptions = new AutoFitterOptions();
autoOptions.setAutoFitMergedCells(true);

worksheets.get(“Sheet1”).autoFitRows(autoOptions);

book.save(“D:/output.xlsx”);

H,

Thanks allot…

But,This code is working for only the merged columns,but not for the merged rows & columns.
Can u please update the code for both merged columns & rows.

Thanks,
Nageswararao

Hi Nageswararao,


Thank you for writing back.

Please try by adding the following statement to the previously provided code snippet. I hope it should help.

Java

worksheets.get(“Sheet1”).autoFitColumns(autoOptions);

In case you still face problems in achieving your desired results, please provide the sample spreadsheet along with a snapshot highlighting the area that you wish to fit. We will review the both artifacts to provide you the custom solution for it.