XWPFDocument 生成word,效果图如下:
详细代码如下:
public static void main(String[] args) throws Exception{
{
XWPFDocument doc = new XWPFDocument();
//创建段落================标题
XWPFParagraph paragraph = doc.createParagraph();
//设置对齐
paragraph.setAlignment(ParagraphAlignment.CENTER);
//相同属性得区域
XWPFRun run = paragraph.createRun();
run.setColor("000000");
run.setText("java生成word文档");
run.setFontFamily("黑体");
run.setFontSize(29);
run.setBold(true);
//换行
run.addBreak();
//表格标题
paragraph = doc.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setFontSize(18);
run.setText("一、表格标题");
run.setFontFamily("黑体");
run.addBreak();
//3*4 表格
XWPFTable table = doc.createTable(3, 4);
//表格属性
CTTblPr tablePr = table.getCTTbl().addNewTblPr();
//表格宽度
CTJc cTJc = tablePr.addNewJc();
//居中
cTJc.setVal(STJc.CENTER);
//列宽自动分割
CTTblWidth tableWidth = tablePr.addNewTblW();
//设置表格宽度
tableWidth.setType(STTblWidth.DXA);
tableWidth.setW(BigInteger.valueOf(9072));
//========================================第一行===================================
//设置单元格宽度
XWPFTableRow carRow1 = table.getRow(0);
//设置单元格高度
carRow1.setHeight(768);
//设置单元格宽度
carRow1.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
//以创建段落的方式给单元格赋值
List<XWPFParagraph> paragraphs = carRow1.getCell(0).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
XWPFRun run1 = paragraphs.get(0).createRun();
run1.setText("语言");
run1.setFontSize(10);
run1.setFontFamily("微软雅黑");
run1.setColor("000000");
run1.setBold(true);
carRow1.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
carRow1.getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow1.getCell(1).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.CENTER);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("java");
run1.setFontSize(10);
run1.setFontFamily("微软雅黑");
run1.setColor("000000");
carRow1.getCell(1).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
carRow1.getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow1.getCell(2).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("作者");
run1.setFontSize(10);
run1.setFontFamily("微软雅黑");
run1.setColor("000000");
run1.setBold(true);
carRow1.getCell(2).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
carRow1.getCell(3).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow1.getCell(3).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.CENTER);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("音乐土豆");
run1.setFontSize(10);
run1.setFontFamily("微软雅黑");
run1.setColor("000000");
carRow1.getCell(3).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
XWPFTableRow carRow2 = table.getRow(1);
carRow2.setHeight(768);
carRow2.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow2.getCell(0).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("");
carRow2.getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow2.getCell(1).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("");
carRow2.getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow2.getCell(2).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("key");
run1.setFontSize(10);
run1.setFontFamily("微软雅黑");
run1.setColor("000000");
run1.setBold(true);
carRow2.getCell(2).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
carRow2.getCell(3).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow2.getCell(3).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.CENTER);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("value");
run1.setFontSize(10);
run1.setFontFamily("微软雅黑");
run1.setColor("000000");
carRow1.getCell(3).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
XWPFTableRow carRow3 = table.getRow(2);
carRow3.setHeight(768);
carRow3.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow3.getCell(0).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("创作时间");
run1.setFontSize(10);
run1.setFontFamily("微软雅黑");
run1.setColor("000000");
run1.setBold(true);
carRow3.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
carRow3.getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow3.getCell(1).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("");
carRow3.getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow3.getCell(2).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("2021年12月16日");
run1.setFontSize(10);
run1.setFontFamily("微软雅黑");
run1.setColor("000000");
run1.setBold(true);
carRow3.getCell(2).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
carRow3.getCell(3).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
paragraphs = carRow3.getCell(3).getParagraphs();
paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
run1 = paragraphs.get(0).createRun();
run1.setText("");
//合并列
horizontal(table,2,0,1);
horizontal(table,2,2,3);
//合并行
vertically(table,0,0,1);
//输出到D盘
FileOutputStream os = new FileOutputStream("D:\\测试.docx");
doc.write(os);
os.close();
}
XWPFTable 合并单元格
更多推荐
java实现 poi XWPFDocument 生成word(附段落、表格)
发布评论