删除重复行后,无法使用Apache POI编写新的Excel

时间:2017-09-23 17:10:44

标签: java excel apache apache-poi

我是Apache POI的新手。

我编写了一个用于从excel文件中删除重复记录的小代码。我成功地能够识别表格中的重复记录,但在删除记录后写入新文件时,不会生成任何输出。

请帮助我出错的地方?

我写得好吗?或者我错过了什么?

public static void main(String args[]) {
    DataFormatter formatter = new DataFormatter();
    HSSFWorkbook input_workbook;
    HSSFWorkbook workbook_Output_Final;

    HSSFSheet input_workbook_sheet;

    HSSFRow row_Output;
    HSSFRow row_1_index;
    HSSFRow row_2_index;

    String value1 = "";
    String value2 = "";
    int count;


    //main try catch block starts
    try {

        FileInputStream input_file = new FileInputStream("E:\\TEST\\Output.xls"); //reading from input file
        input_workbook = new HSSFWorkbook(new POIFSFileSystem(input_file));

        for (int sheetnum = 0; sheetnum < input_workbook.getNumberOfSheets(); sheetnum++) { //traversing  sheets

            input_workbook_sheet = input_workbook.getSheetAt(sheetnum);

            int input_workbook_sheet_total_row = input_workbook_sheet.getLastRowNum(); //fetching last row nmber

            for (int input_workbook_sheet_row_1 = 0; input_workbook_sheet_row_1 <= input_workbook_sheet_total_row; input_workbook_sheet_row_1++) { //traversing row 1

                for (int input_workbook_sheet_row_2 = 0; input_workbook_sheet_row_2 <= input_workbook_sheet_total_row; input_workbook_sheet_row_2++) {

                    row_1_index = input_workbook_sheet.getRow(input_workbook_sheet_row_1);    //fetching one iteration row index
                    row_2_index = input_workbook_sheet.getRow(input_workbook_sheet_row_2);    //fetching sec iteration row index

                    if (row_1_index != row_2_index) {
                        count = 0;
                        value1 = "";
                        value2 = "";
                        for (int row_1_index_cell = 0; row_1_index_cell < row_1_index.getLastCellNum(); row_1_index_cell++)  { //traversing cell for each row
                            try {
                                value1 = value1 + formatter.formatCellValue(row_1_index.getCell(row_1_index_cell)); //fetching  row cells value
                                value2 = value2 + formatter.formatCellValue(row_2_index.getCell(row_1_index_cell)); //fetching row cells value 

                            } catch (NullPointerException e) {
                            }
                            count++;
                            if (count == row_1_index.getLastCellNum()) {

                                if (value1.hashCode() == value2.hashCode()) { //remove the duplicate logic  
                                    System.out.println("deleted : " + row_2_index);
                                    System.out.println("------------------");
                                    input_workbook_sheet.removeRow(row_2_index);
                                }

                            }
                        }

                    }
                }
            }

        }
        FileOutputStream fileOut = new FileOutputStream("E:\\TEST\\workbook.xls");
        input_workbook.write(fileOut);
        fileOut.close();
        input_file.close();
    } catch (Exception e) {
        //e.printStackTrace();
    }
    //main try catch block ends

}

1 个答案:

答案 0 :(得分:1)

有几点需要注意:

  1. 你吞下任何一种例外; Igotsome nullpointers与我的测试数据,这将阻止编写工作簿

  2. 删除行时,向后移动行号是一个老技巧,因为您不必调整刚刚删除的行号

  3. 代码清空行,但它不会向上移动所有行(=删除后有一个间隙)。如果您想消除这种差距,可以使用shiftRows

  4. 你通过哈希码比较事物,这是可能的(在某些用例中),但我觉得.equals()就是你想要做的。另请参阅Relationship between hashCode and equals method in Java

  5. 这里有一些适用于我的测试数据的代码,如果某些内容无法处理您的数据,请随时发表评论:

    public static void main(String args[]) throws IOException {
        DataFormatter formatter = new DataFormatter();
        HSSFWorkbook input_workbook;
        HSSFWorkbook workbook_Output_Final;
    
        HSSFSheet input_workbook_sheet;
    
        HSSFRow row_Output;
        HSSFRow row_1_index;
        HSSFRow row_2_index;
    
        String value1 = "";
        String value2 = "";
        int count;
    
        FileInputStream input_file = new FileInputStream("c:\\temp\\test.xls");
        input_workbook = new HSSFWorkbook(new POIFSFileSystem(input_file));
    
        for (int sheetnum = 0; sheetnum < input_workbook.getNumberOfSheets(); sheetnum++) {
    
            input_workbook_sheet = input_workbook.getSheetAt(sheetnum);
    
            int input_workbook_sheet_total_row = input_workbook_sheet.getLastRowNum(); 
    
            for (int input_workbook_sheet_row_1 = input_workbook_sheet_total_row; input_workbook_sheet_row_1 >=0; input_workbook_sheet_row_1--) { // traversing
    
                for (int input_workbook_sheet_row_2 = input_workbook_sheet_total_row; input_workbook_sheet_row_2 >= 0 ; input_workbook_sheet_row_2--) {
    
                    row_1_index = input_workbook_sheet.getRow(input_workbook_sheet_row_1);
                    row_2_index = input_workbook_sheet.getRow(input_workbook_sheet_row_2); 
    
                    if (row_1_index != null && row_2_index != null && row_1_index != row_2_index) {
                        count = 0;
                        value1 = "";
                        value2 = "";
    
                        int row_1_max = row_1_index.getLastCellNum() - 1;
                        for (int row_1_index_cell = 0; row_1_index_cell < row_1_max; row_1_index_cell++) {
                            try {
                                value1 = value1 + formatter.formatCellValue(row_1_index.getCell(row_1_index_cell)); 
    
                                value2 = value2 + formatter.formatCellValue(row_2_index.getCell(row_1_index_cell)); 
    
                            } catch (NullPointerException e) {
                                e.printStackTrace();
                            }
                            count++;
    
                            if (value1.equals(value2)) {
                                System.out.println("deleted : " + row_2_index.getRowNum());
                                System.out.println("------------------");
                                input_workbook_sheet.removeRow(row_2_index);
    
    
                                input_workbook_sheet.shiftRows(
                                        row_2_index.getRowNum() + 1, 
                                        input_workbook_sheet_total_row, 
                                        -1, 
                                        true, 
                                        true);
                            }
    
    
                        }
    
                    }
                }
            }
    
        }
        FileOutputStream fileOut = new FileOutputStream("c:\\temp\\workbook.xls");
        input_workbook.write(fileOut);
        fileOut.close();
        input_file.close();
        input_workbook.close();
    }