Apache POI autoSizeColumn() 无法正常工作 [英] Apache POI autoSizeColumn() not working right

查看:49
本文介绍了Apache POI autoSizeColumn() 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用 apache poi 将信息写入 excel 文件的程序.在我将所有数据输入文件后,我在文件的每一列上调用 autoSizeColumn 方法.但它会将列的大小调整为输入的最后一个单元格的宽度,该宽度有时不如列中的其他单元格大.我知道我正在正确使用它,不幸的是我现在没有互联网来发布任何代码,但我会尽可能更新.

I am creating a program that writes information to an excel file using the apache poi. After I enter all of my data into the file I call the autoSizeColumn method on every column of the file. But it resizes the columns to the width of the last cell entered which is sometime not as big as the other cells in the column. I know I'm using it right and unfortunately I don't have Internet right now to post any code but I will update when I can.

好的,我希望我正确使用代码标签,但它是:

Ok I hope I am using the code tags right but here it is :

public void writeFile() {
    Workbook wb = new HSSFWorkbook();
    Sheet s = wb.createSheet();
    try {

        FileOutputStream out = new FileOutputStream(
                this.getSelectedFile());
        // create a new workbook

        // create a new sheet

        // declare a row object reference
        Row r = null;
        // declare a cell object reference
        Cell c = null;
        // in case of plain ascii
        wb.setSheetName(0, "Street Light Report");
        // create a sheet with 30 rows (0-29)
        // int rownum;
        // short rownum;
        // PrintWriter printOut = new PrintWriter(
        // this.getSelectedFile());
        String[] colName = { "Light Id", "Flagged",
                "Malfunction", "Comments", "Location", "Date" };
        // printOut.println("Light Id,Flagged,Malfunction,Comments,Location,Date");
        Connections.connect();
        String[][] data = Connections.searchForFlagged();
        for (int i = 0; i < data.length; i++) {
            r = s.createRow(i);
        }
        r.setRowNum(0);
        for (int j = 0; j < 6; j++) {
            c = r.createCell(j);
            switch (j) {
            case 0:
                c.setCellValue(colName[j]);
                break;
            case 1:
                c.setCellValue(colName[j]);
                break;
            case 2:
                c.setCellValue(colName[j]);
                break;
            case 3:
                c.setCellValue(colName[j]);
                break;
            case 4:
                c.setCellValue(colName[j]);
                break;
            case 5:
                c.setCellValue(colName[j]);
                break;

            }
        }

        for (int i = 0; i < data.length; i++) {

            r.setRowNum(i + 1);
            for (int j = 0; j < data[i].length; j++) {

                c = r.createCell(j);

                // System.out.println(data[i][j]);

                switch (j) {
                case 0:
                    c.setCellType(Cell.CELL_TYPE_NUMERIC);
                    c.setCellValue(Integer.parseInt(data[i][j]));
                    break;
                case 1:
                    if (data[i][j].equals("true"))
                        c.setCellValue("Yes");
                    else
                        c.setCellValue("No");
                    break;
                case 2:
                    if (data[i][j].equals("I"))
                        c.setCellValue("Intermittent");
                    else
                        c.setCellValue("Not Functional");
                    break;
                case 3:
                    c.setCellValue(data[i][j]);
                    break;
                case 4:
                    c.setCellValue(data[i][j]);
                    break;
                case 5:
                    c.setCellValue(data[i][j]);
                    break;
                }

            }
        }
                                wb.getSheetAt(0).setPrintGridlines(true);
        for (int j = 0; j < 6; j++) {
            s.autoSizeColumn(j);
        }
        wb.write(out);
        out.close();
    } catch (Exception ex) {

    }
};

我会在这里发布图片,但我需要代表.点,我刚从这里开始.

I would post an image here but I need rep. point and I just started here.

** 注意Connections 类是JDBC 类,这样java 就可以从mySQL 数据库中提取信息,它返回一个二维字符串数组**

** Note that the Connections class is the JDBC class so that java can pull the information from th mySQL database it returns a 2D array of strings **

推荐答案

您只需要将呼叫移至

sheet.autoSizeColumn(columnNumber);

添加数据后到代码中的某个点.

to a point in your code after the data has been added.

这是API

这是唯一的方法.我遇到了同样的问题,我在输入数据之前调用了 autoSizeColumn() 方法.跳它有帮助

That's the only way to do it. I had same problem, I was calling the method autoSizeColumn() before I entered the data. Hop it helped

这篇关于Apache POI autoSizeColumn() 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆