search

Home  >  Q&A  >  body text

java用poi操作excel的时候,new XSSFWorkbook,总是报错?

走到箭头那一步的时候,总是调到handlerMehtodInvoker.class 这里
为什么捕捉不到异常?

PHP中文网PHP中文网2822 days ago889

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 09:45:25

    Looking at your code, it seems that what you implement is to read Excel files. The code is as follows: The specific relationship is related to the EXCEL version supported by your POI.
    public List<RackVo> importExcel(@File("upload")FileItem fileInput, ErrorContext error) {
    List<RackVo> list = null;

        if (null == fileInput) {
            throw new ServiceException(ErrorCode.Params_Lost, "机柜导入文件");
        }
        Workbook wb = null;
        InputStream is = null;
        Sheet sheet = null;
        try {
            is = fileInput.getInputStream();
            wb = new XSSFWorkbook(is);
            sheet = wb.getSheet(sheetName);
        } catch (Exception e) {
            throw new ServiceException(ErrorCode.Upload_File_Error, "上传excel版本文件解析失败");
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    
        if (sheet != null) {
            // 初始化Excel栏目
            List<CellMapping> mappingList = RackUtil.getModColumns(null);
            try {
                list = ExcelUtils.excel2bean(sheet, RackVo.class, mappingList);
            } catch (Exception e) {
                throw new ServiceException(ErrorCode.Upload_File_Error, "Excel解析失败");
            }
        } else {
            throw new ServiceException(ErrorCode.Upload_File_Error, "未找到模板对应sheet");
        }
    
        return list;

    }

    reply
    0
  • Cancelreply