@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(); }
poi实现word文档导出
最新推荐文章于 2024-04-23 18:52:22 发布