Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java POI如何生成word文档?并动态添加数据?我做了以下几件事_Java_Ms Word_Apache Poi - Fatal编程技术网

Java POI如何生成word文档?并动态添加数据?我做了以下几件事

Java POI如何生成word文档?并动态添加数据?我做了以下几件事,java,ms-word,apache-poi,Java,Ms Word,Apache Poi,首先,我需要在我的程序中创建一个文件,如下所示: File file = new File(jChooser.getSelectedFile().getPath()+".docx"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e1) { e1.printStackTrace(); } } OPCPackage pack = POIXML

首先,我需要在我的程序中创建一个文件,如下所示:

File file = new File(jChooser.getSelectedFile().getPath()+".docx");
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    } 
}
OPCPackage pack = POIXMLDocument.openPackage(path); //this path is the file's path;  
XWPFDocument doc = new XWPFDocument(pack);
XWPFParagraph paragraph = doc.createParagraph();
paragraph.setAlignment(wordFont.getAlign());
XWPFRun run = paragraph.insertNewRun(paragraph.getRuns().size());
run.setText(data);
Exception in thread "AWT-EventQueue-0"org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'C:\Users\sks\Desktop\17.docx'
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:103)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:207)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:186)
at org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:67)
然后,我需要像这样添加一些信息:

File file = new File(jChooser.getSelectedFile().getPath()+".docx");
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    } 
}
OPCPackage pack = POIXMLDocument.openPackage(path); //this path is the file's path;  
XWPFDocument doc = new XWPFDocument(pack);
XWPFParagraph paragraph = doc.createParagraph();
paragraph.setAlignment(wordFont.getAlign());
XWPFRun run = paragraph.insertNewRun(paragraph.getRuns().size());
run.setText(data);
Exception in thread "AWT-EventQueue-0"org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'C:\Users\sks\Desktop\17.docx'
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:103)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:207)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:186)
at org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:67)
当我运行它时,它有如下错误:

File file = new File(jChooser.getSelectedFile().getPath()+".docx");
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    } 
}
OPCPackage pack = POIXMLDocument.openPackage(path); //this path is the file's path;  
XWPFDocument doc = new XWPFDocument(pack);
XWPFParagraph paragraph = doc.createParagraph();
paragraph.setAlignment(wordFont.getAlign());
XWPFRun run = paragraph.insertNewRun(paragraph.getRuns().size());
run.setText(data);
Exception in thread "AWT-EventQueue-0"org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'C:\Users\sks\Desktop\17.docx'
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:103)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:207)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:186)
at org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:67)
线程“AWT-EventQueue-0”org.apache.poi.openxml4j.exceptions.invalidooperationexception中的异常:无法打开指定的文件:“C:\Users\sks\Desktop\17.docx”
在org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:103)
位于org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:207)
位于org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:186)
位于org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:67)

我如何解决这个问题?

我已经解决了这个问题,只需在创建XWPFDocument时使用它:

                XWPFDocument xwpfDocument = new XWPFDocument();
                File file = new  File("hello.docx");
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } 
                }
                try {
                    xwpfDocument.write(new FileOutputStream(file));
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

我已经解决了这个问题,只需在创建XWPFDocument时使用它:

                XWPFDocument xwpfDocument = new XWPFDocument();
                File file = new  File("hello.docx");
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } 
                }
                try {
                    xwpfDocument.write(new FileOutputStream(file));
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

你看过这个吗?我很确定POI不喜欢从空文件中“读取”,也就是说,你必须告诉它,如果文件不存在,它应该创建基础,而不是从文件中读取(例如,stacktrace显示它试图从一个zip文件中读取,因为它是空的,所以它根本不是zip)。我想多次写入word文件,所以我必须按路径写入。就像我写入txt文件一样,附加一些内容是的,这就是为什么你需要告诉POI它是否需要创建新文档或读取并更新现有文档。你能帮我怎么做吗?看看我发布的链接。有很多例子可以让你开始学习。你看过这个吗?我很确定POI不喜欢从空文件中“读取”,也就是说,你必须告诉它,如果文件不存在,它应该创建基础,而不是从文件中读取(例如,stacktrace显示它试图从一个zip文件中读取,因为它是空的,所以它根本不是zip)。我想多次写入word文件,所以我必须按路径写入。就像我写入txt文件一样,附加一些内容是的,这就是为什么你需要告诉POI它是否需要创建新文档或读取并更新现有文档。你能帮我怎么做吗?看看我发布的链接。有很多例子可以让你开始学习。