从源(输入)excel复制列数据并写入目标excel

时间:2014-03-05 10:52:40

标签: java apache-poi

我有一个已经有数据的源excel。现在我想从col1,col3和col5获取数据并从下到上写入目标excel(意味着标题位于dest.excel表格中间的某处,我的数据应该从上面的行而不是下面的行写入。) 目前,我可以从col1获取数据并存储在列表中并打印,但不知道如何将其保存到目标excel。

这是两个问题

1)如何从col1,col4,col8等获取列数据 2)如何在另一个excel中打印该数据

提前致谢。

以下是代码

    try {
        //sample excel file
        FileInputStream fileIn = new     FileInputStream("D:/ExportSample.xlsm");


        //workbook
        XSSFWorkbook wb = new XSSFWorkbook(fileIn);

        //open sheet at 0
        XSSFSheet sheet = wb.getSheetAt(0);


         //search for column index containing string "REV No." in the row 0 (which is first row of a worksheet
        String columnWanted = "REV No.";
        Integer columnNo = null;
        //output all not null values to the list
        List<Cell> cells = new ArrayList<Cell>();

        Row firstRow = sheet.getRow(2);

        for(Cell cell:firstRow){
            if (cell.getStringCellValue().equals(columnWanted)){

                columnNo = cell.getColumnIndex();
            }
        }

        if (columnNo != null){
        for (Row row : sheet) {
           Cell c = row.getCell(columnNo);
          // final String[] array = cells.toArray(new String[cells.size()]);
           if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
              // Nothing in the cell in this row, skip it
           } else {
              cells.add(c);
           }
        }
        System.out.println("Column values : "+cells.toString());


        }else{
            System.out.println("could not find column " + columnWanted + " in first row of " + fileIn.toString());
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

输出 柱值:[REV编号,RA00012A,RA00013A,RA00014A]

0 个答案:

没有答案