如何使用apache POI创建word doc

时间:2014-12-15 10:17:36

标签: java apache apache-poi

我正在尝试使用apache POI创建一个word文档,但是我得到的noclassdeffound错误到目前为止我做了这个:

import java.io.File;   
import java.io.FileOutputStream;   
import org.apache.poi.xwpf.usermodel.XWPFDocument;   
import org.apache.poi.xwpf.usermodel.XWPFParagraph;   
import org.apache.poi.xwpf.usermodel.XWPFRun;   
public class DocFile {   
public void newWordDoc(String filename, String fileContent)   
   throws Exception {   
 XWPFDocument document = new XWPFDocument();   
 XWPFParagraph tmpParagraph = document.createParagraph();   
 XWPFRun tmpRun = tmpParagraph.createRun();   
 tmpRun.setText(fileContent);   
 tmpRun.setFontSize(18);   
 FileOutputStream fos = new FileOutputStream(new File("C:\\"+filename + ".doc"));   
 document.write(fos);   
 fos.close();   
  }   
  public static void main(String[] args) throws Exception {   
   DocFile app = new DocFile();   
   app.newWordDoc("testfile", "Hi hw r u?");   

 }   
}   

1 个答案:

答案 0 :(得分:0)

XWPFDocument doc = new XWPFDocument();
       XWPFParagraph paragraph = doc.createParagraph();
       XWPFRun tmpRun = paragraph.createRun();
       String folder = "E:/New folder/";
       String filename = "newDocx.docx";
       File f = new File(filename);
   
       FileOutputStream out = new FileOutputStream(new File(folder + filename));
       doc.write(out);
       System.out.println("_ok");
       doc.close();