Не удается добавить данные в Word с помощью API JavatoWord - PullRequest
2 голосов
/ 09 июня 2011

Я пытаюсь добавить данные в документ Word в моей автоматизации, используя JavatoWord API, и 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();  
    }
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...