zoukankan      html  css  js  c++  java
  • Java 生成Word文档

    Word具有强大的文字处理功能,是我们日常工作生活中广泛使用到的工具之一。本文就将介绍如何使用Free Spire.Doc for Java在Java应用程序中创建Word文档,插入图片,并且设置段落的字体格式、对齐方式、段首缩进以及段落间距等。

    Jar包导入

    方法一:下载Free Spire.Doc for Java包并解压缩,然后将lib文件夹下的Spire.Doc.jar包作为依赖项导入到Java应用程序中。

    方法二:通过Maven仓库安装JAR包,配置pom.xml文件的代码如下

    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>2.7.3</version>
        </dependency>
    </dependencies>

    Java代码

    import com.spire.doc.*;
    import com.spire.doc.documents.HorizontalAlignment;
    import com.spire.doc.documents.Paragraph;
    import com.spire.doc.documents.ParagraphStyle;
    import com.spire.doc.fields.DocPicture;
    
    import java.awt.*;
    
    public class CreateWordDocument {
        public static void main(String[] args){
            //创建Word文档
            Document document = new Document();
    
            //添加一个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 = new ParagraphStyle(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 = new ParagraphStyle(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); } }

  • 相关阅读:
    剑指offer 数值的整数次方
    剑指offer 链表中倒数第K个节点
    剑指offer反转链表
    剑指offer 栈的压入弹出 顺序
    剑指offer 二叉搜索树后续遍历序列 判断
    剑指offer 复杂链表的复制 (有向图的复制)
    剑指offer 二叉搜索树与双向链表
    2018/03/15 常用的超全局变量
    2018/03/14 每日一个Linux命令 之 ln
    PHP使用 zip 扩展压缩文件
  • 原文地址:https://www.cnblogs.com/jazz-z/p/13524155.html
Copyright © 2011-2022 走看看