解析excel

/**
 * 将上传文件解析,按逻辑入库
 * <p>
 * 2017年9月4日 16:07:28
 * xj
 *
 * @param request 请求体
 */
public void importCompanyIndustryTemplate(HttpServletRequest request) {
    // 检查上传文件
    MultipartFile file = Util.checkFile(request);
    Workbook workbook = null;
    try {
        InputStream in = file.getInputStream();
        workbook = WorkbookFactory.create(in);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } catch (InvalidFormatException e) {
        logger.error(e.getMessage(), e);
    }
    // 行业字典表
    List<Industry> list = industryRepository.findAll();
    // excel信息合集
    Map<Integer, String> map = new HashMap<Integer, String>();
    String str = "";
    // 遍历excel,获取sheet信息
    for (int k = 0; k < workbook.getNumberOfSheets(); k++) {
        // 获取指定sheet信息
        Sheet sheet = workbook.getSheetAt(k);
        // 获取第一行信息
        Row row = sheet.getRow(0);
        // 获取sheet总列数
        int colNum = row.getPhysicalNumberOfCells();
        // 获取sheet总行数
        int rowNum = sheet.getLastRowNum();
        // 遍历每行信息
        for (int i = 1; i <= rowNum; i++) {
            row = sheet.getRow(i);
            boolean flag = isBlankRow(row);
            if (!flag) {
                int j = 0;
                // 将表格中信息按格式存入map
                while (j < colNum) {
                    // 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
                    // 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
                    str += ExcelUtil.getCellFormatValue(row.getCell((short) j)).trim() + "-";
                    j++;
                }
                map.put(i, str);
                str = "";
            }
        }
        saveCompanyIndustryTemplate(list, map);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_34117825/article/details/78399613
今日推荐