尝试在Java中设置Excel中的cellvalue时,单元格值不会复制到正确的行(POI API)

时间:2016-06-28 05:43:13

标签: java excel apache-poi

我有2个长度不一的数组,希望能够从B3和D3开始向下插入数据,这些是v8columnname和v9columnname。我还有2个已经填充的名为v8tableNames和v9tableNames。我从两个不同的数据库查询我的arraylists,我计划比较VBA中的两行,但我已经设置了这个宏。我只是坚持让行完全对齐,因为现在代码的B列从B3开始,但D列从B行的最后一个索引开始。非常感谢任何帮助

ArrayList v8tableNames = new ArrayList();
ArrayList v9tableNames = new ArrayList();
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet;
XSSFRow row;

ArrayList v8columns = new ArrayList();
ArrayList v9columns = new ArrayList();
for (int i = 0; i<5; i++) {
        sheet = workbook.createSheet();
        row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("Version 8");
        row = sheet.getRow(0);
        cell = row.createCell(2);
        cell.setCellValue("Version 9");
        row = sheet.getRow(0);
        cell = row.createCell(3);
        cell.setCellValue(v9tableNames.get(i).toString());

        //getv9 is a function that returns column names from v8table

        v9columns = getv9Columns(v9tableNames.get(i).toString());

        row = sheet.getRow(0);

        //set C1 to v9tablename, one value from the array
        cell = row.createCell(3);
        cell.setCellValue(v9tableNames.get(i).toString());

        //loop through all table names on v9 to see if there is a match for v8
        for (int count = 0; count<v9tableNames.size();count++) {
        //checks if there is a match in table names
            if (v9tableNames.get(i).toString().equals(v8tableNames.get(count).toString())) {
                //puts v8 table name in B1
                cell = row.createCell(1);
                cell.setCellValue(v8tableNames.get(count).toString());

                //getv8 is a function that returns table names

                v8columns = getv8Columns(v8tableNames.get(count).toString());

                for (int k = 0; k<v8columns.size() || k<v9columns.size();k++) {
                    //would like to put columnnames from v8table here starting at B3
                    row = sheet.createRow(k+2);
                    cell = row.createCell(1);
                    cell.setCellValue(v8columns.get(k).toString());
                }
            }
        }


        //put v9columns into D3, this is where it gets stuck putting the last value into D(lastindex of B)
        for (int m = 0; m<v9columns.size();m++) {
                    cell = row.createCell(3);
                    cell.setCellValue(v9columns.get(m).toString());
        }
    }
    try {
        FileOutputStream out = new FileOutputStream( 
              new File("Connect.xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("Workbook.xlsx written successfully" );
    } catch(Exception e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:1)

您需要更新输入v9columns for循环的'row':

for (int m = 0; m<v9columns.size();m++) {
    // Start at row zero and shift to row 3, then increment by m++ on each iteration
    row = sheet.createRow(0+m+2);
    cell = row.createCell(3);
    cell.setCellValue(v9columns.get(m).toString());
}

另外,我注意到'row = sheet.getRow(0);'的多余使用在您的代码中,在某些情况下,行已指向sheet.getRow(0)。它可以帮助您在您的行/单元创建/获取的位置注释代码,以指示您正在寻址哪个列或行。