I'm using the poi libary in java for my application to put strings in an excel file. The thing is some of my strings contains multiple lines and when put in a cell only some of the informations are visible as seen on the third column. https://i.sstatic.net/j0tLg.jpg I'm forced to double click on the cell to view the informations as they should be presented. Is there a method I can use to make the informations to be like the last row of the third column? Thanks in advance. Edit: here is the code of the class that do the conversion job.
public class conversion {
String filename = "C:\\Users\\daoudi\\Desktop\\FactureTest.xls" ;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow(0);
public conversion(List<String> nom , List<String> tel, List<String> abo, List<String> total) throws IOException{
try{
rowhead.createCell(0).setCellValue("Nom");
rowhead.createCell(1).setCellValue("Telephone");
rowhead.createCell(2).setCellValue("Abonnements");
rowhead.createCell(3).setCellValue("Total");
//HSSFCellStyle aligned = workbook.createCellStyle();
//aligned.setAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
for (int i=0;i<nom.size();i++){
HSSFRow row = sheet.createRow(i+1);
Cell cell = row.createCell(0);
cell.setCellValue(nom.get(i));
cell = row.createCell(1);
cell.setCellValue(tel.get(i));
cell = row.createCell(2);
cell.setCellValue(abo.get(i));
//cell.setCellStyle(aligned);
cell = row.createCell(3);
cell.setCellValue(total.get(i));
//cell.setCellStyle(aligned);
}
sheet.autoSizeColumn((short)0);
sheet.autoSizeColumn((short)1);
sheet.autoSizeColumn((short)2);
sheet.autoSizeColumn((short)3);
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
Below a working code example
try {
FileInputStream is = new FileInputStream(new File("C:\\Desktop\\ook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(is);
String str = "111111111111111111111111111111111111111111111111111111111111111111111111111111111";
HSSFSheet sheet = wb.getSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
Cell cell1 = row.createCell(1);
CellStyle style = wb.createCellStyle();
cell.setCellStyle(style);
cell.setCellValue(str);
CellStyle style2 = wb.createCellStyle();
cell1.setCellStyle(style2);
cell1.setCellValue(str);
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
wb.write(new FileOutputStream(new File("C:\\Desktop\\ook.xls")));
} catch (IOException e) {
e.printStackTrace();
}
I write "str" at the first two columns at the first row .
The libraries im using are :
Result:
In addition , there is even autowrap you could use , an example below :
try {
FileInputStream is = new FileInputStream(new File("C:\\Desktop\\ook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(is);
String str = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
HSSFSheet sheet = wb.getSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
CellStyle style = wb.createCellStyle();
style.setWrapText(true);
cell.setCellStyle(style);
cell.setCellValue(str);
wb.write(new FileOutputStream(new File("C:\\Desktop\\ook.xls")));
} catch (IOException e) {
e.printStackTrace();
}
Result :