web开发总结----word的写入、读取

以下是Java对几种文本文件内容读取代码。其中,OFFICE文档(WORD,EXCEL)使用了POI控件,PDF使用了PDFBOX控件。

 点击这里查看相关控件的下载地址和配置方法。

  WORD

Java代码
  1. package textReader;   
  2. import java.io.*;   
  3. import org.apache.poi.hwpf.extractor.WordExtractor;   
  4.   
  5. public class WordReader {   
  6.     public WordReader(){   
  7.     }   
  8.     /**  
  9.      * @param filePath 文件路径  
  10.      * @return 读出的Word的内容  
  11.      */  
  12.     public String getTextFromWord(String filePath){   
  13.         String result = null;   
  14.         File file = new File(filePath);   
  15.         try{   
  16.             FileInputStream fis = new FileInputStream(file);   
  17.             WordExtractor wordExtractor = new WordExtractor(fis);   
  18.             result = wordExtractor.getText();   
  19.         }catch(FileNotFoundException e){   
  20.             e.printStackTrace();   
  21.         }catch(IOException e){   
  22.             e.printStackTrace();   
  23.         };   
  24.         return result;   
  25.     }   
  26. }    

  EXCEL

Java代码
  1. package textReader;   
  2. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  3. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  4. import org.apache.poi.hssf.usermodel.HSSFRow;   
  5. import org.apache.poi.hssf.usermodel.HSSFCell;   
  6.   
  7. import java.io.FileInputStream;   
  8. import java.io.FileNotFoundException;   
  9. import java.io.IOException;   
  10.   
  11.   
  12. public class ExcelReader {   
  13.   
  14.     @SuppressWarnings("deprecation")   
  15.     /**  
  16.      * @param filePath 文件路径  
  17.      * @return 读出的Excel的内容  
  18.      */  
  19.     public String getTextFromExcel(String filePath) {   
  20.         StringBuffer buff = new StringBuffer();   
  21.         try {   
  22.             //创建对Excel工作簿文件的引用   
  23.             HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));   
  24.             //创建对工作表的引用。           
  25.             for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) {   
  26.                 if (null != wb.getSheetAt(numSheets)) {   
  27.                     HSSFSheet aSheet = wb.getSheetAt(numSheets);//获得一个sheet   
  28.                     for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {   
  29.                         if (null != aSheet.getRow(rowNumOfSheet)) {   
  30.                             HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一个行   
  31.                             for (int cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {   
  32.                                 if (null != aRow.getCell(cellNumOfRow)) {   
  33.                                     HSSFCell aCell = aRow.getCell(cellNumOfRow);//获得列值   
  34.                                     switch(aCell.getCellType()){   
  35.                                         case HSSFCell.CELL_TYPE_FORMULA:   
  36.                                             break;    
  37.                                         case HSSFCell.CELL_TYPE_NUMERIC:   
  38.                                             buff.append(aCell.getNumericCellValue()).append('\t');break;   
  39.                                         case HSSFCell.CELL_TYPE_STRING:   
  40.                                             buff.append(aCell.getStringCellValue()).append('\t');break;                                                                
  41.                                 }                              
  42.                         }                                                                                   
  43.                     }   
  44.                     buff.append('\n');   
  45.                     }   
  46.                 }                                         
  47.             }              
  48.         }   
  49.         } catch (FileNotFoundException e) {   
  50.             e.printStackTrace();   
  51.         } catch (IOException e) {   
  52.             e.printStackTrace();   
  53.         }   
  54.         return buff.toString();   
  55.     }               
  56. }   
 

  PDF  

Java代码
  1. package textReader;   
  2. import java.io.FileInputStream;   
  3. import java.io.FileNotFoundException;   
  4. import java.io.IOException;   
  5.   
  6. import org.pdfbox.pdfparser.PDFParser;   
  7. import org.pdfbox.pdmodel.PDDocument;   
  8. import org.pdfbox.util.PDFTextStripper;   
  9.   
  10.   
  11. public class PdfReader {   
  12.     public PdfReader(){   
  13.     }   
  14.     /**  
  15.      * @param filePath 文件路径  
  16.      * @return 读出的pdf的内容  
  17.      */  
  18.     public String getTextFromPdf(String filePath) {   
  19.         String result = null;   
  20.         FileInputStream is = null;   
  21.         PDDocument document = null;   
  22.         try {   
  23.             is = new FileInputStream(filePath);   
  24.             PDFParser parser = new PDFParser(is);   
  25.             parser.parse();   
  26.             document = parser.getPDDocument();   
  27.             PDFTextStripper stripper = new PDFTextStripper();   
  28.             result = stripper.getText(document);   
  29.         } catch (FileNotFoundException e) {   
  30.             e.printStackTrace();   
  31.         } catch (IOException e) {   
  32.             e.printStackTrace();   
  33.         } finally {   
  34.             if (is != null) {   
  35.                 try {is.close();}catch(IOException e){e.printStackTrace();}   
  36.             }   
  37.             if (document != null) {   
  38.                 try{document.close();}catch (IOException e){e.printStackTrace();}   
  39.             }   
  40.         }   
  41.         return result;   
  42.     }   
  43.   
  44. }  

  TXT

Java代码
  1. package textReader;   
  2. import java.io.*;   
  3.   
  4.   
  5. public class TxtReader {   
  6.     public TxtReader() {           
  7.     }   
  8.     /**  
  9.      * @param filePath 文件路径  
  10.      * @return 读出的txt的内容  
  11.      */  
  12.     public String getTextFromTxt(String filePath) throws Exception {   
  13.            
  14.         FileReader fr = new FileReader(filePath);   
  15.         BufferedReader br = new BufferedReader(fr);   
  16.         StringBuffer buff = new StringBuffer();   
  17.         String temp = null;   
  18.         while((temp = br.readLine()) != null){   
  19.             buff.append(temp + "\r\n");   
  20.         }   
  21.         br.close();        
  22.         return buff.toString();        
  23.     }   
  24. }  

RTF

Java代码
  1. package textReader;   
  2. import java.io.File;   
  3. import java.io.FileInputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6.   
  7. import javax.swing.text.BadLocationException;   
  8. import javax.swing.text.DefaultStyledDocument;   
  9. import javax.swing.text.rtf.RTFEditorKit;   
  10.   
  11.   
  12. public class RtfReader {   
  13.     public RtfReader(){   
  14.     }   
  15.     /**  
  16.      * @param filePath 文件路径  
  17.      * @return 读出的rtf的内容  
  18.      */  
  19.     public String getTextFromRtf(String filePath) {   
  20.         String result = null;   
  21.         File file = new File(filePath);   
  22.         try {          
  23.             DefaultStyledDocument styledDoc = new DefaultStyledDocument();   
  24.             InputStream is = new FileInputStream(file);   
  25.             new RTFEditorKit().read(is, styledDoc, 0);   
  26.             result = new String(styledDoc.getText(0,styledDoc.getLength()).getBytes("ISO8859_1"));   
  27.             //提取文本,读取中文需要使用ISO8859_1编码,否则会出现乱码   
  28.         } catch (IOException e) {   
  29.             e.printStackTrace();   
  30.         } catch (BadLocationException e) {   
  31.             e.printStackTrace();   
  32.         }   
  33.         return result;   
  34.     }   
  35.        
  36. }  

   HTML

Java代码
  1. package textReader;   
  2. import java.io.*;   
  3.   
  4. public class HtmlReader {   
  5.     public HtmlReader() {   
  6.     }   
  7.     /**  
  8.      * @param filePath 文件路径  
  9.      * @return 获得html的全部内容  
  10.      */  
  11.     public String readHtml(String filePath) {   
  12.         BufferedReader br=null;   
  13.         StringBuffer sb = new  StringBuffer();   
  14.         try {   
  15.             br=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),  "GB2312"));            
  16.             String temp=null;          
  17.             while((temp=br.readLine())!=null){   
  18.                 sb.append(temp);   
  19.             }              
  20.         } catch (FileNotFoundException e) {   
  21.             e.printStackTrace();   
  22.         } catch (IOException e) {   
  23.             e.printStackTrace();   
  24.         }   
  25.         return sb.toString();   
  26.     }   
  27.     /**  
  28.      * @param filePath 文件路径  
  29.      * @return 获得的html文本内容  
  30.      */  
  31.     public String getTextFromHtml(String filePath) {   
  32.         //得到body标签中的内容   
  33.         String str= readHtml(filePath);   
  34.         StringBuffer buff = new StringBuffer();   
  35.         int maxindex = str.length() - 1;   
  36.         int begin = 0;   
  37.         int end;               
  38.         //截取>和<之间的内容   
  39.         while((begin = str.indexOf('>',begin)) < maxindex){              
  40.             end = str.indexOf('<',begin);   
  41.             if(end - begin > 1){   
  42.                 buff.append(str.substring(++begin, end));                  
  43.             }              
  44.             begin = end+1;   
  45.         };         
  46.         return buff.toString();   
  47.     }   
  48.   
  49. }  

猜你喜欢

转载自2277259257.iteye.com/blog/2161761