Convert HTML to word

Source: Internet
Author: User

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 ");

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.