Java reads PDF, Word, Excel files

Source: Internet
Author: User

Jar used:

Itextpdf-5.5.8.jar (PDF)

Poi.jar

public class FileUtils {/** * Determines if the file exists * * @Title: Isexcite * @param @param filePath * @param @return * @return Boolean return Back type * @throws */public static Boolean isexcite (String filePath) {File File = new file (filePath);//If the folder does not exist, create if (!file.ex Ists () &&!file.isdirectory ()) {return false;} else {return true;}} /** * * @Title: Getpdffiletext * @Description: Gets the file contents of the PDF in the specified location * @param @param fileName * @param @return * @param @throws IOException * @return String return type * @throws */public static string Getpdffiletext (String fileName) throws IOException {PDF Reader reader = new Pdfreader (fileName); Pdfreadercontentparser parser = new Pdfreadercontentparser (reader); StringBuffer buff = new StringBuffer (); Textextractionstrategy strategy;for (int i = 1; I <= reader.getnumberofpages (); i++) {strategy = Parser.processcontent ( I,new simpletextextractionstrategy ()); Buff.append (Strategy.getresultanttext ());} return buff.tostring ();} /** * Get Doc Documents * * @Title: Gettextfromword * @param @param FilepaTH * @param @return * @return string return type * @throws */public static string Gettextfromword (String filePath) {string result = NULL; File File = new file (FilePath); try {fileinputstream fis = new FileInputStream (file); Wordextractor wordextractor = new Wordextractor (FIS); result = Wordextractor.gettext ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return result;} /** * Read Excel content * * @Title: Gettextfromexcel * @param @param filePath * @param @return * @return String return type * @throws * * public static string Gettextfromexcel (String filePath) {StringBuffer buff = new StringBuffer (); try {//Create reference to Excel workbook file hs Sfworkbook wb = new Hssfworkbook (new FileInputStream (FilePath));//Create a reference to the worksheet. for (int numsheets = 0; numsheets < wb.getnumberofsheets (); numsheets++) {if (Null! = Wb.getsheetat (numsheets)) {Hssfsh  Asheet = Wb.getsheetat (numsheets);//Get a sheetfor (int rownumofsheet = 0; Rownumofsheet <= asheet.getlastrownum (); rownumofsheet++) {if (Null! = Asheet.getrow (Rownumofsheet))  {Hssfrow Arow = Asheet.getrow (Rownumofsheet);//Get a line for (int cellnumofrow = 0; Cellnumofrow <= arow.getlastcellnum (); cellnumofrow++) {if (Null! = Arow.getcell (Cellnumofrow)) {Hssfcell Acell = Arow.getcell (Cellnumofrow);//Get column value switch (aCe Ll.getcelltype ()) {Case Hssfcell.cell_type_formula:break;case HSSFCell.CELL_TYPE_NUMERIC:buff.append ( Acell.getnumericcellvalue ()). Append (' \ t '); Break;case HSSFCell.CELL_TYPE_STRING:buff.append ( Acell.getstringcellvalue ()). Append (' \ t '); break;}}} Buff.append (' \ n ');}}}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return buff.tostring ();}   /** * Replace the contents of the file * @Title: Replacecontenttofile * @param @param path file paths * @param @param str to replace * @param @param con Replace the contents of the name * @return void return type * @throws */public static void Replacecontenttofile (string path, String str, string con) {try { if (isexcite (path)) {FileReader read = new FileReader (path); BufferedReader br = newBufferedReader (read); StringBuilder content = new StringBuilder (); while (Br.ready ()! = False) {Content.append (Br.readline ()); Content.append ( "\ r \ n");} int dex = Content.indexof (str), if (dex! =-1) {System.out.println ("Find tag!");} else {System.out.println ("The specified tag does not exist!");} Content.replace (Dex, Dex, con); Br.close (); Read.close (); FileOutputStream fs = new FileOutputStream (path), Fs.write (Content.tostring (). GetBytes ()); Fs.close (); else {System.out.println ("file does not exist!");}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}

Save it and take it directly.

Java reads PDF, Word, Excel files

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.