1. Third-party jar package download:
To export HTML files to word in Java, you need to apply them to a third-party jar package: poi-bin-3.0-final-20070503.zip. You can download the latest website at http://poi.apache.org/official website.
Ii. development ideas:
Use Java Io to read HTML files into a temporary String object, and then use APIs provided by poi to generate WORD Documents.
3. Development source code:
Package com. Solid. util;
Import java. Io. bufferedreader;
Import java. Io. bytearrayinputstream;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. filereader;
Import java. Io. ioexception;
Import org. Apache. Poi. poifs. filesystem. directoryentry;
Import org. Apache. Poi. poifs. filesystem. documententry;
Import org. Apache. Poi. poifs. filesystem. poifsfilesystem;
/**
* Convert HTML document to Doc
* @ Author soildwang
*
*/
Public class htmltodoc {
/**
* Read HTML files to word
* @ Param filepath: Path of the HTML file
* @ Return
* @ Throws exception
*/
Public Boolean writewordfile (string filepath) throws exception {
Boolean flag = false;
Bytearrayinputstream BAIS = NULL;
Fileoutputstream Fos = NULL;
String Path = "C:/"; // write the path according to the actual situation
Try {
If (! "". Equals (PATH )){
File filedir = new file (PATH );
If (filedir. exists ()){
String content = readfile (filepath );
Byte B [] = content. getbytes ();
BAIS = new bytearrayinputstream (B );
Poifsfilesystem poifs = new poifsfilesystem ();
Directoryentry directory = poifs. getroot ();
Documententry = directory. createdocument ("worddocument", BAIS );
Fos = new fileoutputstream (path + "temp.doc ");
Poifs. writefilesystem (FOS );
BAIS. Close ();
FOS. Close ();
}
}
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (FOS! = NULL) FOS. Close ();
If (BAIS! = NULL) BAIS. Close ();
}
Return flag;
}
/**
* Read HTML files to strings
* @ Param filename
* @ Return
* @ Throws exception
*/
Public String readfile (string filename) throws exception {
Stringbuffer buffer = new stringbuffer ("");
Bufferedreader BR = NULL;
Try {
BR = new bufferedreader (New filereader (filename ));
Buffer = new stringbuffer ();
While (Br. Ready ())
Buffer. append (char) Br. Read ());
} Catch (exception e ){
E. printstacktrace ();
} Finally {
If (BR! = NULL) Br. Close ();
}
Return buffer. tostring ();
}
Public static void main (string [] ARGs) throws exception {
New htmltodoc (). writewordfile ("C:/preview4510.html ");
}
}