简体   繁体   English

使用具有不同列宽的Java生成Excel文件

[英]Generate excel file using java with different column width

I am doing a java application (J2SE) which I have to generate a excel document (.xls). 我正在做一个Java应用程序(J2SE),我必须生成一个Excel文档(.xls)。 I did generate the file but I am not able to change the cell width dynamically according to the content which is having the maximum length in the same cell. 我确实生成了文件,但是我无法根据同一单元格中具有最大长度的内容动态地更改单元格宽度。

Here is my coding... 这是我的编码...

     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();
sheet.autoSizeColumn(0) //for first column
sheet.autoSizeColumn(1) //for second column ... etc

Refer: http://poi.apache.org/spreadsheet/quick-guide.html#Autofit 请参阅: http : //poi.apache.org/spreadsheet/quick-guide.html#Autofit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM