栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 操作excel-2

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 操作excel-2

        
            org.apache.poi
            poi
            4.1.2
        
        
            org.apache.poi
            poi-ooxml
            4.1.2
        
        
            org.apache.poi
            poi-ooxml-schemas
            4.1.2
        
  @RequestMapping("/extArrangeOrderTemp")
    public void extArrangeOrderTemp(HttpServletResponse response) {
        try {
            String[] title = {"客户", "订单号", "款号", "交货日期", "开款日期", "订单数量", "目标数量", "生产小组", "SAM值/秒"};
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet();
            sheet.setDefaultRowHeight((short) 500);
            sheet.setDefaultColumnWidth(10000);
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell;
            row.setHeight((short) 500);
            for (int i = 0; i < title.length; i++) {
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
            }
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RequestMapping(value = "/batchArrangeOrder")
    public baseResult batchArrangeOrder(HttpServletRequest request, Long adminId) {
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            Map fileMap = multipartRequest.getFileMap();
            MultipartFile mf = multipartRequest.getFile("file");
            assert mf != null;
            return mArrangeOrderService.batchArrangeOrder(mf, adminId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseResult.result();
    }




public baseResult batchArrangeOrder(MultipartFile mf, Long adminId) {
        try {
            Workbook workbook = new HSSFWorkbook(mf.getInputStream());
            Sheet sheet = workbook.getSheetAt(0);
            int index = 0;
            for (Row row : sheet) {
                if (index != 0) {
                    String customer = ProjectUtils.getCellStringValue(row, 0);
                    String orderCode = ProjectUtils.getCellStringValue(row, 1);
                    String styleCode = ProjectUtils.getCellStringValue(row, 2);
                    String deliveryDate = MSDateUtil.formatTime(ProjectUtils.getCellDatevalue(row, 3), "yyyy-MM-dd");
                    String paymentDate = MSDateUtil.formatTime(ProjectUtils.getCellDatevalue(row, 4), "yyyy-MM-dd");
                    Integer orderNum = ProjectUtils.getCellIntValue(row, 5);
                    Integer targetNum = ProjectUtils.getCellIntValue(row, 6);
                    String proLine = ProjectUtils.getCellStringValue(row, 7);
                    Integer stdDur = ProjectUtils.getCellIntValue(row, 8);
                    if (orderCode == null) continue;
                    inArrangeOrder(adminId, customer, orderCode, styleCode, deliveryDate, paymentDate, orderNum, targetNum, proLine, stdDur);
                }
                index++;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return baseResult.result().setReturnCode(ReturnCode.CODE_4001);
        }
        return baseResult.result();
    }

public static String getCellStringValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell.getCellType() == CellType.STRING) {
            return cell.getStringCellValue();
        }

        if (cell.getCellType() == CellType.NUMERIC) {
            return new Double(cell.getNumericCellValue()).longValue() + "";
        }
        return null;
    }


    public static Integer getCellIntValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell.getCellType() == CellType.STRING) {
            return Integer.parseInt(cell.getStringCellValue());
        }

        if (cell.getCellType() == CellType.NUMERIC) {
            return new Double(cell.getNumericCellValue()).intValue();
        }
        return null;
    }

    public static Float getCellFloatValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell.getCellType() == CellType.STRING) {
            return Float.parseFloat(cell.getStringCellValue());
        }

        if (cell.getCellType() == CellType.NUMERIC) {
            return new Double(cell.getNumericCellValue()).floatValue();
        }
        return null;
    }

    public static Date getCellDatevalue(Row row, int index) {
        Cell cell = row.getCell(index);
        return cell.getDateCellValue();
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770447.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号