---restore content starts---
Java Background Create Word document
Summarize the documents you read online
Share POI Tutorial Address: Http://www.tuicool.com/articles/emqaEf6
Way One,(normal creation suitable for short text without formatting requirements Word document)
1 New xwpfdocument ();//create Word file 2 Xwpfparagraph P1 = doc.createparagraph ();//Create paragraph 3 xwpfrun r1 = p1.createrun ();//Create paragraph text 4 R1.settext ("Hello World");//Set Text 5 new fileoutputstream ("D:\\simple.docx" 6 doc.write (out); Output 7 out.close (); Turn off the output stream
Method two, (suitable for the format requirements of the Word document.) )
Demo code is my working code this code is a file download function to read the selected items and generate a Word document and compress the download into a zip format
//1. Create a temporary folder existsFile Zipfolder =NewFile ("F:\\ compressed word"); if(!zipfolder.exists ()) {Zipfolder.mkdirs (); } File Wordfolder=NewFile ("f:\\ test word"); if(!wordfolder.exists ()) {Wordfolder.mkdirs (); } //2. Declaring VariablesString splitsymbol = "\ \"; byte[] Byt =New byte[1024]; intCount = 0; //Write compressed file streamFileOutputStream zipout =NewFileOutputStream (zipfolder+splitsymbol+ "Myfigs.zip"); //get the file object for ZipFile Filezip =NewFile (zipfolder+splitsymbol+ "Myfigs.zip"); //3, determine whether the user choice is one or more//Get Typestring[] Liblist = Request.getparameter ("Libsource"). Split (","); //Iteration IDstring[] IDArray = Request.getparameter ("Hidfavid"). Split (","); for(inti = 0;i<idarray.length;i++){ //4, according to the type of collection to access the database to obtain Word body content dataString Wordtext =Gettextdata (Request,idarray[i],liblist[i]); //3. Create a new wordStringBuffer buffer =NewStringBuffer ();//Because HTML must be in accordance with the format, you must add an HTML header buffer.append ("); Buffer.append (Wordtext); Buffer.append ("</body>); //generate a file nameUUID UUID =Uuid.randomuuid (); File OutFile=NewFile (wordfolder+splitsymbol+uuid.tostring () + ". Doc"); //turn data into byte arrays byte[] Contentbytes =buffer.tostring (). GetBytes (); Bytearrayinputstream ByteStream=NewBytearrayinputstream (contentbytes); //Create a Word documentPoifsfilesystem Poifsystem = new poifsfilesystem ();//temporarily don't know what it means directorynode root = poifsystem.getroot ();//Ibid. root.createdocument ("Worddocument", ByteStream);//This Worddo Cument can not be modified otherwise garbled fileoutputstream outstream = new FileOutputStream (outFile); Poifsystem.writefilesystem (OutStream); Outputs a stream to a Word document bytestream.close (); Outstream.close (); } //5. Compress with compressed streamZipoutputstream Zipoutstream =NewZipoutputstream (NewBufferedoutputstream (zipout)); Create a compressed stream corresponding file output stream filename and path//get the folder you want to compressFile File =NewFile (wordfolder+Splitsymbol); String[] FileList=file.list (); for(String filename:filelist) {//iteration to compress file clip contents FileInputStream InputStream=NewFileInputStream (file+splitsymbol+fileName); //sets the name of each item to be displayed when the compressed file is openedZipEntry ZipEntry =NewZipEntry (fileName); Zipoutstream.putnextentry (ZipEntry); Associating a compressed stream with a self item while((Count=inputstream.read (byt, 0, 1024))!=-1) {zipoutstream.write (byt,0, Count); } inputstream.close (); } zipoutstream.close (); //7. File DownloadResponse.reset (); String FileName=NewString ("Aggregate.zip". GetBytes ("UTF-8"), "Iso-8859-1"); Response.setheader ("Content-disposition", "attachment; Filename= "+fileName); File name Response.setcontenttype ("Application/octet-stream; Charset=utf-8 "); Response.setheader ("Content-length", Long.valueof (Filezip.length ()). ToString ());//after page download box file () sizeResponse.setcharacterencoding ("UTF-8"); Servletoutputstream OutputStream=Response.getoutputstream (); byte[] ByteArray =Fileutils.readfiletobytearray (Filezip);//Get the file byte array to download//8. Delete the temp directory before downloadingfile[] Zipfiles =Zipfolder.listfiles (); File[] WORDFiles=Wordfolder.listfiles (); for(File zip:zipfiles) {zip.delete (); } for(File word:wordfiles) {word.delete (); } zipfolder.delete (); Wordfolder.delete (); Outputstream.write (ByteArray); Output Outputstream.flush () using output stream;
---restore content ends---
Java Background Create Word document