如何在java中使用poi自动调整行高度?
我正在使用java中的poi库将字符串放入excel文件中。 问题是我的一些字符串包含多行,当放入单元格时,只有一些信息可见,如第三列所示。 我被迫双击单元格以查看应该显示的信息。 是否有一种方法可以使信息像第三列的最后一行一样? 提前谢谢。 编辑:下面是执行转换作业的类的代码 公共类转换{如何在java中使用poi自动调整行高度?,java,excel,apache-poi,row,Java,Excel,Apache Poi,Row,我正在使用java中的poi库将字符串放入excel文件中。 问题是我的一些字符串包含多行,当放入单元格时,只有一些信息可见,如第三列所示。 我被迫双击单元格以查看应该显示的信息。 是否有一种方法可以使信息像第三列的最后一行一样? 提前谢谢。 编辑:下面是执行转换作业的类的代码 公共类转换{ String filename = "C:\\Users\\daoudi\\Desktop\\FactureTest.xls" ; HSSFWorkbook workbook = new HSSFWorkb
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();
}
}
String filename=“C:\\Users\\daoudi\\Desktop\\FactureTest.xls”;
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet sheet=workbook.createSheet(“第一张纸”);
HSSFRow rowhead=sheet.createRow(0);
公共转换(列表名称、列表电话、列表abo、列表总数)引发IOException{
试一试{
rowhead.createCell(0.setCellValue(“Nom”);
rowhead.createCell(1.setCellValue(“电话”);
rowhead.createCell(2.setCellValue(“Abonnements”);
rowhead.createCell(3.setCellValue(“总计”);
//HSSFCellStyle aligned=工作簿.createCellStyle();
//对齐。设置对齐(HSSFCellStyle.垂直对齐);
对于(int i=0;i以下的工作代码示例
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”
我使用的库包括:
结果:
此外,您甚至可以使用自动包裹,例如:
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();
}
结果:
添加有问题的代码。是否尝试过类似的操作?sheet.autoSizeColumn(1);put sheet.autoSizeColumn..在将数据放入单元格之前,BASIL是我尝试的第一件事,但它似乎只在宽度上起作用,甚至不完美。@弗兰克同样的问题您的代码确实在一行上处理很长的字符串,但问题是我的数据存储在多行上,这段代码产生了一个非常复杂的结果,如您所见:I刚刚看到你的编辑,setWrapText成功了。非常感谢!我想我已经完成了这里写的内容,但是文本仍然从单元格中流出,单元格没有正常增长。我为什么要面对这个问题?