需要帮助的使用Apache POI微软取代2007年(HWPFDocument)文件内容 [英] Need help in replacing contents of microsoft 2007 (HWPFDocument) document using apache poi

查看:2402
本文介绍了需要帮助的使用Apache POI微软取代2007年(HWPFDocument)文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有IM感觉它不是很容易使用Apache POI API,像任何其他Java API的,因为我很困惑这是在修改的docx的内容,这种情况下需要的所有jar文件我使用POI-scratchpad-第一3.1 beta1.jar和POI-3.2-FINAL-20081019.jar(HWPFDocument类),但在一些论坛上有人用不同的版本一起指向其他一些POI jar文件不同的名字...(指示使用类XSSFDocument)。

First of all i m feeling its not very easy to use Apache POI API, as like any other java API's because i am confused which are all jar files needed in case of modifying contents of docx for this i am using poi-scratchpad-3.1-beta1.jar and poi-3.2-FINAL-20081019.jar (HWPFDocument class) but in some forums it was pointing to some other poi jar file different name along with different versions... (indicating to use the class XSSFDocument).

我的要求是一些文件的内容来替换一些文本。
为此我使用以下code,以取代2007年微软(的docx文件)的内容。我已经结束了与错误说..

My requirement is to replace some text with some file contents. for this i am using the following code to replace the contents of microsoft 2007 (docx file). and i have ended up with the error saying..

org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的数据似乎是在Office 2007+ XML。 POI只支持OLE2 Office文档

import java.io.*;
import org.apache.poi.POIDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class GenerateTicket {

    public static void main(String[] args) {

        final String FILE_PATH = "/home/mateen/Desktop/Ticket.docx";
        try {
            File file = new File(FILE_PATH);
            FileInputStream fis = new FileInputStream(file);
            POIFSFileSystem poifs = new POIFSFileSystem(fis);
            HWPFDocument doc = new HWPFDocument(poifs);
            Range range = doc.getRange();
            CharacterRun run = range.insertAfter("Hello POI CORE/HWPF !!!");
            run.setBold(true);
            run.setItalic(true);
            doc.write(new FileOutputStream("/home/mateen/Desktop/FromTicket.Docx"));

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

所以...


  • 有没有谁知道为什么需要两个单独的jar文件的任何机构

  • 我能做些什么让我的code的工作?

推荐答案

HWPF作品上了年纪的,基于OLE2 .doc文件。如果你想与新的OOXML文件的.docx工作,你需要使用XWPF来代替。

HWPF works on the older, OLE2 based .doc files. If you want to work with the newer OOXML .docx files, you need to use XWPF instead.

接下来,您正在使用旧版本的POI,而且是不匹配的。您应该确保您使用的是最新版本,并确保所有的POI罐子都来自同一个版本。要制定出你想要的罐子,和他们有什么相关性,看到href=\"http://poi.apache.org/overview.html\" rel=\"nofollow\">详情组件概览页面的

Next, you're using older versions of POI, and mismatched ones at that. You should make sure you're using the latest version, and make sure all your POI jars are all from the same version. To work out what Jars you want, and what dependencies they have, see the component overview page for details.

这篇关于需要帮助的使用Apache POI微软取代2007年(HWPFDocument)文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆