Java 如何使用 Apache POI 将现有 Excel 工作表中的一行复制到新的 Excel 工作表中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22627691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:56:48  来源:igfitidea点击:

How to copy a row from existing excel sheet to a new excel sheet using Apache POI?

javaexcelapache-poi

提问by jake295

I want to compare two excel sheets and find if any row differs, if it does I want to copy that single row which differs from that excel to a new row in a new excel. Below is a code snippet I took from the net and tried but the problem if I copy the 10th row of the existing excel(for example) to the first row of new excel, 1st row is properly copied in new excel but all the remaining rows from first excel are also copied to the new one. I think the problem is with the way I write to new excel [workbook.write(out)] Please help!! Thanks in advance!!

我想比较两个 excel 表并查找是否有任何行不同,如果有,我想将与该 excel 不同的单行复制到新 excel 中的新行。下面是我从网上获取并尝试过的代码片段,但问题是如果我将现有 excel 的第 10 行(例如)复制到新 excel 的第一行,第一行在新 excel 中正确复制,但所有其余行从第一个 excel 也复制到新的。我认为问题出在我写新 excel [workbook.write(out)] 的方式上,请帮忙!!提前致谢!!

    public class RowCopy {

public static void main(String[] args) throws Exception{
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("c:/input.xls"));
    HSSFSheet sheet = workbook.getSheet("Sheet1");
    copyRow(workbook, sheet, 0, 1);
    FileOutputStream out = new FileOutputStream("c:/output.xls");
    workbook.write(out);
    out.close();
}

private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, int sourceRowNum, int destinationRowNum) {
    // Get the source / new row
    HSSFRow newRow = worksheet.getRow(destinationRowNum);
    HSSFRow sourceRow = worksheet.getRow(sourceRowNum);

    // If the row exist in destination, push down all rows by 1 else create a new row
    if (newRow != null) {
        worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {
        newRow = worksheet.createRow(destinationRowNum);
    }

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        HSSFCell oldCell = sourceRow.getCell(i);
        HSSFCell newCell = newRow.createCell(i);

        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;
            continue;
        }

        // Copy style from old cell and apply to new cell
        HSSFCellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        ;
        newCell.setCellStyle(newCellStyle);

        // If there is a cell comment, copy
        if (oldCell.getCellComment() != null) {
            newCell.setCellComment(oldCell.getCellComment());
        }

        // If there is a cell hyperlink, copy
        if (oldCell.getHyperlink() != null) {
            newCell.setHyperlink(oldCell.getHyperlink());
        }

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());

        // Set the cell data value
        switch (oldCell.getCellType()) {
            case Cell.CELL_TYPE_BLANK:
                newCell.setCellValue(oldCell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                newCell.setCellValue(oldCell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                newCell.setCellErrorValue(oldCell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                newCell.setCellFormula(oldCell.getCellFormula());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                newCell.setCellValue(oldCell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                newCell.setCellValue(oldCell.getRichStringCellValue());
                break;
        }
    }
}

采纳答案by Angga

Just modify the copyRowmethod, add parameter HSSFSheet resultSheetthen modify newRowvariable in the method to get that from resultSheetlike this

只需修改copyRow方法,添加参数HSSFSheet resultSheet然后修改newRow方法中的变量即可从resultSheet这样获得

private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, HSSFSheet resultSheet, int sourceRowNum, int destinationRowNum) {
    // Get the source / new row
    HSSFRow newRow = resultSheet.getRow(destinationRowNum);
    HSSFRow sourceRow = worksheet.getRow(sourceRowNum);

take that resultSheet from your destination "output.xls"

从您的目的地“output.xls”中获取该结果表