Poi exports data to an instance of Word

Source: Internet
Author: User
Export the processed webpage or background data to word through POI. The open/save dialog box is displayed in the export.

Import java. Io .*;
Import java. util .*;
Import org. Apache. Poi. poifs. filesystem .*;
Import org. Apache. Poi. util. littleendian;

Public class wordtest {
Public wordtest (){
}
Public static Boolean writewordfile (string path, string content ){
Boolean W = false;
Try {

// Byte B [] = content. getbytes ("ISO-8859-1 ");
Byte B [] = content. getbytes ();

Bytearrayinputstream BAIS = new bytearrayinputstream (B );

Poifsfilesystem FS = new poifsfilesystem ();
Directoryentry directory = FS. getroot ();

Documententry de = directory. createdocument ("worddocument", BAIS );

Fileoutputstream ostream = new fileoutputstream (PATH );

FS. writefilesystem (ostream );

BAIS. Close ();
Ostream. Close ();

} Catch (ioexception e ){
E. printstacktrace ();
}
Return W;
}
Public static void main (string [] ARGs ){
Boolean B = writewordfile ("E: // test.doc", "hello ");
}
}
/**//*
Public String extracttext (inputstream in) throws ioexception {
Arraylist text = new arraylist ();
Poifsfilesystem fsys = new poifsfilesystem (in );

Documententry headerprops = (documententry) fsys. getroot (). getentry ("worddocument ");
Documentinputstream din = fsys. createdocumentinputstream ("worddocument ");
Byte [] header = new byte [headerprops. getsize ()];

Din. Read (header );
Din. Close ();
// Prende le informazioni Dall 'header del incluento
Int info = littleendian. getshort (header, 0xa );

Boolean usetable1 = (info & 0x200 )! = 0;

// Boolean usetable1 = true;

// Prende informazioni Dalla piece table
Int complexoffset = littleendian. getint (header, 0x1a2 );
// Int complexoffset = littleendian. getint (header );

String tablename = NULL;
If (usetable1 ){
Tablename = "1 Table ";
} Else {
Tablename = "0 table ";
}

Documententry table = (documententry) fsys. getroot (). getentry (tablename );
Byte [] tablestream = new byte [Table. getsize ()];

Din = fsys. createdocumentinputstream (tablename );

Din. Read (tablestream );
Din. Close ();

Din = NULL;
Fsys = NULL;
Table = NULL;
Headerprops = NULL;

Int multiple = findtext (tablestream, complexoffset, text );

Stringbuffer sb = new stringbuffer ();
Int size = text. Size ();
Tablestream = NULL;

For (INT x = 0; x <size; X ++ ){

Wordtextpiece nextpiece = (wordtextpiece) text. Get (X );
Int start = nextpiece. getstart ();
Int length = nextpiece. getlength ();

Boolean Unicode = nextpiece. usesunicode ();
String tostr = NULL;
If (UNICODE ){
Tostr = new string (header, start, length * multiple, "UTF-16LE ");
} Else {
Tostr = new string (header, start, length, "ISO-8859-1 ");
}
SB. append (tostr). append ("");

}
Return sb. tostring ();
}

Private Static int findtext (byte [] tablestream, int complexoffset, arraylist text)
Throws ioexception {
// Actual text
Int Pos = complexoffset;
Int multiple = 2;
// Skips through the PRMS before we reach the piece table. These contain data
// For actual fast saved files
While (tablestream [POS] = 1 ){
Pos ++;
Int skip = littleendian. getshort (tablestream, POS );
Pos + = 2 + skip;
}
If (tablestream [POS]! = 2 ){
Throw new ioexception ("upted Word file ");
} Else {
// Parse out the text pieces
Int piecetablesize = littleendian. getint (tablestream, ++ POS );
Pos + = 4;
Int pieces = (piecetablesize-4)/12;
For (INT x = 0; x <pieces; X ++ ){
Int filepos =
Littleendian. getint (tablestream, POS + (pieces + 1) * 4) + (x * + 2 );
Boolean Unicode = false;
If (filepos & 0x40000000) = 0 ){
Unicode = true;
} Else {
Unicode = false;
Multiple = 1;
Filepos & = ~ (0x40000000); // gives me FC in Doc stream
Filepos/= 2;
}
Int totlength =
Littleendian. getint (tablestream, POS + (x + 1) * 4)
-Littleendian. getint (tablestream, POS + (x * 4 ));

Wordtextpiece piece = new wordtextpiece (filepos, totlength, Unicode );
Text. Add (piece );

}

}
Return multiple;
}
Public static void main (string [] ARGs ){
Wordtest W = new wordtest ();
Poifsfilesystem PS = new poifsfilesystem ();
Try {

File file = new file ("C: // test.doc ");

Inputstream in = new fileinputstream (File );
String S = W. extracttext (in );
System. Out. println (s );


} Catch (exception e ){
E. printstacktrace ();
}

}
Public Boolean writewordfile (string path, string content ){
Boolean W = false;
Try {

// Byte B [] = content. getbytes ("ISO-8859-1 ");
Byte B [] = content. getbytes ();

Bytearrayinputstream BAIS = new bytearrayinputstream (B );

Poifsfilesystem FS = new poifsfilesystem ();
Directoryentry directory = FS. getroot ();

Documententry de = directory. createdocument ("worddocument", BAIS );

Fileoutputstream ostream = new fileoutputstream (PATH );

FS. writefilesystem (ostream );

BAIS. Close ();
Ostream. Close ();

} Catch (ioexception e ){
E. printstacktrace ();
}

Return W;
}

}

Class wordtextpiece {
Private int _ fcstart;
Private Boolean _ usesunicode;
Private int _ length;

Public wordtextpiece (INT start, int length, Boolean Unicode ){
_ Usesunicode = Unicode;
_ Length = length;
Fc_start = start;
}
Public Boolean usesunicode (){
Return _ usesunicode;
}

Public int getstart (){
Return _ fcstart;
}
Public int getlength (){
Return _ length;
}

}

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.