zoukankan      html  css  js  c++  java
  • POI解析Excel代码

    // 批量区域数据导入
        @Action(value = "area_batchImport")
        public String batchImport() throws IOException {
            List<Area> areas = new ArrayList<Area>();
            // 编写解析代码逻辑
            // 基于.xls 格式解析 HSSF
            // 1、 加载Excel文件对象
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
            // 2、 读取一个sheet
            HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
            // 3、 读取sheet中每一行
            for (Row row : sheet) {
                // 一行数据 对应 一个区域对象
                if (row.getRowNum() == 0) {
                    // 第一行 跳过
                    continue;
                }
                // 跳过空行
                if (row.getCell(0) == null
                        || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
                    continue;
                }
                Area area = new Area();
                area.setId(row.getCell(0).getStringCellValue());
                area.setProvince(row.getCell(1).getStringCellValue());
                area.setCity(row.getCell(2).getStringCellValue());
                area.setDistrict(row.getCell(3).getStringCellValue());
                area.setPostcode(row.getCell(4).getStringCellValue());
                // 基于pinyin4j生成城市编码和简码
                String province = area.getProvince();
                String city = area.getCity();
                String district = area.getDistrict();
                province = province.substring(0, province.length() - 1);
                city = city.substring(0, city.length() - 1);
                district = district.substring(0, district.length() - 1);
                // 简码
                String[] headArray = PinYin4jUtils.getHeadByString(province + city
                        + district);
                StringBuffer buffer = new StringBuffer();
                for (String headStr : headArray) {
                    buffer.append(headStr);
                }
                String shortcode = buffer.toString();
                area.setShortcode(shortcode);
                // 城市编码
                String citycode = PinYin4jUtils.hanziToPinyin(city, "");
                area.setCitycode(citycode);
    
                areas.add(area);
            }
            // 调用业务层
            areaService.saveBatch(areas);
    
            return NONE;
        }
  • 相关阅读:
    JuiceSSH:安卓平台免费好用的 SSH 客户端
    git&github-本地库推送到远程库
    git&githib-给远程库取别名
    Git分支管理的本质
    MySQL学习笔记(一)--逻辑架构学习
    mysql-主从备份问题小结
    Docker--数据管理之Volumes
    初识OpenSSH--1
    一个最简单的Dockfile实践
    构词法2
  • 原文地址:https://www.cnblogs.com/niwotaxuexiba/p/10336185.html
Copyright © 2011-2022 走看看