public static void copySheet(XSSFSheet inputSheet1, XSSFSheet outputSheet, XSSFSheet inputSheet2)
{
int rowCount = inputSheet1.getLastRowNum();
int rc = inputSheet2.getLastRowNum(),rcnt=0,cri =0,l=0;
int currentRowIndex = 0, cell = 0;
if (rowCount > 0) {
Iterator rowIterator = inputSheet1.iterator();
while (rowIterator.hasNext()) {
int currentCellIndex = 0;
Iterator cellIterator;
Row row = (Row) rowIterator.next();
cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
//Creating new Row, Cell and Input value in the newly created sheet.
String cellData = cellIterator.next().toString();
if (currentCellIndex == 0)
outputSheet.createRow(currentRowIndex).createCell(currentCellIndex).setCellValue(cellData);
else
outputSheet.getRow(currentRowIndex).createCell(currentCellIndex).setCellValue(cellData);
currentCellIndex++;
System.out.println("content test2 " + cellData);
System.out.println("current row index " + currentRowIndex);
System.out.println("current cell " + cell);
}
currentRowIndex++;
cell = currentCellIndex;
rcnt = cell;
}
}
cri = 0;
if (rc > 0) {
Iterator rowIterator = inputSheet2.iterator();
while (rowIterator.hasNext()) {
int currentCellIndex2 = 0,cci = rcnt+1;
Iterator cellIterator2;
Row row = (Row) rowIterator.next();
cellIterator2 = row.cellIterator();
while (cellIterator2.hasNext()) {
String cellData = cellIterator2.next().toString();
if (currentCellIndex2 == 0)
outputSheet.createRow(cri).createCell(cci).setCellValue(cellData);
else
outputSheet.getRow(cri).createCell(cci).setCellValue(cellData);
currentCellIndex2 = currentCellIndex2+1;
cci = cci +1;
System.out.println("content "+cellData);
System.out.println("current row index " + cri);
}
cri++;
}
}
}
预期输出:
这两个工作表都应该复制到新的工作表。
实际输出:
只有第二个输入工作表被复制到新的工作表。如果我尝试复制第一个工作表,它将被复制。但是如果我尝试复制两个,只有第二个工作表会出现。
Input_one
Input_two
输出
看起来您正在用第二个循环的输出覆盖第一个while
循环的输出,因为在一个循环中您使用当前RowIndex
,而在第二个循环中您使用cri
。第二个whis-loop也重用当前RowIndex
,以便第二个工作表中的行被附加在第一个工作表的行之后。