Java reads Excel and merges data of desired columns in table

HSSFWorkbook: It is the version before Excel2003 (including 2003), the extension is .xls

XSSFWorkbook: It is the version that operates Excel2007, and the extension is .xlsx

For different versions of EXCEL documents, different tool classes should be used. Prompt the following error message.

org.apache.poi.openxml4j.exceptions.InvalidOperationException

org.apache.poi.poifs.filesystem.OfficeXmlFileException

.xlsx file supports 1048576 lines, .xls only supports 65535 lines (Row) Packet processing to prevent unnecessary situations such as memory overflow caused by too much data.
In addition, when taking the value, the data type of each column to be considered, the value of the string corresponding to the string, and the value of the number corresponding to the number, can be judged according to the CellType.

public class Sunny {

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        // TODO Auto-generated method stub
        String path = "d:/sunny"; // Store excel file path

        XSSFWorkbook hwb = new XSSFWorkbook();

        // set workbook
        XSSFSheet sunnySheet = hwb.createSheet("sunny oops!");
        // set header
        XSSFRow firstrow = sunnySheet.createRow(0);

        firstrow.createCell(0).setCellValue("transaction date");
        firstrow.createCell(1).setCellValue("Summary");
        firstrow.createCell(2).setCellValue("Debit Balance");
        firstrow.createCell(3).setCellValue("Credit Balance");
        firstrow.createCell( 4).setCellValue("Balance");
        firstrow.createCell(5).setCellValue("Voucher Serial Number");
        firstrow.createCell(6).setCellValue("Business Organization Number");
        firstrow.createCell(7).setCellValue( "Name of the other party");
        firstrow.createCell(8). setCellValue("the other party's account");

        File f = new File(path);
        File fa[] = f.listFiles();
        List<Row> rowList = new ArrayList<Row>();
        for (int i = 0; i < fa.length; i++) {
            File targetFile = fa[i];
            System.out.println(targetFile.getName());
            Workbook book = null;
            try {
                InputStream is = new FileInputStream(targetFile);
                book = new HSSFWorkbook(is);
            } catch (Exception e) {
                e.getStackTrace();
            }
            Sheet sheet = book.getSheetAt(0);
            // 得到总行数
            int rowNum = sheet.getLastRowNum();
            for (int j = 3; j <= rowNum; j++) {
                Row row = sheet.getRow(j);
                String cellValue = getCellValueWithString(row.getCell(2));
                if (!(cellValue.equals("0")||cellValue.equals("0.0")||cellValue.equals("0.00"))) {
//                    String cell = row.getCell(1).getStringCellValue();
                    rowList.add(row);
//                    System.out.println(cell);
                }else{
                    System.out.println("XXX");
                }
            }
        }
        System.out.println(rowList.size());
        int k = 1;
        for (Row row : rowList) {
            XSSFRow createRow = sunnySheet.createRow(k);
            createRow.createCell(0).setCellValue(row.getCell(0).getStringCellValue());
            createRow.createCell(1).setCellValue(row.getCell(1).getStringCellValue());
            Cell tmpCell2 = row.getCell(2);
            Cell tmpCell3 = row.getCell(3);
            switch (tmpCell2.getCellType()) {
                case HSSFCell.CELL_TYPE_STRING:
                    createRow.createCell(2).setCellValue(tmpCell2.getStringCellValue());
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    createRow.createCell(2).setCellValue(tmpCell2.getNumericCellValue());
                    break;
                default:
                    createRow.createCell(2).setCellValue(tmpCell2.getNumericCellValue());
            }
            switch (tmpCell3.getCellType()) {
                case HSSFCell.CELL_TYPE_STRING:
                    createRow.createCell(3).setCellValue(tmpCell3.getStringCellValue());
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    createRow.createCell(3).setCellValue(tmpCell3.getNumericCellValue());
                    break;
                default:
                    createRow.createCell(3).setCellValue(tmpCell3.getNumericCellValue());
            }
            createRow.createCell(4).setCellValue(row.getCell(4).getStringCellValue());
            createRow.createCell(5).setCellValue(row.getCell(5).getStringCellValue());
            createRow.createCell(6).setCellValue(row.getCell(6).getStringCellValue());
            createRow.createCell(7).setCellValue(row.getCell(7).getStringCellValue());
            createRow.createCell(8).setCellValue(row.getCell(8).getStringCellValue());
            k++;
        }

        try {
            hwb.write(new FileOutputStream("d:\\sunny\\sunny_8-9.xls"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("合并代扣耗时{"+ (System.currentTimeMillis() - startTime)+"}ms");
    }

    private static String getCellValueWithString(Cell cell) {
        if (cell == null) {
            return "";
        } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
            return String.valueOf(cell.getNumericCellValue());
        } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
            return cell.getStringCellValue().trim();
        } else {
            return "";
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326357306&siteId=291194637