Apache的POI autoSizeColumn()不工作的权利 [英] Apache POI autoSizeColumn() not working right

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

问题描述

我创建到使用Apache POI一个excel文件中写入信息的程序。我输入我的数据到文件后,我呼吁文件的每一列autoSizeColumn方法。但它调整大小的列输入的最后一个细胞,其是有时不一样大列中的其他单元的宽度。我知道我使用的是正确的,不幸的是我没有上网,现在张贴任何code,但我会在我可以更新。

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.

好吧,我希望我现在用的是code标记正确的,但在这里它是:

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.

**注意连接类是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);

在你的code点的数据已经被添加了。

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

下面是对<一个链接href=\"http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html#autoSizeColumn%28int%29\">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天全站免登陆