1.pom 引入
<!--word报告生成依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2.java代码示例文章来源:https://www.toymoban.com/news/detail-729153.html
public class test {
public static void main(String[] args) throws IOException, InvalidFormatException {
log.info("Word文档开始生成!");
XWPFDocument document = new XWPFDocument();
WriteHomePage(document,1,"SSS"); //写首页
WriteText(document); //写表格前正文与题注
WriteFirstTable(document); //写第一个表格
FileOutputStream out = new FileOutputStream("Report.docx");
document.write(out);
out.close();
document.close();
log.info("Word文档已成功生成!");
}
public static XWPFDocument WriteHomePage(XWPFDocument document, int orb, String salliteName) throws IOException, InvalidFormatException {
//插入图片
String imgFile = "E:\\test.png";
byte[] imageBytes = Files.readAllBytes(new File(imgFile).toPath());
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run00 = paragraph.createRun();
run00.setText(" ");
int width = 200; // 图片宽度
int height = 140; // 图片高度
int pictureType = XWPFDocument.PICTURE_TYPE_PNG;
run00.addPicture(new ByteArrayInputStream(imageBytes), pictureType, imgFile, Units.toEMU(width), Units.toEMU(height));
//输入标题
XWPFParagraph title1 = document.createParagraph();
XWPFRun run = title1.createRun();
run.setText("O00");
run.setUnderline(UnderlinePatterns.SINGLE);
run.setFontFamily("Times New Roman");
run.setFontSize(28);
run.setBold(true);
run.setItalic(true);
//不同字体设置的参数大小不同,需要尝试
title1.setIndentationFirstLine(1130);// <!-- 设置首行缩进2字符 单位是 1/20 磅 -->
run.addBreak();
XWPFParagraph title2 = document.createParagraph();
XWPFRun run1 = title2.createRun();
run1.setText(salliteName+" Continuity of Data");
run1.setFontFamily("Times New Roman");
run1.setFontSize(28);
run1.setBold(true);
title2.setIndentationFirstLine(1130); // <!-- 设置首行缩进2字符 -->
//设置空白行
for (int i = 0; i < 16; i++) {
XWPFParagraph blank = document.createParagraph();
blank.createRun().setText("");
}
// 创建段落并设置右对齐样式
XWPFParagraph footer = document.createParagraph();
footer.setAlignment(ParagraphAlignment.RIGHT);
// 创建页脚并添加到段落中
XWPFRun run2 = footer.createRun();
run2.setText("Data Center");
run2.setFontSize(18);
run2.setFontFamily("Times New Roman");
run2.addBreak();
XWPFRun run3 = footer.createRun();
run3.setText("www.xxx.com");
run3.setFontSize(18);
run3.setFontFamily("Times New Roman");
run3.setUnderline(UnderlinePatterns.SINGLE);
run3.addBreak();
return document;
}
public static XWPFDocument WriteText(XWPFDocument document){
//新建一页,创建表格
XWPFParagraph newPage = document.createParagraph();
newPage.setPageBreak(true);
XWPFParagraph para = document.createParagraph();
XWPFRun run = para.createRun();
run.setText("该报告数据内容正文如下。");
run.setFontFamily("宋体");
run.setFontSize(12);//小四
// XWPFFont font = ;
para.setIndentationFirstLine(500); // <!-- 设置首行缩进2字符 -->
para.setSpacingBetween(1.5f);
//表题注
XWPFParagraph para2 = document.createParagraph();
XWPFRun runPara2 = para2.createRun();
runPara2.setText("表1 数据概况");
runPara2.setFontFamily("宋体");
runPara2.setFontSize(12);
para2.setAlignment(ParagraphAlignment.CENTER);
para2.setVerticalAlignment(TextAlignment.CENTER);
para2.setSpacingBetween(1.5f);
return document;
}
//输入表格
public static XWPFDocument WriteFirstTable(XWPFDocument document){
int row = 2;
int col = 2;
XWPFTable table = document.createTable(row, col);
// 设置表格行高
for (XWPFTableRow tableRow : table.getRows()) {
tableRow.setHeight(500);
}
table.setStyleID("Table Grid");
table.setWidth("100%");
//设置表格标题
String[] titleArr = {"数据类型","数据连续性"};
XWPFTableRow titleRow = table.getRow(0);
//设置表头内容和样式,
for (int i = 0; i < col; i++) {
XWPFTableCell cell = titleRow.getCell(i);
cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);
XWPFParagraph p = cell.getParagraphs().get(0);
p.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run3 = p.createRun();
run3.setBold(true); // 设置字体加粗
run3.setText(titleArr[i]);
}
//CTHMerge 横向合并,CTVMerge纵向合并, STMerge.RESTART 表示开始合并,为 STMerge.CONTINUE 表示继续合并
// titleRow.getCell(4).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
// titleRow.getCell(5).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
// XWPFParagraph p = titleRow.getCell(4).getParagraphs().get(0);
// p.setAlignment(ParagraphAlignment.CENTER);
XWPFTableRow row1 = table.getRow(1);
row1.getCell(0).setText("11");
row1.getCell(1).setText("12");
XWPFTableRow row2 = table.getRow(1);
row2.getCell(0).setText("21");
row2.getCell(1).setText("22");
return document;
}
}
3.效果展示
文章来源地址https://www.toymoban.com/news/detail-729153.html
到了这里,关于java导出word(含图片、表格)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!