How can I create a simple docx file with Apache POI?

前端 未结 2 1644
挽巷
挽巷 2020-12-28 14:09

I\'m searching for a simple example code or a complete tutorial how to create a docx file with Apache POI and its underlying openxml4j.

I t

相关标签:
2条回答
  • 2020-12-28 14:49

    Here is how you can create a simple docx file with POI :

    XWPFDocument document = new XWPFDocument();
    XWPFParagraph tmpParagraph = document.createParagraph();
    XWPFRun tmpRun = tmpParagraph.createRun();
    tmpRun.setText("LALALALAALALAAAA");
    tmpRun.setFontSize(18);
    document.write(new FileOutputStream(new File("yourpathhere")));
    document.close();
    
    0 讨论(0)
  • 2020-12-28 14:54
    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:\\Users\\amitabh\\Pictures\\pics\\"+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?");   
    
        }   
      }   
    
    0 讨论(0)
提交回复
热议问题