使用Apache POI在XLSM文件之间复制单元格值

时间:2019-09-03 08:43:32

标签: java excel apache-poi xssf

我有两个documentElement文件,并尝试使用XLSM在Java代码中的文件之间复制单元格值。 第一个文件初始化如下:

Apache POI

第二个文件:

File file = new File(GlobalVariables.XLSM);
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(recordcount+4);
Cell cell;

之后,我尝试按预定义条件复制单元格。单元格应该复制到原始文件的不同列中,因此我在代码中为此使用了减法。

File prevfile = new File(PrevResXLS);
FileInputStream prevInputStream = new FileInputStream(prevfile);
XSSFWorkbook prevworkbook = XSSFWorkbookFactory.createWorkbook(prevInputStream);
XSSFSheet prevsheet = prevworkbook.getSheetAt(0);
XSSFRow prevrow = prevsheet.getRow(0);
Cell prevCell;

我得到 for (Row r : prevsheet) { if (r.getRowNum()==0) continue; if(r.getCell(1).toString().equals(trans_filename)){ prevCell = prevrow.getCell(21); prevCell.setCellValue(r.getCell(21-13).toString()); prevCell = prevrow.getCell(22); prevCell.setCellValue(r.getCell(22-13).toString()); prevCell = prevrow.getCell(23); prevCell.setCellValue(r.getCell(23-13).toString()); prevCell = prevrow.getCell(24); prevCell.setCellValue(r.getCell(24-13).toString()); prevCell = prevrow.getCell(25); prevCell.setCellValue(r.getCell(25-13).toString()); prevCell = prevrow.getCell(26); prevCell.setCellValue(r.getCell(26-13).toString()); prevCell = prevrow.getCell(27); prevCell.setCellValue(r.getCell(27-13).toString()); prevCell = prevrow.getCell(28); prevCell.setCellValue(r.getCell(28-13).toString()); prevCell = prevrow.getCell(29); prevCell.setCellValue(r.getCell(29-13).toString()); prevCell = prevrow.getCell(30); prevCell.setCellValue(r.getCell(30-13).toString()); prevCell = prevrow.getCell(31); prevCell.setCellValue(r.getCell(31-13).toString()); prevCell = prevrow.getCell(32); prevCell.setCellValue(r.getCell(32-13).toString()); prevCell = prevrow.getCell(33); prevCell.setCellValue(r.getCell(33-13).toString()); } } ,但是文件,工作表和单元格都存在。

0 个答案:

没有答案