POI提取word,读写excel

poi提取word:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hwpf.extractor.WordExtractor;

public class TikaTest {

public static void main(String[] args) {
   File file = new File("E:\\expo_qa.doc");
   try {
    FileInputStream fis = new FileInputStream(file);
    WordExtractor wordExtractor = new WordExtractor(fis);
    System.out.println("使用getText()方法提取的Word文件的内容:");
    System.out.println("==============================");
    System.out.println(wordExtractor.getText());
    //System.out.println(wordExtractor.getFileSystem());
    
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
   e.printStackTrace();
}
}
} 


poi读写excel:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class TikaExcel {

	/**
	 * @param args
	 * @throws Exception 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, Exception {
		// TODO Auto-generated method stub
		
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("E:/develop/staff.xls")); 
		HSSFWorkbook wb = new HSSFWorkbook(fs);
	
		 
         HSSFSheet sheet = wb.getSheetAt(0); 
         HSSFRow row = sheet.getRow(0); 
         HSSFCell cell = row.getCell((short)0); 
        String msg = cell.getStringCellValue(); 
       
        System.out.println(msg);
        HSSFRow row2 = sheet.getRow(1);
        System.out.println(row2.getCell(0)+" "+row2.getCell(1)+" "+row2.getCell(2)+" "+row2.getCell(3));
        HSSFRow row3 = sheet.getRow(11);
        System.out.println(row3.getCell(0)+" "+row3.getCell(1)+" "+row3.getCell(2)+" "+row3.getCell(3));
        
       // POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream("workbook.xls")); 

        //HSSFWorkbook wb = new HSSFWorkbook(fs); 

       // HSSFSheet sheet = wb.getSheetAt(0); 

        HSSFRow rowX = sheet.getRow(11); 

        HSSFCell cellX = rowX.getCell((short)0); 

        cellX.setCellValue("李宁(James)"); 

        // Write the output to a file 

        FileOutputStream fileOut = new FileOutputStream("E:/develop/staff.xls"); 

        wb.write(fileOut); 

        fileOut.close(); 

	}

}

你可能感兴趣的:(java,apache,Excel)