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

尝试从Java(POI API)在excel中设置cellvalue时,单元格值未复制到正确的行中,java,excel,apache-poi,Java,Excel,Apache Poi,我有两个数组,长度不同,希望能够插入从B3和D3开始向下的数据,它们是v8columnname和v9columnname。我还有两个名为v8tableNames和v9tableNames的表名,它们已经填充了。我的ArrayList是从两个不同的数据库查询的,我计划在VBA中比较这两行,但我已经设置了宏。我只想让行正确对齐,因为代码现在的B列从B3开始,而D列从B行的最后一个索引开始。非常感谢您的帮助 ArrayList v8tableNames = new ArrayList(); Array

我有两个数组,长度不同,希望能够插入从B3和D3开始向下的数据,它们是v8columnname和v9columnname。我还有两个名为v8tableNames和v9tableNames的表名,它们已经填充了。我的ArrayList是从两个不同的数据库查询的,我计划在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();
    }
ArrayList v8tableNames=new ArrayList();
ArrayList v9tableNames=新的ArrayList();
XSSFWorkbook工作簿=新XSSFWorkbook();
XSSF表;
XSSFRow行;
ArrayList v8columns=新的ArrayList();
ArrayList v9columns=新的ArrayList();

对于(int i=0;i您需要更新进入v9columns for循环的“行”:

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());
}
for(int m=0;m