0

我正在做一个 java 应用程序 (J2SE),我必须生成一个 excel 文档 (.xls)。我确实生成了文件,但我无法根据同一单元格中具有最大长度的内容动态更改单元格宽度。

这是我的编码...

     try {

      ResultSet rs = com.dss.system.DBORCL.search("sql statement");

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Excel Sheet");
        HSSFRow rowhead = sheet.createRow((short) 0);

        rowhead.createCell((short) 0).setCellValue("Site");
        rowhead.createCell((short) 1).setCellValue("Part No");
        rowhead.createCell((short) 2).setCellValue("Part Description");
        int index = 1;
        while (rs.next()) {

            HSSFRow row = sheet.createRow((short) index);

            row.createCell((short) 0).setCellValue(rs.getString("site"));
            row.createCell((short) 1).setCellValue(rs.getString("part_no"));
            row.createCell((short) 1).setCellValue(rs.getString("description"));
            index++;
            }           


        FileOutputStream fileOut = new FileOutputStream("F:\\IFS\\IFSDOC" +    txtPLCode.getText() + "[" + currentDate + "-" + usesecond + "]" + ".xls");
        wb.write(fileOut);
        fileOut.close();
        System.out.println("Data is saved in excel file.");
        //  rs.close();
4

1 回答 1

0
sheet.autoSizeColumn(0) //for first column
sheet.autoSizeColumn(1) //for second column ... etc

参考: http: //poi.apache.org/spreadsheet/quick-guide.html#Autofit

于 2013-06-13T12:59:10.473 回答