```java
---------------------------------后端获取response-------------------------------------
ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletResponse response = servletRequestAttributes.getResponse();
response.setContentType("application/msword;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".docx");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
---------------------------------创建文件,设置标题段落start-------------------------------------
//方式三:
XWPFDocument document = new XWPFDocument();
//添加标题
XWPFParagraph titleParagraph = document.createParagraph();
//设置段落居中
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleRun = titleParagraph.createRun();
titleRun.setText("标题文字");
titleRun.setFontSize(18);
titleRun.setBold(true);
-----------段落start--------------------
XWPFParagraph firstParagraph = document.createParagraph();
XWPFRun run = firstParagraph.createRun();
run.setText(" 我是第一段文字");
run.addCarriageReturn();//文件折行
run.setFontSize(16);//字体
run.setColor("000000");
//设置段落背景颜色
CTShd cd = run2.getCTR().addNewRPr().addNewShd();
cd.setVal(STShd.CLEAR);
-----------表格start--------------------
XWPFTable table = document.createTable(3, 2);
WordExportUtil.setTableStyle(table);
//表格第一行
XWPFTableRow headRow = table.getRow(0);
WordExportUtil.setRowProperty(headRow);
XWPFTableCell headCell0 = headRow.getCell(0);//第一个单元格
XWPFTableCell headCell1 = headRow.getCell(1);//第二个单元格
List<XWPFParagraph> list = rowThree0.getParagraphs();
XWPFParagraph pIO = list.get(0);
XWPFRun rIO = pIO.createRun();
rIO.setFontFamily("宋体");
rIO.setFontSize(16);
rIO.setText("第一行第一个单元格文字");
XWPFParagraph pIO = list.get(1);
XWPFRun rIO = pIO.createRun();
rIO.setFontFamily("宋体");
rIO.setFontSize(16);
rIO.setText("第一行第二个单元格文字");
//-----------设置页眉--------------------
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
//添加页眉
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctHeader = ctrHeader.addNewT();
String headerText = "xxx有限公司";
ctHeader.setStringValue(headerText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
//设置为右对齐
headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
//添加页脚
CTP ctpFooter = CTP.Factory.newInstance();
CTR ctrFooter = ctpFooter.addNewR();
CTText ctFooter = ctrFooter.addNewT();
ctFooter.setStringValue("xxx有限公司");
XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
headerParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFParagraph[] parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerParagraph;
policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
//写入到response中实现下载
OutputStream outputStream = response.getOutputStream();
document.write(outputStream);
outputStream.flush();
outputStream.close();