12345

发布时间:2024年01月19日
 @GetMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Map<String,String>> list1 = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("q1","moveNo");
        map.put("q2","moveNo");
        map.put("q3","78");
        map.put("q4","moveNo");
        map.put("q5","moveNo");
        map.put("q6","moveNo");
        map.put("q7","moveNo");
        Map<String, String> map1 = new HashMap<>();
        map1.put("q5","moveNo");
        map1.put("q6","moveNo");
        map1.put("q7","moveNo");
        list1.add(map);
        list1.add(map1);



        List<Map<String,Object>> printVoList = new ArrayList<>();
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("COL1","123");
        hashMap.put("COL2","234");
        hashMap.put("COL3","345");
        hashMap.put("COL4","456");
        Map<String, Object> hashMap1 = new HashMap<>();
        hashMap1.put("COL1","哈");
        hashMap1.put("COL2","吧");
        hashMap1.put("COL3","吗");
        printVoList.add(hashMap);
        printVoList.add(hashMap1);


        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("123.xlsx");

        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("我测试的", "utf-8") + ".xlsx");
        OutputStream out = response.getOutputStream();

        if (inputStream == null){
            throw new RuntimeException("文件未找到!");
        }

        Workbook workbook = new XSSFWorkbook(inputStream);

        //获取创建工作簿的第一页
        Sheet sheet=workbook.getSheetAt(0);
        //给指定的sheet命名
//        workbook.setSheetName(0,"dataSheet");
        //获取当前工作簿的行数
//        int totalRows=sheet.getPhysicalNumberOfRows();
        //****遍历模板sheet,根据当中的设定进行赋值****
        int newRowIndex = 3;//起始行 从第4行开始
        for (int i=0;i<list1.size();i++) {
            Row row = sheet.createRow(newRowIndex++);
            int column = row.getLastCellNum();
            int j = 0;
            (row.getCell(j)!=null?row.getCell(j):row.createCell(j)).setCellValue(list1.get(i).get("q1"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q2"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q3"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q4"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q5"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q6"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q7"));
        }


        //获取创建工作簿的第2页
        Sheet sheet1=workbook.getSheetAt(1);
        //获取当前工作簿的行数
        int totalRows=sheet1.getPhysicalNumberOfRows();
        for (int i = 0;i<totalRows;i++){
            Row row = sheet1.getRow(i);
            if (row == null) continue;
            System.out.println(row.getRowNum() + JSONUtil.toJsonStr(row));;
        }
        sheet1.getRow(4).getCell(3).setCellValue("1");
        sheet1.getRow(5).getCell(3).setCellValue("1");
        //获取创建工作簿的第3页
        Sheet sheet2=workbook.getSheetAt(2);
        System.out.println("================");
        for (int i = 0;i<sheet2.getPhysicalNumberOfRows();i++){
            Row row = sheet2.getRow(i);
            if (row == null) continue;
            System.out.println(row.getRowNum() + JSONUtil.toJsonStr(row));;
        }
        //获取当前工作簿的行数
//        int totalRows2=sheet1.getPhysicalNumberOfRows();

        sheet2.getRow(2).getCell(3).setCellValue("1");
        sheet2.getRow(3).getCell(3).setCellValue("3");
        sheet2.getRow(5).getCell(3).setCellValue("111");
        sheet2.getRow(5).getCell(5).setCellValue("3333");
        sheet2.getRow(11).getCell(3).setCellValue("3333");
        sheet2.getRow(11).getCell(5).setCellValue("3333");
        /*int newRowIndex = 3;//起始行 从第4行开始
        //获取模板对应的数据表字段的值
        Row dataRow=sheet.getRow(newRowIndex++);
        //获得该行对应的字段的数量
        int columnNum=dataRow.getPhysicalNumberOfCells();

        //将结果集渲染到当前sheet当中
        for (Map<String,Object> pageData:printVoList) {
            sheet.shiftRows(newRowIndex, totalRows + 1, 1); //在startRow和endRow之间的行移动一行
            Row newRow = sheet.createRow(newRowIndex++);
            //创建需要插入的目标行,该值需要在每次完成一行记录值插入后重新归0
            int cellIndex = 0;

            //ic此处为cells数组的结果集的字段的位置
            for (int i = 1; i <= columnNum; i++) {
                //从pageData当中取出目标单元格需要的值
                String cellContent = pageData.get("COL"+i) != null ? String.valueOf(pageData.get("COL"+i)) : "";
                //设置目标单元格的位置和类型
                Cell cell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
                //设置目标单元格的值
                cell.setCellValue(cellContent);
            }
        }*/
        workbook.write(out);
        out.flush();
        out.close();

    }
文章来源:https://blog.csdn.net/weixin_43996168/article/details/135706988
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。