@PostMapping("down")
public void down(HttpServletResponse response,HttpServletRequest request) throws IOException, IllegalAccessException {
List<Persion> list = new ArrayList<>();
Persion persion = new Persion("1","xiao11","1","1");
Persion persion1 = new Persion("1","da11","1","1");
list.add(persion);
list.add(persion1);
File file = new File("D:\\work\\study\\boottest\\cgi\\src\\main\\resources\\excel\\人员.xlsx");
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
XSSFSheet sheet = null;
sheet = workbook.getSheetAt(0);
for (int i = 0; i < list.size(); i++) {
//获得第二行
XSSFRow row = sheet.createRow(i + 1);
//通过反射获得这个对象的属性字段数组
Field[] fields = list.get(i).getClass().getDeclaredFields();
int a = 0;
for (Field field : fields) {
//允许访问私有属性
field.setAccessible(true);
//得到value
Object o = field.get(list.get(i));
XSSFCell cell = row.createCell(a);
cell.setCellValue(String.valueOf(o));
a++;
}
/*Persion persion2 = list.get(i);
XSSFRow row = sheet.createRow(i+4);
XSSFCell cell = row.createCell(i+1);
cell.setCellValue(persion2.getId());
cell = row.createCell(i+2);
cell.setCellValue(persion2.getName());
cell = row.createCell(i+3);
cell.setCellValue(persion2.getAge());
cell = row.createCell(i+4);
cell.setCellValue(persion2.getSex());*/
}
File file1 = new File("文件保存路径");// D:\work\人员表.xlsx
//创建文件
file1.createNewFile();
FileOutputStream fileOutputStream = FileUtils.openOutputStream(file1);
workbook.write(fileOutputStream);
//前端下载
/* ServletOutputStream outputStream = response.getOutputStream();
response.reset();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");//设置生成的文件类型application/msexcel
response.setCharacterEncoding("UTF-8");//设置文件头编码方式和文件名
response.setHeader("Content-Disposition", "attachment;filename=库存统计信息.xlsx");
String origin = request.getHeader("Origin");
response.addHeader("Access-Control-Allow-Origin", origin);
workbook.write(outputStream);*/
}
Excel模板填充数据数据后导出
于 2022-10-10 16:31:28 首次发布