Excel模板填充数据数据后导出

@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);*/
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值