编辑.doc文件头java

编辑.doc文件头java,java,apache-poi,doc,Java,Apache Poi,Doc,我需要编辑.doc和.docx文件头并维护文档的样式 我试着通过使用: poi api:我设法读取了文件头,但找不到如何替换其中的文本并将结果保存为原始样式 public static void mFix(String iFilePath , HashMap<String, String> iOldNewCouples) { aOldNewCouples = iOldNewCouples; try { if(iFilePath==null)

我需要编辑.doc和.docx文件头并维护文档的样式

我试着通过使用:

  • poi api:我设法读取了文件头,但找不到如何替换其中的文本并将结果保存为原始样式

    public static void mFix(String iFilePath , HashMap<String, String> iOldNewCouples) 
    {
    
        aOldNewCouples = iOldNewCouples;
    
         try {
             if(iFilePath==null)
                 return;
    
               File  file = new File(iFilePath);
               FileInputStream fis=new FileInputStream(file.getAbsolutePath());
               HWPFDocument document=new HWPFDocument(fis);
               WordExtractor extractor = new WordExtractor(document); // read the doc as rtf
    
               String fileData = extractor.getHeaderText();
               String fileDataResult =fileData ;
    
               for (Entry<String, String> entry : aOldNewCouples.entrySet())
               {
                    if(fileData.contains(entry.getKey())) {
                         System.out.println("replace " +entry.getKey());
                        fileDataResult = fileData.replace(entry.getKey(), entry.getValue()); 
                      } 
               }
               document.getHeaderStoryRange().replaceText(fileData, fileDataResult);
               saveWord(iFilePath ,document);
               fis.close();
    
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace( );
        }
    
    }
    
    private static void saveWord(String filePath, HWPFDocument doc) throws FileNotFoundException, IOException
    {
         FileOutputStream fileOutputStream = null;
                try{
                    fileOutputStream = new FileOutputStream(new File(filePath.replace(".doc", "-test.doc")));
                    BufferedOutputStream buffOutputStream = new BufferedOutputStream(fileOutputStream);
                    doc.write(buffOutputStream);
                    buffOutputStream.close();
                    fileOutputStream.close();
    
    
                }
                finally{
                  if( fileOutputStream != null)
                      fileOutputStream.close();
                }
    }
    
    publicstaticvoidmfix(字符串iFilePath,HashMap iOldNewCouples)
    {
    aOldNewCouples=iOldNewCouples;
    试一试{
    如果(iFilePath==null)
    返回;
    文件=新文件(iFilePath);
    FileInputStream fis=新的FileInputStream(file.getAbsolutePath());
    HWPF文件=新的HWPF文件(fis);
    WordExtractor extractor=newwordextractor(document);//以rtf格式读取文档
    String fileData=extractor.getHeaderText();
    字符串fileDataResult=fileData;
    for(条目:aOldNewCouples.entrySet())
    {
    if(fileData.contains(entry.getKey())){
    System.out.println(“replace”+entry.getKey());
    fileDataResult=fileData.replace(entry.getKey(),entry.getValue());
    } 
    }
    document.getHeaderStoryRange().replaceText(fileData,fileDataResult);
    保存字(iFilePath、文档);
    fis.close();
    }捕获(IOE异常){
    //TODO自动生成的捕捉块
    e、 printStackTrace();
    }
    }
    私有静态void saveWord(字符串filePath,HWPFDocument doc)抛出FileNotFoundException,IOException
    {
    FileOutputStream FileOutputStream=null;
    试一试{
    fileOutputStream=newfileoutputstream(新文件(filePath.replace(“.doc”,“-test.doc”));
    BufferedOutputStream buffOutputStream=新的BufferedOutputStream(fileOutputStream);
    文件写入(buffOutputStream);
    buffOutputStream.close();
    fileOutputStream.close();
    }
    最后{
    if(fileOutputStream!=null)
    fileOutputStream.close();
    }
    }
    
  • 我尝试了docx的doc4japi:我找到了如何编辑标题,但没有找到如何保持样式

    public static void mFix(String iFilePath , HashMap<String, String> iOldNewCouples) {
    
        aOldNewCouples = iOldNewCouples;
        WordprocessingMLPackage output;
        try {
            output = WordprocessingMLPackage.load(new java.io.File(iFilePath));
            replaceText(output.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getDefaultHeader());
            output.save(new File(iFilePath));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    
    public static void replaceText(ContentAccessor c)    throws Exception
    {
        for (Object p: c.getContent())
        {
          if (p instanceof ContentAccessor)
            replaceText((ContentAccessor) p);
    
          else if (p instanceof JAXBElement)
          {
            Object v = ((JAXBElement) p).getValue();
    
            if (v instanceof ContentAccessor)
              replaceText((ContentAccessor) v);
    
            else if (v instanceof org.docx4j.wml.Text)
            {
              org.docx4j.wml.Text t = (org.docx4j.wml.Text) v;
              String text = t.getValue();
              if (text != null)
              {
                  boolean flag = false;
                  for (Entry<String, String> entry : aOldNewCouples.entrySet())
                   {
                        if(text.contains(entry.getKey())) {
                            flag =true;
                            text = text.replaceAll(entry.getKey(), entry.getValue());
                             t.setSpace("preserve"); 
                             t.setValue(text);
    
                          } 
                   }
                }
            }
          }
        }
      }
    
    publicstaticvoidmfix(字符串iFilePath,HashMap iOldNewCouples){
    aOldNewCouples=iOldNewCouples;
    字处理mlpackage输出;
    试一试{
    output=WordprocessingMLPackage.load(新的java.io.File(iFilePath));
    replaceText(output.getDocumentModel().getSections().get(0.getHeaderFooterPolicy().getDefaultHeader());
    保存(新文件(iFilePath));
    }
    捕获(例外e){
    e、 printStackTrace();
    }
    }
    公共静态void replaceText(ContentAccessor c)引发异常
    {
    对于(对象p:c.getContent())
    {
    if(ContentAccessor的p实例)
    replaceText((ContentAccessor)p);
    else if(JAXBElement的p实例)
    {
    objectv=((JAXBElement)p.getValue();
    if(v instanceof ContentAccessor)
    replaceText((ContentAccessor)v);
    else if(v instanceof org.docx4j.wml.Text)
    {
    org.docx4j.wml.Text t=(org.docx4j.wml.Text)v;
    字符串text=t.getValue();
    如果(文本!=null)
    {
    布尔标志=假;
    for(条目:aOldNewCouples.entrySet())
    {
    if(text.contains(entry.getKey())){
    flag=true;
    text=text.replaceAll(entry.getKey(),entry.getValue());
    t、 设置空间(“保留”);
    t、 设定值(文本);
    } 
    }
    }
    }
    }
    }
    }
    
  • 我想为这些api提供一些示例

    如果有其他针对Java项目的免费解决方案,请用示例编写它们

    谢谢


    Tami

    您尝试了什么代码?我添加了我的代码,很抱歉没有添加好…您可以通过保留格式替换标题文本吗?@tamih