poi简单读取excel内容

public void testIn() {
        try {
            // 读取Excel中数据
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("D:\\test.xls"));
            HSSFSheet sheet = workbook.getSheetAt(0);

            HSSFRow row = null;
            HSSFCell cell = null;

            int rowIndex = 0;

            while (true) {

                String name = null;
                row = sheet.getRow(rowIndex);
                if (row == null) {
                    break;
                }
                cell = row.getCell(0);
                if (cell != null) {
                    name = cell.getStringCellValue();
                }

                String code = null;
                cell = row.getCell(1);
                if (cell != null) {
                    code = cell.getStringCellValue();
                }

                System.out.println(name + " " + code);

                rowIndex++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

你可能感兴趣的:(java,Excel)