使用Jacob导出Word

1、网上搜索下载Jacob

下载网址:JACOB - Java COM Bridge - Browse Files at SourceForge.net

2、解压文件夹

将图中的后缀为.dll文件复制到C盘下的Windows下

3、由于直接放置maven并不会好使,所以需要我们手动导入

将文件中的jar包导入到项目中

4、添加依赖并刷新

<!--添加本地的jacob.jar包-->
<dependency>
     <groupId>com.jacob</groupId>
     <artifactId>jacob</artifactId>
     <version>1.18</version>
     <scope>system</scope>
     <systemPath>${basedir}/lib/jacob.jar</systemPath>
</dependency>

5、添加工具类,模板的制作和我上一篇导word一样

public class XmlTemplate2Word {
    /**
     * 将xml模板转换为后缀为doc文件,本质仍是属于xml
     * @param dataMap	需要填充到模板的数据
     * @param templateFilePath	模板文件路径
     * @param targetFilePath	目标文件保存路径
     * @throws IOException
     * @throws TemplateException
     */
    public static void xml2XmlDoc(Map<String,Object> dataMap, String templateFilePath, String targetFilePath){
        // 将模板文件路径拆分为文件夹路径和文件名称
        String tempLetDir = templateFilePath.substring(0,templateFilePath.lastIndexOf("/"));
        // 注意:templetFilePath.lastIndexOf("/")中,有的文件分隔符为:\ 要注意文件路径的分隔符
        String templetName = templateFilePath.substring(templateFilePath.lastIndexOf("/")+1);
        // 将目标文件保存路径拆分为文件夹路径和文件名称
        String targetDir = targetFilePath.substring(0,targetFilePath.lastIndexOf("/"));
        String targetName = targetFilePath.substring(targetFilePath.lastIndexOf("/")+1);
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        // 如果目标文件目录不存在,则需要创建
        File file = new File(targetDir);
        if(!file.exists()){
            file.mkdirs();
        }
        try {
        // 加载模板数据(从文件路径中获取文件,其他方式,可百度查找)
        configuration.setDirectoryForTemplateLoading(new File(tempLetDir));
        // 获取模板实例
        Template template = configuration.getTemplate(templetName);
        File outFile = new File(targetDir + File.separator + targetName);
        //将模板和数据模型合并生成文件
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
        //生成文件

            template.process(dataMap, out);
            out.flush();
            out.close();
        }catch (TemplateException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * xml形式的doc文件转换为Docx格式
     * @param sourcePath 被转换文件的路径
     * @param targetPath 目标文件路径
     * @return
     */
    public static void docToDocx(String sourcePath, String targetPath){
        //Word.Application代表COM OLE编程标识,可查询MSDN得到
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        //设置Word不可见
        app.setProperty("Visible",false);
        //调用Application对象的Documents属性,获得Documents对象
        Dispatch docs = app.getProperty("Documents").toDispatch();
        //Dispatch doc = Dispatch.call(docs,"Open",sourcePath,new Variant(false),new Variant(true)).getDispatch();
        Dispatch doc = Dispatch.call(docs,"Open",sourcePath).getDispatch();
        Dispatch.call(doc,"SaveAS",targetPath,12);
        //关闭打开的Word文件
        Dispatch.call(doc,"Close",false);
        //关闭Word应用程序
        app.invoke("Quit",0);
    }

    /**
     * @Description: word文件转pdf文件
     * @param sourcePath 被转换word文档路径
     * @param targetPath 目标PDF文件路径路径
     */
    public static boolean word2pdf(String sourcePath, String targetPath) {
        ActiveXComponent app = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);
            Dispatch docs = app.getProperty("Documents").toDispatch();
            Dispatch doc = Dispatch.call(docs, "Open", sourcePath, false, true).toDispatch();
            File tofile = new File(targetPath);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", targetPath, 17); // word转PDF格式
            Dispatch.call(doc, "Close", false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (app != null) {
                app.invoke("Quit", 0); // 不保存待定的更改
            }
        }
    }
}

6、查询出数据源,添加模板位置,以及导出位置就成功啦

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Jacob 可以通过使用适当的编程工具和库来读取 Word 文档中的图片。一种常见的方法是使用 Python 编程语言中的 python-docx 库。以下是一个简单的示例代码,说明如何使用 python-docx 读取 Word 文档中的图片: 1. 首先,我们需要安装 python-docx 库。可以使用以下命令来安装: pip install python-docx 2. 确保已将要读取的 Word 文档保存在适当的路径下,并将其命名为 "example.docx"。 3. 使用以下代码来读取 Word 文档中的图片: ```python from docx import Document # 打开要读取的文档 doc = Document('example.docx') # 遍历文档中的所有段落 for paragraph in doc.paragraphs: # 遍历段落中的所有run for run in paragraph.runs: # 检查run中是否包含图片 if run._element.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}r': # 获取图片的二进制数据 image_data = run.inline.graphic.graphicData.pic.blipFill.blip.embed.rels['rEmbed'] # 保存图片数据到磁盘 with open('image.jpg', 'wb') as f: f.write(doc.part.read(image_data).blob) print("成功读取了图片!") ``` 请注意,在代码中,我们假设 Word 文档中仅有一个图片,并将其保存为名为 "image.jpg" 的文件。如果 Word 文档中有多个图片,您可能需要根据自己的需求进行相应的修改。 这就是通过使用 python-docx 库来读取 Word 文档中的图片的基本过程。通过适当的修改和处理,您可以根据自己的需求来读取和处理 Word 文档中的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值