读取Excel文件并使用POI在特定工作表中写入其他Excel

时间:2018-01-11 10:28:18

标签: java excel apache-poi

我正在阅读Excel文件,并根据工作表名称将内容写入已存在的文件中。 Excel文件有3个标题列。

XSSFSheet conSheet = workbook.getSheet("EMPLOYYEE");
XSSFRow headerRow = conSheet.getRow(0);
XSSFRow dataRow = conSheet.createRow(conSheet.getLastRowNum() + 1);
int physicalNumberOfCells = headerRow.getPhysicalNumberOfCells();
for (int i = 0; i < physicalNumberOfCells; i++) {
    XSSFCell headerCell = headerRow.getCell(i);
    if (headerCell != null) {
        String stringCellValue = headerCell.getStringCellValue();
        ConvertsColumn charcol = ConvertsColumn.getEnum(stringCellValue);
        XSSFCell dataCell = dataRow.createCell(i);
        if (charcol != null) {
            try {
                FileInputStream file = new FileInputStream("file..anme");
                Workbook wb1 = new XSSFWorkbook(file);
                Sheet sheet1 = wb1.getSheetAt(0);
                Iterator < Row > iterator = sheet1.iterator();
                while (iterator.hasNext()) {
                    Row nextRow = iterator.next();
                    Iterator < Cell > cellIterator = nextRow.cellIterator();
                    while (cellIterator.hasNext()) {
                        Cell nextCell = cellIterator.next();
                        int columnIndex = nextCell.getColumnIndex();
                        System.out.println(columnIndex + "" + nextCell);
                        switch (columnIndex) {
                            case 0:
                                dataCell.setCellValue(nextCell.getStringCellValue());
                                break;
                            case 1:
                                dataCell.setCellValue(nextCell.getStringCellValue());
                                break;
                            case 2:
                                dataCell.setCellValue(nextCell.getStringCellValue());
                                break;
                            default:
                                break;
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

我们正在使用POI。当数据写入Excel文件时,它会覆盖第一列,只有一个列数据添加一个值......

0 个答案:

没有答案