如何使用poi在java中自动调整行高度?
我在我的应用程序中使用java的poi库文件来将字符串放入excel文件。 事情是我的一些字符串包含多行,当放入单元格时,只有一些信息可见,如第三列所示。 http://imgur.com/Iy7B2OY 我不得不双击单元格来查看应该显示的信息。 有没有一种方法可以让信息像第三列的最后一行? 在此先感谢。 编辑:这是执行转换作业的类的代码。如何使用poi在java中自动调整行高度?
公共类转换{
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();
}
}
}
下文一个工作代码示例
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();
}
我写的 “STR” 处的第一行的前两列。
即时通讯使用的库是:
结果:
此外,甚至有autowrap你可以使用下面的例子:
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();
}
成果:
你的代码确实可以在一行上使用非常长的字符串,但事情是我的数据存储在多行上,并且这段代码产生了一个非常奇怪的结果,你可以看到:http:// imgur。 com/a/kmAAx –
我刚看到你的编辑,setWrapText做了这个工作。非常感谢你! –
加入你的代码中的问题。 – Nikolay
你尝试过这样的事吗? sheet.autoSizeColumn(1); –
put sheet.autoSizeColumn ..在将数据放入单元格之前 – Frank