Hi ,
We are using aspose-cells-7.4.3.jar. The following code is not doing word wrap in the output excel file. Could you please check it out ?
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().get(0);
worksheet.setName(“Sheet Test”);
for (int i = 0; i < 10; i++) {
Row row = worksheet.getCells().getRows().get(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.get(j);
cell.getStyle().setTextWrapped(true);
cell.setValue(“Test dfsadasd asdasds \n asdasdsadas asdasdasdasd asdasdasd asdasdad asdasdasd asdasddad asdasd” + j);
}
}
// worksheet.autoFitColumns();
// worksheet.autoFitRows();
workbook.getWorksheets().setOleSize(1, 5, 1, 5);
workbook.save(“TestAutofit.xlsx”);
Hi,
There is an error in your code snippet, so you need to correct it accordingly. You have to get the style of a cell, specify the text wrapping on and then apply that style to the cell to implement the text wrapping option. See the updated code segment below that works fine here.
Sample code:
public static void main(String[] args) throws IOException, Exception {
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().get(0);
worksheet.setName("Sheet Test");
for (int i = 0; i < 10; i++) {
Row row = worksheet.getCells().getRows().get(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.get(j);
Style style = cell.getStyle();
style.setTextWrapped(true);
cell.setStyle(style);
cell.setValue("Test dfsadasd asdasds \n asdasdsadas asdasdasdasd asdasdasd asdasdad asdasdasd asdasddad asdasd" + j);
}
}
//worksheet.autoFitColumns();
//worksheet.autoFitRows();
workbook.getWorksheets().setOleSize(1, 5, 1, 5);
workbook.save("TestAutofit2.xlsx");
Thank you.
Hi ,
After adding code as mentioned by you. I got the attached output . In that wrapping is not proper. Can you please help me in getting that resolved ?
Hi,
Thanks for providing the template file.
Well, I have checked your template file and found text wrapping option is on properly (right-click on a cell, click Format Cells... In the dialog, click "Alignment" tab and the "Wrap text" option is checked). For your information, you would also need to extend the column's width and row heights accordingly for your needs. Please see the topic for your further reference, you got to extend the column's width and row's height to display the text properly while wrapping the contents:
http://www.aspose.com/docs/display/cellsjava/Line+Break+and+Text+Wrapping
Sample code:
//Create Workbook Object
Workbook wb = new Workbook();
//Open first Worksheet in the workbook
Worksheet ws = wb.getWorksheets().get(0);
//Get Worksheet Cells Collection
Cells cell = ws.getCells();
//Increase the width of First Column Width
cell.setColumnWidth(0, 35);
//Increase the height of first row
cell.setRowHeight(0, 65);
//Add Text to the First Cell
cell.getCell(0, 0).setValue("I am using the latest version of Aspose.Cells to test this functionality");
//Get Cell's Style
Style style = cell.getCell(0, 0).getStyle();
//Set Text Wrap property to true
style.setTextWrapped(true);
//Set Cell's Style
cell.getCell(0, 0).setStyle(style);
//Save Excel File
wb.save("C:\\WrappingText.xls");
Hope, this helps a bit.
Thank you.
Hi
Using cell.setColumnWidth(0, 35) is making the column size to be 35 always even though text size in entire is less than 35.
Hi,
Well, you need to extend the column width to see the wrapped contents fine in the cell(s). I have updated my previous code (add/update a few lines) to accommodate your needs. I use "\n" escape character to place the line break and finally auto-fit row/column to make it more precise. Please see and refer to the following code:
Sample code:
//Create Workbook Object
Workbook wb = new Workbook();
//Open first Worksheet in the workbook
Worksheet ws = wb.getWorksheets().get(0);
//Get Worksheet Cells Collection
Cells cell = ws.getCells();
//Increase the width of First Column Width
cell.setColumnWidth(0, 35);
//Increase the height of first row
cell.setRowHeight(0, 65);
//Add Text to the First Cell
cell.getCell(0, 0).setValue("I am using the latest version of Aspose.Cells\n to test this functionality\n to check if the feature is working fine\n or not");
//Get Cell's Style
Style style = cell.getCell(0, 0).getStyle();
//Set Text Wrap property to true
style.setTextWrapped(true);
//Set Cell's Style
cell.getCell(0, 0).setStyle(style);
ws.autoFitRow(0);
ws.autoFitColumn(0);
//Save Excel File
wb.save("outWrappingText.xlsx");