XSSF parsing

public class TestExcel {

    static Log log = LogFactory.getLog(TestExcel.class);

    private final static int httpPort = 8087;

    

    @Autowired

    PhoneMatchCodeService phoneMatchCodeService;

 

    // Get the path of the Excel document

    public static String filePath = "E://phoneMatchCode//phoneMatchCode.xlsx";

 

    private String saveData() {

        try {

            // Create a reference to the Excel workbook file

            Workbook wookbook = null;

//            OPCPackage pkg = OPCPackage.open(path);

            wookbook = new XSSFWorkbook(new FileInputStream(filePath));

            // HSSFWorkbook wookbook = new HSSFWorkbook(new

            // FileInputStream(filePath));

            // In an Excel document, the default index of the first sheet is 0

            // Its statement is: HSSFSheet sheet = workbook.getSheetAt(0);

            Sheet sheet = wookbook.getSheet("Sheet1");

            // HSSFSheet sheet = wookbook.getSheet("Sheet1");

            // Get all the rows in the Excel file

            int rows = sheet.getPhysicalNumberOfRows();

            // iterate over rows

 

            PhoneMatchCode phoneMatchCode = new PhoneMatchCode();

            for (int i = 1; i < rows; i++) {

                // read the upper left cell

                Row row = sheet.getRow(i);

                // row is not empty

                if (row != null) {

                    // Get all the columns in the Excel file

                    int cells = row.getPhysicalNumberOfCells();

                    String value = ""; // loop through the column

 

                    row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);

                    phoneMatchCode.setCode(row.getCell(0).getStringCellValue());

 

                    row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);

                    phoneMatchCode.setMatchCode(row.getCell(1).getStringCellValue());

 

                    row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);

                    phoneMatchCode.setProvince(row.getCell(2).getStringCellValue());

 

                    row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);

                    phoneMatchCode.setCity(row.getCell(3).getStringCellValue());

 

                    System.out.println(phoneMatchCode.getCity());

                    phoneMatchCodeService.add(phoneMatchCode);

                }

            }

            return "ok";

        } catch (Exception e) {

            log.error("excell parsing failed", e);

            return "error";

        }

    }

}

Guess you like

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