@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);
int newRowIndex = 3;
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"));
}
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");
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));;
}
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");
workbook.write(out);
out.flush();
out.close();
}