How to autofit height of the Row?

Hi,
I would like to autofit the row based on content. I mean If I want to set the multiline values through cell.setValue() method, the complete row value should be visible in excel. What API should I use to achieve this?


Regards,
Azhar

Also, I am setting multiline values by terminating the value with ‘\n’ character. is there any better way?

Regards,
Azhar

Hi Azhar,


Thank you for contacting Aspose support.

If you wish to display each portion of the text (separated with \n) in different line inside a cell, I would recommend using the Wrap Text property. You can set the aforesaid property through the Style object, and can apply on a Cell, Row or Column. Please check the below provided code snippet demonstrating the usage.

Java

Workbook book = new Workbook();
Worksheet sheet = book.getWorksheets().get(0);

Cell A1 = sheet.getCells().get(“A1”);
A1.putValue(“This is a very very long text\nAnother line of text\nAnd another”);

int styleIndex = book.getStyles().add();
Style style = book.getStyles().get(styleIndex);
style.setTextWrapped(true);

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

A1.setStyle(style, flag);

book.save(myDir + “setTextWrapped.xlsx”);

Please feel free to write back in case you have any concerns.

Hi,

I would like to retrieve the height of the row whenever someone puts the value in cell. is it advisable to create style flag for every putValue() call and then retrive the height of that cell?

I tried the API worksheet.autoFitRow(rowIndex) but it seems not working. Even the row height before and after content is same.

Here is the program I am trying


try {
Workbook workbook = new Workbook();
WorksheetCollection sheets = workbook.getWorksheets();
Worksheet sheet = sheets.get(0);
Cells cells = sheet.getCells();
Cell cell = cells.get(“A1”);
System.out.println("height before content: "+cells.getRowHeightPixel(0));
cell.setValue(“azhar sdfaskdf asdkfaskdf sad \n testing adsfsakdfk safdksa df\n dfaksdfkasdkfkjkasdfk akds\n”
+ “azhar sdfaskdf asdkfaskdf sad \n testing adsfsakdfk safdksa df\n dfaksdfkasdkfkjkasdfk akds\n”
+ “azhar sdfaskdf asdkfaskdf sad \n testing adsfsakdfk safdksa df\n dfaksdfkasdkfkjkasdfk akds”);
int styleIndex = workbook.getStyles().add();
Style style = workbook.getStyles().get(styleIndex);
style.setTextWrapped(true);
StyleFlag flag = new StyleFlag();
flag.setWrapText(true);

cell.setStyle(style, flag);

cells.getRows().get(0).applyStyle(style, flag);;

System.out.println("height after content : "+cells.getRowHeightPixel(0));


workbook.save(“multiline.xlsx”,FileFormatType.XLSX);
System.out.println(“done”);
} catch (Exception e) {
e.printStackTrace();
}

Output:
height before content: 17
height after content : 17
done

Please do suggest.

Regards,
Azhar

Hi Azhar,
azhar920:

I would like to retrieve the height of the row whenever someone puts the value in cell. is it advisable to create style flag for every putValue() call and then retrive the height of that cell?

I tried the API worksheet.autoFitRow(rowIndex) but it seems not working. Even the row height before and after content is same.

As discussed earlier, you can apply the Style to a Cell, Row and Column, therefore the most appropriate approach would be to apply the TextWrapped property to either rows or columns instead of applying it to the individual cells. Moreover, if you like to retrieve the height of the row after inserting the value in any of it's cell, you should first apply the TextWrapped property to that particular row, insert the values and then call the autoFitRow method. This way, you will be able to get the exact row height after inserting the data.

Java
try { Workbook workbook = new Workbook(); WorksheetCollection sheets = workbook.getWorksheets(); Worksheet sheet = sheets.get(0); Cells cells = sheet.getCells(); Cell cell = cells.get("A1");
int styleIndex = workbook.getStyles().add(); Style style = workbook.getStyles().get(styleIndex); style.setTextWrapped(true); StyleFlag flag = new StyleFlag(); flag.setWrapText(true);
cell.setStyle(style, flag);
cells.getRows().get(0).applyStyle(style, flag);;
System.out.println("height before content: "+cells.getRowHeightPixel(0)); cell.setValue("azhar sdfaskdf asdkfaskdf sad \n testing adsfsakdfk safdksa df\n dfaksdfkasdkfkjkasdfk akds\n" + "azhar sdfaskdf asdkfaskdf sad \n testing adsfsakdfk safdksa df\n dfaksdfkasdkfkjkasdfk akds\n" + "azhar sdfaskdf asdkfaskdf sad \n testing adsfsakdfk safdksa df\n dfaksdfkasdkfkjkasdfk akds");
sheet.autoFitRow(0); System.out.println("height after content : "+cells.getRowHeightPixel(0));
workbook.save(myDir + "multiline.xlsx",FileFormatType.XLSX); System.out.println("done"); } catch (Exception e) { e.printStackTrace(); }

Output
height before content: 17
height after content : 546

Thanks…

Regards,
Azhar