gpt4 book ai didi

java - 使用 XWPFDocument 将文本附加到现有 Word 文件

转载 作者:行者123 更新时间:2023-12-02 11:13:27 24 4
gpt4 key购买 nike

我正在尝试将文本和屏幕截图附加到现有的 Word 文件中。但每次执行以下代码时,我都会收到错误:

org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long) at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:74) at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:57) at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:135) at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:175) at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:209) at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:98) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) at org.apache.poi.xwpf.usermodel.XWPFDocument.(XWPFDocument.java:116) at test.tester.(tester.java:44) at test.tester.main(tester.java:100)

创建文件失败,先获取 ss

java.lang.NullPointerException at test.tester.setText(tester.java:62) at test.tester.main(tester.java:103)

代码如下:

 public class tester{

FileOutputStream fos = null;
XWPFDocument doc = null;
// create para and run
XWPFParagraph para = null;
XWPFRun run = null;

File file = null;

public tester() {
try {

file = new File("WordDocWithImage.docx");

writeToWord();
//doc = new XWPFDocument();
doc = new XWPFDocument(OPCPackage.open(file));
//doc = new XWPFDocument(new FileInputStream("WordDocWithImage.docx"));
para = doc.createParagraph();
run = para.createRun();
para.setAlignment(ParagraphAlignment.CENTER);
} catch (Exception e) {
e.printStackTrace();
System.out.println("failed to create file");
}
}

public FileOutputStream writeToWord() throws FileNotFoundException {
fos = new FileOutputStream("WordDocWithImage.docx");
return fos;

}

public void setText(String text) {

run.setText(text);
}

public void takeScreenshot() throws IOException, AWTException, InvalidFormatException {

// Take screenshot
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);

// convert buffered image to Input Stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(screenFullImage, "jpeg", baos);
baos.flush();
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
baos.close();

// add image to word doc
run.addBreak();
run.addPicture(bis, XWPFDocument.PICTURE_TYPE_JPEG, "image file", Units.toEMU(450), Units.toEMU(250)); // 200x200
// pixels
bis.close();

}

public void writeToFile() {
try {
// write word doc to file

doc.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

tester t = new tester();
try {
System.out.println("Taking first ss");
t.setText("First Text");
t.takeScreenshot();
System.out.println("Taking second ss");
t.setText("Second Text");
t.takeScreenshot();
t.writeToFile();
} catch(Exception e) {
e.printStackTrace();
}
}

}

请帮忙。

最佳答案

The supplied file was empty (zero bytes long)

问题在于获取此行中的 WordDocWithImage 文件。

file = new File("WordDocWithImage.docx"); 

找不到 docx 文件。您应该检查文件的位置并给出其中的真实路径。

编辑:您需要将outputStream更改为不同的位置。您可以创建一个子文件夹。我让 ss 尝试这个。

public FileOutputStream writeToWord() throws FileNotFoundException {
fos = new FileOutputStream("path/subFolder/WordDocWithImage.docx");
return fos;

}

注意:我已经尝试过该代码。

关于java - 使用 XWPFDocument 将文本附加到现有 Word 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50447409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com