excel导入导出

    @AutoLog(value = "导入excel")
    @ApiOperation(value = "导入excel", notes = "需要传入的参数为excel文件的绝对路径:filePhth  需要注意:excel表格的第一列为空(由于某些原因获取不到第一列的值)")
    @PostMapping(value = "/importExcel")
    public Result<?> importExcel(String filePath) {
        List<Saler> infoList = new ArrayList<Saler>();
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        infoList.clear();
        try {
            InputStream is = new FileInputStream(filePath);

            int index = filePath.lastIndexOf(".");
            String postfix = filePath.substring(index + 1);

            Workbook workbook = null;
            if ("xls".equals(postfix)) {
                workbook = new HSSFWorkbook(is);
            } else if ("xlsx".equals(postfix)) {
                workbook = new XSSFWorkbook(is);
            }
            // 获取第1张表
            Sheet sheet = workbook.getSheetAt(0);
            // 总的行数
            int rows = sheet.getLastRowNum();
            // 总的列数--->最后一列为null则有问题,读取不完整,将表头的数目作为总的列数,没有的则补为null
            int columns = sheet.getRow(0).getLastCellNum();
            // 先列后行
            for (int i = 1; i <= rows; i++) {
                Row row = sheet.getRow(i);
                if (null != row && row.getFirstCellNum() == -1) {// 这一行是空行,不读取
                    continue;
                }
                // 这一行的总列数
                // columns = row.getLastCellNum();
                List<String> contentList = new ArrayList<String>();
                contentList.clear();
                for (int j = 1; j < columns; j++) {
                    if (row.getCell(j) != null) {
                        row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                        contentList.add(row.getCell(j).getStringCellValue());
                    } else {
                        contentList.add("");
                    }
                }
                map.put("StorageInfo" + i, contentList);
            }

            // 遍历map集合,封装成bean
          
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值