java word文档_Java 生成Word文档

import com.spire.doc.*;importcom.spire.doc.documents.HorizontalAlignment;importcom.spire.doc.documents.Paragraph;importcom.spire.doc.documents.ParagraphStyle;importcom.spire.doc.fields.DocPicture;import java.awt.*;public classCreateWordDocument {public static voidmain(String[] args){//创建Word文档

Document document = newDocument();//添加一个section

Section section =document.addSection();//添加4个段落至section

Paragraph para1 =section.addParagraph();

para1.appendText("滕王阁序");

Paragraph para2=section.addParagraph();

para2.appendText("豫章故郡,洪都新府。星分翼轸,地接衡庐。襟三江而带五湖,控蛮荆而引瓯越。"+

"物华天宝,龙光射牛斗之墟;人杰地灵,徐孺下陈蕃之榻。雄州雾列,俊采星驰。台隍枕夷夏之交,宾主尽东南之美。"+

"都督阎公之雅望,棨戟遥临;宇文新州之懿范,襜帷暂驻。十旬休假,胜友如云;千里逢迎,高朋满座。"+

"腾蛟起凤,孟学士之词宗;紫电青霜,王将军之武库。家君作宰,路出名区;童子何知,躬逢胜饯。");

Paragraph para3=section.addParagraph();

para3.appendText("时维九月,序属三秋。潦水尽而寒潭清,烟光凝而暮山紫。俨骖騑于上路,访风景于崇阿;临帝子之长洲,得天人之旧馆。"+

"层峦耸翠,上出重霄;飞阁流丹,下临无地。鹤汀凫渚,穷岛屿之萦回;桂殿兰宫,即冈峦之体势。");//添加图片

Paragraph para4 =section.addParagraph();

DocPicture picture= para4.appendPicture("C:\\Users\\Administrator\\Desktop\\1.jpg");//设置图片宽度

picture.setWidth(300f);//设置图片高度

picture.setHeight(250f);//将第一段作为标题,设置标题格式

ParagraphStyle style1 = newParagraphStyle(document);

style1.setName("titleStyle");

style1.getCharacterFormat().setBold(true);

style1.getCharacterFormat().setTextColor(Color.BLUE);

style1.getCharacterFormat().setFontName("宋体");

style1.getCharacterFormat().setFontSize(12f);

document.getStyles().add(style1);

para1.applyStyle("titleStyle");//设置第2、3段的段落的格式

ParagraphStyle style2 = newParagraphStyle(document);

style2.setName("paraStyle");

style2.getCharacterFormat().setFontName("宋体");

style2.getCharacterFormat().setFontSize(11f);

document.getStyles().add(style2);

para2.applyStyle("paraStyle");

para3.applyStyle("paraStyle");//将第1段和第4段设置为水平居中对齐方式

para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);//设置第2段和第3段的段首缩进

para2.getFormat().setFirstLineIndent(25f);

para3.getFormat().setFirstLineIndent(25f);//设置第一段和第二段的段后间距

para1.getFormat().setAfterSpacing(15f);

para2.getFormat().setAfterSpacing(10f);//保存文档

document.saveToFile("Output.docx", FileFormat.Docx);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值