使用XWPFDocument

时间:2018-05-21 10:59:22

标签: java file apache-poi file-handling

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

  
    

org.apache.poi.EmptyFileException:提供的文件为空(零字节长)at     org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:74)at at     org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:57)at at     org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:135)       在     org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:175)       在     org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:209)       在org.apache.poi.openxml4j.opc.ZipPackage。(ZipPackage.java:98)       在org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)       在org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)at     org.apache.poi.xwpf.usermodel.XWPFDocument。(XWPFDocument.java:116)       在test.tester。(tester.java:44)at     test.tester.main(tester.java:100)

  
     

无法创建文件采取第一个ss

     
    

test.tester.setText的java.lang.NullPointerException(tester.java:62)         在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();
        }
    }

    }

请协助。

1 个答案:

答案 0 :(得分:1)

  

提供的文件为空(零字节长)

问题是如何在此行中获取 WordDocWithImage 文件。

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

找不到docx文件。您应该检查文件的位置并在那里给出真正的路径。

编辑:您需要将outputStream更改为其他位置。您可以创建一个子文件夹。我试过这个。

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

    }

注意:我试过了代码。