java通过POI和jacob实现word文档的在线预览和下载

1 篇文章 0 订阅
1 篇文章 0 订阅

通过POI和jacob可以实现word文档的在线预览和下载。

首先,引入以下maven依赖。

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.3</version>
        </dependency>

以上maven依赖主要是POI的相关jar包,话不多说,开始上课。

首先通过word模板实现word文档的数据填充,在下载生成的word文档。

(一)生成word模板

打开word文档,在需要填充数据的地方以${}的方式进行填写。

将该word文档另存为xml文件。

通过记事本或者其他编辑工具(我用的是Notepad++)代开xml文件并编辑如下:

保存之后把xml文件另存为ftl文,该文件便是word的模板。

(二)模板准备准备好了,开始撸代码。

package bingosoft.metro.sendfile.utils;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class WordGenerator {
    private static Configuration configuration = null;
    private static Map<String, Template> allTemplates = null;
    private static final String templateFolder = "D:\\Test\\Template\\";

    static {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
//        configuration.setClassForTemplateLoading(WordGenerator.class, "D:\\Test\\Template");
        try{
            configuration.setDirectoryForTemplateLoading(new File(templateFolder));
        }catch(Exception e){
            e.printStackTrace();
        }
        allTemplates = new HashMap<String,Template>();	// Java 7 钻石语法
        try {
            //南宁轨道交通集团有限责任公司方案优化审查申请表
            allTemplates.put("GroupGHSendFile", configuration.getTemplate("GroupGHSendFile.ftl"));

            //南宁轨道交通集团有限责任公司方案优化审批表
            allTemplates.put("GroupJWSendFile",configuration.getTemplate("GroupJWSendFile.ftl"));

            //施工图变更申请表(业主方)
            allTemplates.put("GroupTWSendFile",configuration.getTemplate("GroupTWSendFile.ftl"));

            //  施工图变更申请表(非业主方I、II、III类)
            allTemplates.put("GroupDWSendFile",configuration.getTemplate("GroupDWSendFile.ftl"));

            //施工图变更申请表(非业主方IV类)
            allTemplates.put("GroupInSendFile",configuration.getTemplate("GroupInSendFile.ftl"));

            //工程洽商审批表
            allTemplates.put("ResourceInSendFile",configuration.getTemplate("ResourceInSendFile.ftl"));

            //招标文件技术部分审批表 附件3
            allTemplates.put("ResourceOutSendFile",configuration.getTemplate("ResourceOutSendFile.ftl"));

            //招标文件技术部分审批表 附件4
            allTemplates.put("OperateInSendFile",configuration.getTemplate("OperateInSendFile.ftl"));
//            allTemplates.put("OperateOutSendFile",configuration.getTemplate("OperateOutSendFile.ftl"));
//            allTemplates.put("ConstructInSendFile",configuration.getTemplate("ConstructInSendFile.ftl"));
//            allTemplates.put("ConstructOutSendFile",configuration.getTemplate("ConstructOutSendFile.ftl"));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private WordGenerator() {
        throw new AssertionError();
    }

    public static File createDoc(Map<?, ?> dataMap, String pathAndFileName) {
        //创建文件
        File f = new File(pathAndFileName);
        //获取类型
        String type = (String) dataMap.get("appCode");
        //类型模板
        Template t = allTemplates.get(type);
        try {
            Writer w = new FileWriter(f);
            t.process(dataMap, w);
            w.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        return f;
    }
public static void docToDocx(String pathAndFileName,String targetFile){
        //Word.Application代表COM OLE编程标识,可查询MSDN得到
        ActiveXComponent activeXComponent = new ActiveXComponent("Word.Application");
        //设置Word不可见
        activeXComponent.setProperty("Visible", false);
        //调用Application对象的Documents属性,获得Documents对象
        Dispatch dispatches = activeXComponent.getProperty("Documents").toDispatch();
        Dispatch dispatch = Dispatch.call(dispatches, "Open", pathAndFileName).getDispatch();
        Dispatch.call(dispatch, "SaveAS", targetFile, 12);
        //关闭打开的Word文件    
        Dispatch.call(dispatch, "Close", true);
        //关闭Word应用程序    
        activeXComponent.invoke("Quit", 0);
    }
}

通过模板读取${}的位置,并通过map集合将数据传进去上面的createDoc()方法,生成word文档。

不过此时生成的word文档依然是xml格式的,通过docToDocx()方法可以实现在xml格式的文档转化为docx格式的文档。

不过需要对应的jacob依赖。

jacob我采用的是jacob-1.19,在引入jacob的依赖包之前,需要先将jacob的jar包和.dll文件放到相应的位置。

以下链接是jacob-1.19的下载链接,你也可以去官网下载。

https://download.csdn.net/download/weixin_42311540/10763277

解压文件之后,会有

将jacob的jar包保存到jdk的jre的bin目录下,E:\JAVA\Java\jre8\bin

将相关的dll文件保存到jdk的jre目录下的ext文件目录下,E:\JAVA\Java\jre8\lib\ext

接下来便是在maven中导入相关的jar包,

接下来,word的文档下载便实现了。

(三)生成的word文档我们可以再转化为html页面。

代码如下:

package bingosoft.metro.sendfile.utils;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.List;

public class HtmlGenerator{

    public static void wordToHtml(String path,String fileName) throws Exception{
        File file = new File(path+fileName);
        InputStream inputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

        //处理2007版本
        if(file.getName().endsWith(".docx")||file.getName().endsWith(".DOCX")){
            XWPFDocument xwpfDocument = new XWPFDocument(bufferedInputStream);

            System.out.println(xwpfDocument.getTables());

            File imageFolderFile = new File(path);
            XHTMLOptions options = XHTMLOptions.create().URIResolver( new BasicURIResolver("./"));

            options.setExtractor(new FileImageExtractor(imageFolderFile));

            OutputStream out = new FileOutputStream(new File(path+fileName.substring(0,fileName.indexOf(".")+1)+"html"));

            XHTMLConverter.getInstance().convert(xwpfDocument, out, options);
            out.close();
        }
        else{
            //处理2003版本
            HWPFDocument hwpfDocument = new HWPFDocument(new POIFSFileSystem(file,false));
            WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
                    DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
            );

            wordToHtmlConverter.setPicturesManager(new PicturesManager() {
                @Override
                public String savePicture(byte[] bytes, PictureType pictureType, String s, float v, float v1) {
                    return s;
                }
            });
            wordToHtmlConverter.processDocument(hwpfDocument);
            List pics = hwpfDocument.getPicturesTable().getAllPictures();
            if(pics != null){
                for(int i=0;i<pics.size();i++){
                    Picture pic = (Picture) pics.get(i);
                    try{
                        pic.writeImageContent(new FileOutputStream(path+pic.suggestFullFileName()));
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
            Document htmlDocument = wordToHtmlConverter.getDocument();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            DOMSource domSource = new DOMSource(htmlDocument);
            StreamResult streamResult = new StreamResult(outStream);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer serializer = tf.newTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.METHOD, "html");
            serializer.transform(domSource, streamResult);
            outStream.close();
            String content = new String(outStream.toByteArray());
            System.out.println(fileName.substring(0,fileName.indexOf(".")+1));
            FileUtils.writeStringToFile(new File(path, fileName.substring(0,fileName.indexOf(".")+1)+"html"), content, "utf-8");
        }
    }

}

并且能够实现2003版本和2007版本的不同兼容。

(四)功能代码已经全部完成,接下我们进入测试。

以下是生成的word文档。

生成html的方法:

接下来生成的html文档如下:

其实生成的word文档和html文档都有瑕疵,word文档的填充内容还没有解决表格大小自适应的问题,生成的html文档发现内容位置问题。如果有博友知道的话,欢迎一起交流。

以下附上两个工具类的下载链接。

https://download.csdn.net/download/weixin_42311540/10763225

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
JACOB里的总共有两个包: com jacob activeX:JACOB可以通过它调度(Dispatch)activeX 控件 com jacob com:JACOB通过它调用系统DLL (activeX是由微软公司推出的用于Internet的技术 以前曾经被称为OLE 和OCX activeX web插件可以用于各种非HTML的工作 比如 对MicrosoftExcel 或 MicrosoftWord 文件做操作 解析JS VB脚本 播放FA有一点可以确定的是在JACOB内部 com jacob activeX是建立在com jacob com之上的 而com jacob com中有一个很基础的类com jacob com JacobObject 其中比较常用的两个类com jacob com Dispatch和com jacob com Variant便是继承自它 (com jacob com Dispatch;里面提供了调度MS windows系统API 比如进程的管理 com jacob com Variant; 里面数据类型的转换的方法 Variant类本身是JACOB的最最基本的数据类 他可以被转换成任何的类 如: 使用toDispatch();可以将Variant转化成Dispatch对象 ) com jacob com中还有其他的一些类不是建立在JacobObject之上的 如com jacob com ComThread(负责COM线程管理 ) com jacob com所有的类 包括上面说到的JacobObject ComThread 都是通过com jacob com LibraryLoader中的loadJacobLibrary()方法: static { LibraryLoader loadJacobLibrary ; } 调用jacob dll内的方法 比如:Dispatch java就通过LibraryLoader和Dispatch h Dispatch cpp建立联系 从而建立起javajacob dll到jni的之间的联系 LSH 调用Microsoft MediaPlayer等 )">JACOB里的总共有两个包: com jacob activeX:JACOB可以通过它调度(Dispatch)activeX 控件 com jacob com:JACOB通过它调用系统DLL (activeX是由微软公司推出的用于Internet的技术 以前曾经被称为OLE 和OCX activeX web插件可以用于各 [更多]
### 回答1: 要实现Java Vue实现Word在线,可以通过以下步骤: 1. 在Java后端,使用Apache POI库来读取Word文档内容。POI库可以解析Word文档的各种元素,例如段落、表格、图片等。 2. 将读取到的Word文档内容以HTML格式返回给Vue前端。可以使用POI库提供的API将Word内容转换成HTML格式,然后通过Java后端将HTML内容返回给前端。 3. 在Vue前端,使用相应的组件来渲染并显示HTML内容。可以使用著名的富文本编辑器Quill或者其他类似的库,将返回的HTML内容进行渲染,以实现Word文档的显示。 4. 在Vue前端,添加相应的控件和功能来支持在线Word文档。可以添加双击文档打开、缩放、左右滚动等交互功能,以提升用户体验。 5. 在Vue前端,添加样式和布局来美化页面。可以采用CSS框架如Bootstrap或Element UI来实现页面的美化和响应式布局,提供更好的用户界面。 需要注意的是,在这个过程中,Java后端负责处理Word文档的读取和转换,将其转化为HTML格式。而Vue前端负责将这个HTML内容进行渲染和显示,提供给用户在线的功能。这样,用户就可以通过浏器直接在线Word文档了。 ### 回答2: 要实现Java Vue实现Word在线,可以通过以下步骤进行: 1. 首先,使用Vue框架构建前端页面。可以使用Vue的脚手架工具来创建项目,并在项目中安装所需的依赖项(如axios用于发送HTTP请求,element-ui或其他UI库用于页面布局等)。 2. 在Vue的前端页面中创建一个组件,用于上传Word文件。可以使用HTML的`input`元素,并为其添加`type="file"`,当用户选择Word文件时,会触发相应的事件。 3. 在Vue组件中使用axios发送POST请求,将Word文件提交给后端。可以使用axios的`FormData`对象来创建一个表单对象,并将Word文件附加到表单中。 4. 在后端使用Java编写API,用于接收前端提交的Word文件。可以使用Spring Boot框架来搭建Java后端。在API中,使用Java的文件处理功能将接收到的Word文件保存到服务器本地的某个目录中。 5. 使用Java的Apache POI库来解析Word文件。Apache POI提供了对Word文件的读取和操作功能,在后端使用POI库来读取Word文件的内容。 6. 后端将读取到的Word文件内容返回给前端,可以将内容转换为HTML格式,并将其嵌入到前端页面中的一个`<iframe>`元素中。 7. 前端收到后端返回的HTML内容后,使用`<iframe>`元素将其展示在页面中。可以通过设置`<iframe>`元素的`srcdoc`属性,将HTML内容嵌入其中,并通过CSS样式设置适当的宽度和高度。 8. 最后,用户就可以在前端页面中上传的Word文件了。可以通过滚动页面来查看整个文件内容,或者自定义一些导航按钮或操作来进行文件的查看和操作。 通过以上步骤,就可以实现Java Vue实现Word在线的功能了。要注意的是,这只是一个简单的实现示例,具体的实现方式可能因具体项目需求而有所差异。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值