生成doc和docx教程

方便生成doc和docx的相关代码资源

这是jar资源

有很多的包是关于生成文档及内容的控制的,比方最原始的POI,还有基于此开发的阿里的FastExcel之类的,但是使用起来还是比较麻烦,最近发现一个好玩意儿 Spire,一个第三方封装的也是基于POI的,他们的官方网址在

唯一的缺点是这个包很大有七八十兆左右

下面我简单的使用这个来生成内容看一下

加载资源读出来文档内容

import com.spire.doc.*;


/**
 * @ClassName SpireDoc
 * @Author Qin
 * @Date 2020/9/12 13:45
 * @Description SpireDoc
 * @Version 1.0
 */
public class SpireDoc {

    String desPath = "D://des.doc";
    String path = "D://src.doc";


    @Test
    public void testSpiredDoc() {
        //加载Word文档
        Document document = new Document(path);
        String text = document.getText();
        System.out.println(text);

    }

读出内容替换掉然后再保存

import com.spire.doc.*;


/**
 * @ClassName SpireDoc
 * @Author Qin
 * @Date 2020/9/12 13:45
 * @Description SpireDoc
 * @Version 1.0
 */
public class SpireDoc {

    String desPath = "D://des.doc";
    String path = "D://src.doc";


    @Test
    public void testSpiredDoc() {
        //加载Word文档
        Document document = new Document(path);
        document.replace("year", "2020", false, true);
        document.replace("season", "2", false, true);
        document.replace("*pic1*", "2020", false, true);
        document.replace("*pic2*", "2020", false, true);
        //保存文档
        document.saveToFile(desPath, FileFormat.Doc);


    }

上述代码会在文档中查找对应的内容然后替换成你想要替换的内容,有些同志经常会被让用模板来生成报告什么的,可以用特殊字符来占位,然后从数据库提取数据计算得到后替换生成。

替换图片


@Test
    public void replacePic() {
        Document document = new Document();
        document.loadFromFile(path);

        TextSelection[] selections = document.findAllString("*pic1*", true, true);

        //用图片替换文字
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {

            TextSelection textSelection = (TextSelection) obj;
            DocPicture pic = new DocPicture(document);
            pic.loadImage(picpath);
            pic.setHeight(300);
            pic.setWidth(500);
            range = textSelection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index, pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }

        //保存文档
        document.saveToFile(desPath, FileFormat.Doc);
    }

相关代码可以控制图片大小,位置、、、、、

在文档中生成表格替换表格

@Test
    public void controlExcel() throws FileNotFoundException {
        String out = "D://out.doc";
        //加载测试文档
        Document doc = new Document();
        InputStream in = new FileInputStream(desPath);
        doc.loadFromStream(in, FileFormat.Auto);
        String[][] data =
                {
                        new String[]{"Winny", "女", "综合", "0109"},
                        new String[]{"Lois", "女", "综合", "0111"},
                        new String[]{"Jois", "男", "技术", "0110"},
                        new String[]{"Moon", "女", "销售", "0112"},
                        new String[]{"Vinit", "女", "后勤", "0113"},
                };
        //获取表格
        Section section = doc.getSections().get(0);
        // getTables.get()  0,第一个表格 以此类推
        Table table = section.getTables().get(1);


        //table.getRows().insert(2,table.addRow());//在表格中第3行插入一行
        //table.addRow(4);//默认在表格最下方添加4个单元格
        //table.addRow(true,2);//带格式在最后一行添加2个单元格
        //table.addRow(false,2);//不带格式在最后一行添加2个单元格
        //添加数据到剩余行
        for (int r = 0; r < data.length; r++) {
            table.addRow();//默认在表格最下方插入一行
            TableRow dataRow = table.getRows().get(r + 1);
            for (int c = 0; c < data[r].length; c++) {
                dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
            }
        }
        //保存文档
        doc.saveToFile(out, FileFormat.Docx_2013);
        doc.dispose();
    }

对表格的大小位置、背景颜色、文字颜色、行与列合并之类都可以进行操作。

好了相关的介绍简单到这里了,具体可以看他们的官网,因为他们是收费的,最后生成的内容会有水印,去水印会另外收费。我的资源嘛,你们可以看一下,自己试一下。

参见文章开始链接

猜你喜欢

转载自blog.csdn.net/weixin_41086086/article/details/110439576