使用 JavatoWord API 将数据附加到 Word 失败
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论