poi实现word文档导出

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private DepartService departService;


    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {

        XWPFDocument doc = new XWPFDocument();
        XWPFParagraph title = doc.createParagraph();
        title.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun r1 = title.createRun();
        r1.setBold(true);
        r1.setFontFamily("宋体");
        r1.setText("计划表");
        r1.setFontSize(22);

        // 查询部门
        List<Depart> departList = departService.getDepartList();
        // 根据部门遍历
        for (Depart depart : departList) {
            XWPFParagraph actTheme = doc.createParagraph();
            actTheme.setAlignment(ParagraphAlignment.LEFT);
            XWPFRun runText1=actTheme.createRun();
            runText1.setText(depart.getDepartName());
            runText1.setFontSize(13);

            List<Company> companyList = companyService.getCompanyByDeptId(depart.getId());

            // 创建表格
            XWPFTable table=actTheme.getDocument().createTable(companyList.size()+1, 9);
            table.setWidth(500);
            table.setCellMargins(20, 20, 20, 20);

            List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
            tableCells.get(0).setText("序号");
            tableCells.get(1).setText("项目名称");
            tableCells.get(2).setText("标段名称");
            tableCells.get(3).setText("内容");
            tableCells.get(4).setText("金额");
            tableCells.get(5).setText("时间");
            tableCells.get(6).setText("类别");
            tableCells.get(7).setText("方式");
            tableCells.get(8).setText("备注");

            for (int i = 0; i < companyList.size(); i++) {
                List<XWPFTableCell> tableCells2 = table.getRow(i+1).getTableCells();
                tableCells2.get(0).setText(companyList.get(i).getId().toString());
                tableCells2.get(1).setText(companyList.get(i).getProjectName());
                tableCells2.get(2).setText(companyList.get(i).getBlockName());
                tableCells2.get(3).setText(companyList.get(i).getContent());
                tableCells2.get(4).setText(companyList.get(i).getAmout());
                tableCells2.get(5).setText(companyList.get(i).getTime());
                tableCells2.get(6).setText(companyList.get(i).getType());
                tableCells2.get(7).setText(companyList.get(i).getWay());
                tableCells2.get(8).setText(companyList.get(i).getNote());
            }
        }

        FileOutputStream out = new FileOutputStream("E:\\test.docx");
        doc.write(out);
        out.close();

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值