Apache POI Operations Office Documents----Java Online preview txt, word, ppt, execel,pdf code

Source: Internet
Author: User
Tags gettext stringbuffer

Original address: Click on the Open link

Displays the contents of various documents on the page. Logic in the servlet

Word:

Bufferedinputstream bis = null;
URL url = null;
HttpURLConnection httpurl = null; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
bis = new Bufferedinputstream (Httpurl.getinputstream ());

String bodyText = null;
Wordextractor ex = new Wordextractor (bis);
BodyText = Ex.gettext ();
Response.getwriter (). write (BodyText);

Excel:

Bufferedinputstream bis = null;
URL url = null;
HttpURLConnection httpurl = null; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
bis = new Bufferedinputstream (Httpurl.getinputstream ());

Content = new StringBuffer ();
Hssfworkbook workbook = new Hssfworkbook (bis);
for (int numsheets = 0; numsheets < workbook.getnumberofsheets (); numsheets++) {
Hssfsheet Asheet = Workbook.getsheetat (numsheets);//Get a sheet
Content.append ("n");
if (null = = Asheet) {
Continue
}
for (int rownum = 0; rownum <= asheet.getlastrownum (); rownum++) {
Content.append ("n");
Hssfrow Arow = Asheet.getrow (rownum);
if (null = = Arow) {
Continue
}
for (short cellnum = 0; Cellnum <= arow.getlastcellnum (); cellnum++) {
Hssfcell Acell = Arow.getcell (cellnum);
if (null = = Acell) {
Continue
}
if (acell.getcelltype () = = hssfcell.cell_type_string) {
Content.append (Acell.getrichstringcellvalue ()
. getString ());
else if (acell.getcelltype () = = Hssfcell.cell_type_numeric) {
Boolean B = hssfdateutil.iscelldateformatted (Acell);
if (b) {
Date date = Acell.getdatecellvalue ();
SimpleDateFormat df = new SimpleDateFormat (
"Yyyy-mm-dd");
Content.append (Df.format (date));
}
}
}
}
}
Response.getwriter (). Write (content.tostring ());

ppt:

Bufferedinputstream bis = null;
URL url = null;
HttpURLConnection httpurl = null; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
bis = new Bufferedinputstream (Httpurl.getinputstream ());

StringBuffer content = new StringBuffer ("");
Slideshow SS = new Slideshow (new Hslfslideshow (bis));
slide[] Slides = ss.getslides ();
for (int i = 0; i < slides.length; i++) {
textrun[] t = slides[i].gettextruns ();
for (int j = 0; J < T.length; J + +) {
Content.append (T[j].gettext ());
}
Content.append (Slides[i].gettitle ());
}
Response.getwriter (). Write (content.tostring ());

PDF:

Bufferedinputstream bis = null;
URL url = null;
HttpURLConnection httpurl = null; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
bis = new Bufferedinputstream (Httpurl.getinputstream ());

PDDocument pdfdocument = null;
Pdfparser parser = new Pdfparser (bis);
Parser.parse ();
Pdfdocument = Parser.getpddocument ();
Bytearrayoutputstream out = new Bytearrayoutputstream ();
OutputStreamWriter writer = new OutputStreamWriter (out);
Pdftextstripper stripper = new Pdftextstripper ();
Stripper.writetext (Pdfdocument.getdocument (), writer);
Writer.close ();
byte[] contents = Out.tobytearray ();

String ts = new string (contents);
Response.getwriter (). write (TS);

txt:

BufferedReader bis = null;
URL url = null;
HttpURLConnection httpurl = null; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
bis = new BufferedReader (New InputStreamReader (Httpurl.getinputstream ()));

StringBuffer buf=new StringBuffer ();
String temp;
while (temp = Bis.readline ())!= null) {
Buf.append (temp);
Response.getwriter (). write (temp);
if (Buf.length () >=1000) {
Break
}
}
Bis.close ();

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.