try (XWPFDocument doc = new XWPFDocument(); OutPutStream outPutStream = response.getoutPutStream()) {
addCustomheadingStyle(doc,"标题1",1);
addCustomheadingStyle(doc,"标题2",2);
XWPFParagraph title = doc.createParagraph();
title.setStyle("标题1");
title.setAlignment(ParagraphAlignment.CENTER);
title.setIndentationFirstLine(600);
title.setWordWrapped(true);
XWPFRun run = title.createRun();
run.setText("我是标题");
run.setFontSize(20);
run.setFontFamily("宋体");
run.setBold(true);
XWPFTable table = doc.createTable(5, 5);
XWPFTableRow row = table.getRow(0);
for (int i = 0; i < 5; i++) {
XWPFTableRow row1 = table.getRow(i+1);
XWPFTableCell cell = row1.getCell(0);
cell.setText("我是单元格");
CTTcPr ctTcPr = cell.getCTTc().addNewTcPr();
ctTcPr.addNewTcW().setW(BigInteger.valueOf(2000));
XWPFParagraph xwpfParagraph = cell.getParagraphs().get(0);
xwpfParagraph.setFontAlignment(2);
int col = 0;
int fromrow = 4;
int toRow = 5;
for (int rowIndex = fromrow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell mergeCell = table.getRow(rowIndex).getCell(col);
if (mergeCell != null) {
if (rowIndex == fromrow) {
mergeCell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
mergeCell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
int openCell = 0;
int endCell = 2;
for (int cellIndex = openCell;cellIndex<=endCell;cellIndex++){
XWPFTableCell cell1 = row.getCell(cellIndex);
if (cell1 != null) {
if (cellIndex == fromrow) {
cell1.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
cell1.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
String fileName = "111.png";
String filePath = "C:/1/111.png";
XWPFParagraph paragraphArray = row.getCell(7).getParagraphArray(0);
if (paragraphArray != null) {
paragraphArray = row.getCell(7).addParagraph();
}
paragraphArray.setAlignment(ParagraphAlignment.CENTER);
paragraphArray.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun photos = paragraphArray.createRun();
try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
photos.addPicture(fileInputStream, XWPFDocument.PICTURE_TYPE_JPEG, fileName, Units.toEMU(50), Units.toEMU(50));
} catch (Exception e) {
e.printstacktrace();
}
}
} catch (Exception e) {
}
private static void addCustomheadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {
CTStyle ctStyle = CTStyle.Factory.newInstance();
ctStyle.setStyleId(strStyleId);
CTString styleName = CTString.Factory.newInstance();
styleName.setVal(strStyleId);
ctStyle.setName(styleName);
CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
indentNumber.setVal(BigInteger.valueOf(headingLevel));
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
ctStyle.setQFormat(onoffnull);
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setoutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
XWPFStyles styles = docxDocument.createStyles();
style.setType(STStyleType.ParaGRAPH);
styles.addStyle(style);
}
}