try (XWPFDocument doc = new XWPFDocument(); OutPutStream outPutStream = response.getOutPutStream()) {
//------------------word文本------------------------------------
//设置标题
addCustomHeadingStyle(doc,"标题1",1);
addCustomHeadingStyle(doc,"标题2",2);
//创建word
XWPFParagraph title = doc.createParagraph();
//设置标题
title.setStyle("标题1");
title.setAlignment(ParagraphAlignment.CENTER); //对齐方式---居中
title.setIndentationFirstLine(600); //首航缩进---2个媳妇
title.setWordWrapped(true); //换行
//对文字操作
XWPFRun run = title.createRun();
run.setText("我是标题"); //内容
run.setFontSize(20); //字体大小
run.setFontFamily("宋体"); //字体
run.setBold(true); //是否加粗---加粗
//-------------------表格-----------------------------
//创建5行5列的表格
XWPFTable table = doc.createTable(5, 5); //行数,列数
//表格第一行
XWPFTableRow row = table.getRow(0);
//表格内容
for (int i = 0; i < 5; i++) {
//第i+1行
XWPFTableRow row1 = table.getRow(i+1);
//第i+1行的第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);
}
}
}
//------------------word单元格写入图片-----------------
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) {
}
/**
* 增加自定义标题样式。这里用的是stackoverflow的源码
*
* @param docxDocument 目标文档
* @param strStyleId 样式名称
* @param headingLevel 样式级别
*/
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));
// lower number > style is more prominent in the formats bar
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
// style shows up in the formats bar
ctStyle.setQFormat(onoffnull);
// style defines a heading of the given level
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setOutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
// is a null op if already defined
XWPFStyles styles = docxDocument.createStyles();
style.setType(STStyleType.PARAGRAPH);
styles.addStyle(style);
}
}
poi导出word,嵌套excel、图片、合并单元格、字体样式
于 2021-07-01 14:38:43 首次发布