Java上传Excel文件

通过MultipartFile 获取Excel文件

    public Rest upload(HttpServletRequest request, HttpServletResponse response,
            @RequestParam("file") MultipartFile file) throws IOException, ServletException {
        Map<String, Object> upload = excelUploadService.upload(request, response, file);
        List<Map<String, String>> list = new ArrayList<Map<String,String>>();
        if(upload.containsKey("error")){
            return Rest.info(StateCode.STATUS_CODE_FAILURE, "operation failed");
        }else if(upload.containsKey("success")){
            list = (List<Map<String, String>>) upload.get("success");
            return Rest.item(StateCode.STATUS_CODE_SUCCESS, list);
        }
        return null;
    }

判断Excel文件版本,并将数据封装成Map

    public Map<String, Object> upload(HttpServletRequest request,
            HttpServletResponse response, MultipartFile file) 
            throws IOException, ServletException {
        String info = FileUtils.isLegal(file);
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, String>> list = new ArrayList<Map<String,String>>();
        if(info.equals("")){
            String post_fix = FileUtils.getPostFix(file.getOriginalFilename());
            ExcelVersion ev = null;
            if(post_fix.toLowerCase().equals("xls")){
                ev = ExcelVersion.OFFICE_EXCEL_2003;
            }else if(post_fix.toLowerCase().equals("xlsx")){
                ev = ExcelVersion.OFFICE_EXCEL_2007;
            }
            list = ExcelUploadServiceImpl.getData(file.getInputStream(), ev);
            map.put("success", list);
        }else{
            map.put("error", info);
        }
        return map;
    }

getData()方法,判断版本,获取数据

    public static List<Map<String, String>> getData(InputStream inputStream, ExcelVersion version) throws IOException {
        ExcelParser parser  = null;
        List<Map<String, String>>  data = new ArrayList<Map<String,String>>();
        switch (version) {
        case OFFICE_EXCEL_2003:
            parser = new HExcelParser();
            data = parser.parse(inputStream);
            break;
        case OFFICE_EXCEL_2007:
            parser = new XExcelParser();
            data = parser.parse(inputStream);
            break;
        default:
            break;
        }
        //遍历所有数据,把没有值的赋值null
        List<Map<String, String>> temList = fillNull(data);
        return temList;
    }

parse()方法,拆解Excel,封装数据。

    public List<Map<String, String>> parse(InputStream inputStream) throws IOException {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }

            // 循环行Row
            for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow == null) {
                    continue;
                }
                Map<String, String> map = new LinkedHashMap<String, String>();

                // 循环列Cell
                for (int cellNum = 0; cellNum < hssfRow.getLastCellNum(); cellNum++) {
                    HSSFCell hssfCell = hssfRow.getCell(cellNum);
                    if (hssfCell == null) {
                        map.put(cellNum + "", "");
                        continue;
                    }
                    map.put(cellNum + "", getValue(hssfCell));
                }
                list.add(map);
            }
            break;
        }
        return list;
    }

OK,此时,我们已经将Excel数据封装成Map了。获取结果(Json)为:
你可以的对该Excel中数据进程操作了。

[
  {
    "0": "标题一",
    "1": "标题二",
    "2": "标题三",
    "3": "标题四",
    "4": "标题五"
  },
  {
    "0": "值1.1",
    "1": "值2.1",
    "2": "值3.1",
    "3": "值4.1",
    "4": "值5.1"
  },
    {
    "0": "值1.2",
    "1": "值2.2",
    "2": "值3.2",
    "3": "值4.2",
    "4": "值5.2"
  },
    {
    "0": "值1.3",
    "1": "值2.3",
    "2": "值3.3",
    "3": "值4.3",
    "4": "值5.3"
  }
]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值