JAVA POI的初步使用

linjiaqingggg發表於2017-01-19

###下載地址
https://archive.apache.org/dist/poi/release/bin/
###需要的jar包(我用的是3.10final)

  • Poi-3.10-Final.jar (用於xls)
  • Poi-ooxml-3.10-Final.jar (用於xlsx)
  • Poi-ooxml-schemas-3.10.jar
  • Xmlbeans-2.30.jar
  • dom4j-1.6.1.jar
  • poi-scratchpad-3.10-FINAL-20140208.jar(用於word,ppt)
    POI的各個包的作用
    ###問題
  • java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;) in Apache POI 這一類類似問題有很多
  • read2bytes, rest 512byte也是這類問題,因為你用了wordextracer,不是原生的,在這裡推薦使用原生的POI,不要去用那些亂七八糟裁剪過的
    因為你的jar包是從不同的地方下的,所以有可能各個版本引用之間存在問題,我就是之前用了tm-extractors-0.4.jar,其實這個東西是poi-scratchpad裡面的一部分,只不過有人重新打包了就釋出出來,結果有一部分程式碼自動用了tm-extractors裡面的而沒有用POI裡面的,造成了版本不一致。包括jxl也是POI的一部份

讀doc

public static String readWord(String name)
	{
		FileInputStream in;
		String text = null;
		try 
		{
			in = new FileInputStream(name);
			WordExtractor extractor = new WordExtractor(in);
			text = extractor.getText();
		} 
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return text;
	}

讀docx

public static String readDocx(String path)
	{
	//都是隻能用String,不能用Stringbuffer,還不知道原因
		String text = null; 
		try {
			InputStream is = new FileInputStream("doc/aaa.docx");
			XWPFDocument doc = new XWPFDocument(is);  
		    XWPFWordExtractor extractor = new XWPFWordExtractor(doc);  
		    text = extractor.getText(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return text;
	}

###讀xls和xlsx

private static Logger log = Logger.getLogger("client"); 
	public static String readXls(String path)
	{
		String text="";  
	     try 
	     {  
	          FileInputStream is =  new FileInputStream(path);
	          HSSFWorkbook excel=new HSSFWorkbook(is);  
             //獲取第一個sheet  
             HSSFSheet sheet0=excel.getSheetAt(0);  
             for (Iterator rowIterator=sheet0.iterator();rowIterator.hasNext();) 
             {  
                 HSSFRow row=(HSSFRow) rowIterator.next();  
                 for (Iterator iterator=row.cellIterator();iterator.hasNext();) 
                 {  
                     HSSFCell cell=(HSSFCell) iterator.next();  
                     //根據單元的的型別 讀取相應的結果  
                     if(cell.getCellType()==HSSFCell.CELL_TYPE_STRING) text+=cell.getStringCellValue()+"\t";  
                     else if(cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC) text+=cell.getNumericCellValue()+"\t";  
                     else if(cell.getCellType()==HSSFCell.CELL_TYPE_FORMULA) text+=cell.getCellFormula()+"\t";  
                 }  
               text+="\n";  
             }          
	     } 
	     catch (Exception e) 
	     {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	            log.warn(e);
	     }  
 	     
	     return text; 
	}
	public static String readXlsx(String path)
	{
	     String text="";  
	     try 
	     {  
	          OPCPackage pkg=OPCPackage.open(path);  
	          XSSFWorkbook excel=new XSSFWorkbook(pkg);  
              //獲取第一個sheet  
              XSSFSheet sheet0=excel.getSheetAt(0);  
              for (Iterator rowIterator=sheet0.iterator();rowIterator.hasNext();) 
              {  
                  XSSFRow row=(XSSFRow) rowIterator.next();  
                  for (Iterator iterator=row.cellIterator();iterator.hasNext();) 
                  {  
                      XSSFCell cell=(XSSFCell) iterator.next();  
                      //根據單元的的型別 讀取相應的結果  
                      if(cell.getCellType()==XSSFCell.CELL_TYPE_STRING) text+=cell.getStringCellValue()+"\t";  
                      else if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC) text+=cell.getNumericCellValue()+"\t";  
                      else if(cell.getCellType()==XSSFCell.CELL_TYPE_FORMULA) text+=cell.getCellFormula()+"\t";  
                  }  
                text+="\n";  
              }          
	     } 
	     catch (Exception e) 
	     {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	            log.warn(e);
	     }  
  	     
	     return text;  
	}

相關文章