使用 JavatoWord API 将数据附加到 Word 失败

发布于 2024-11-14 07:54:36 字数 2272 浏览 1 评论 0原文

我正在尝试使用 JavatoWord API 将数据添加到自动化中的 Word 文档,但当我附加数据时,Word 已损坏。有人可以帮忙吗?

import java.io.*;
import java.text.*;
import java.util.*;
import word.api.interfaces.IDocument;
import word.w2004.Document2004;
import word.w2004.Document2004.Encoding;
import word.w2004.elements.BreakLine;
import word.w2004.elements.Image;
import word.w2004.elements.ImageLocation;
import word.w2004.elements.Paragraph;
import word.w2004.elements.ParagraphPiece;

public void AppendtoWord() throws Exception 
{
  String strScrShotsFile = "C:/Test.doc/";
  String ScrShotsFile = "C:/test.png";   
  File fileScrShotsFile = new File (strScrShotsFile);
  boolean exists =  (fileScrShotsFile).exists();
  PrintWriter writer = null; 
  myDoc = new Document2004();

  if (!exists) {     
    try {
      writer = new PrintWriter(fileScrShotsFile);
      myDoc.encoding(Encoding.UTF_8); 
      myDoc.company("Comp Inc");
      myDoc.author("Test Automation Teams");
      myDoc.title("Application Automation Test Results");
      myDoc.subject("Screen Shots");                              
      myDoc.setPageOrientationLandscape();
      myDoc.addEle(BreakLine.times(2).create());// Document Header and Footer         
      myDoc.addEle(BreakLine.times(2).create()); 
      myDoc.getHeader().addEle(
        Paragraph.withPieces(
          ParagraphPiece.with("Screenshots for test case: "),
          ParagraphPiece.with( "Test" ).withStyle().bold().create()
        ).create()
      );
      myDoc.getFooter().addEle(Paragraph.with(ScrShotsFile).create());  // Images
      myDoc.addEle(BreakLine.times(1).create());
      myDoc.addEle(Paragraph.with("Test").withStyle().bgColor("RED").create());
      writer.println(myDoc.getContent());
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      writer.close();                          
    }     
  } else {   
    PrintWriter writer1= null;
    try {
      writer1 = new PrintWriter(new FileWriter(fileScrShotsFile,true));         
      myDoc.addEle(Paragraph.with("This is Important").withStyle().bgColor("RED").create());
      writer1.println(myDoc.getContent());
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      writer1.close();  
    }
  }

I'm trying to add data to Word document in my automation using JavatoWord API and Word is getting corrupted when I append the data. Can anyone help?

import java.io.*;
import java.text.*;
import java.util.*;
import word.api.interfaces.IDocument;
import word.w2004.Document2004;
import word.w2004.Document2004.Encoding;
import word.w2004.elements.BreakLine;
import word.w2004.elements.Image;
import word.w2004.elements.ImageLocation;
import word.w2004.elements.Paragraph;
import word.w2004.elements.ParagraphPiece;

public void AppendtoWord() throws Exception 
{
  String strScrShotsFile = "C:/Test.doc/";
  String ScrShotsFile = "C:/test.png";   
  File fileScrShotsFile = new File (strScrShotsFile);
  boolean exists =  (fileScrShotsFile).exists();
  PrintWriter writer = null; 
  myDoc = new Document2004();

  if (!exists) {     
    try {
      writer = new PrintWriter(fileScrShotsFile);
      myDoc.encoding(Encoding.UTF_8); 
      myDoc.company("Comp Inc");
      myDoc.author("Test Automation Teams");
      myDoc.title("Application Automation Test Results");
      myDoc.subject("Screen Shots");                              
      myDoc.setPageOrientationLandscape();
      myDoc.addEle(BreakLine.times(2).create());// Document Header and Footer         
      myDoc.addEle(BreakLine.times(2).create()); 
      myDoc.getHeader().addEle(
        Paragraph.withPieces(
          ParagraphPiece.with("Screenshots for test case: "),
          ParagraphPiece.with( "Test" ).withStyle().bold().create()
        ).create()
      );
      myDoc.getFooter().addEle(Paragraph.with(ScrShotsFile).create());  // Images
      myDoc.addEle(BreakLine.times(1).create());
      myDoc.addEle(Paragraph.with("Test").withStyle().bgColor("RED").create());
      writer.println(myDoc.getContent());
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      writer.close();                          
    }     
  } else {   
    PrintWriter writer1= null;
    try {
      writer1 = new PrintWriter(new FileWriter(fileScrShotsFile,true));         
      myDoc.addEle(Paragraph.with("This is Important").withStyle().bgColor("RED").create());
      writer1.println(myDoc.getContent());
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      writer1.close();  
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文