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

查看:200
本文介绍了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数据库中提取信息,返回一个2D数组字符串**

** 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()。 Hop帮助

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天全站免登陆