POI操作本地excel

private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

    /**
     * 判断文件是否是excel
     * 
     * @throws Exception
     */
    public static void checkExcelVaild(File file) throws Exception {
        if (!file.exists()) {
            throw new Exception("文件不存在");
        }
        if (!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))) {
            throw new Exception("文件不是Excel");
        }
    }

    public static void main(String[] args) throws Exception {
        // C:\Users\lqf\Desktop\import.xlsx
        String path = "C://Users//lqf//Desktop//import.xlsx";
        List<Object> list = new ArrayList<>();
        try {
            // 同时支持Excel 2003、2007
            File excelFile = new File(path); // 创建文件对象
            FileInputStream in = new FileInputStream(excelFile); // 文件流
            checkExcelVaild(excelFile);
            Workbook workbook = WorkbookFactory.create(in); // 这种方式
            Sheet sheet = workbook.getSheetAt(0); // 遍历第一个Sheet
            Object o = new Object();
            for (Row row : sheet) {
                for (int i = 0; i < row.getLastCellNum(); i++) {
                    Cell cell = row.getCell(i);
                    int cellType = cell.getCellType();
                    switch (cellType) {
                    case 0:
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                            o = cell.getDateCellValue();
                        } else if (cell.getCellStyle().getDataFormat() == 58) {
                            double value = cell.getNumericCellValue();
                            o = DateUtil.getJavaDate(value);
                        } else {
                            o = cell.getNumericCellValue();
                        }
                        break;
                    case 1:
                        o = cell.getStringCellValue();
                        break;
                    case 2:
                        o = cell.getCellFormula();
                        break;
                    case 3:
                        o = "";
                        break;
                    case 4:
                        o = cell.getBooleanCellValue();
                        break;
                    default:
                        // cell.setCellType(1);
                        o = cell.getStringCellValue();
                        break;
                    }
                    // 如果要修改笨本地的excel
                    /*
                     * cell.setCellValue(""); FileOutputStream excelFileOutPutStream = new
                     * FileOutputStream(path);// 写数据到这个路径上 workbook.write(excelFileOutPutStream);
                     */
                    list.add(o);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            for (Object o : list) {
                System.out.println(o);
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值