타사 – excel 2003, 2007 읽기

4762 단어 Excel
package ;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.POITextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
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.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.XmlException;
public class Test {
	/**
	 *  office 2003 xls
	 * @param filePath
	 */
 @SuppressWarnings({ "unchecked", "deprecation" })
public void loadXls(String filePath){
	  try {
		   InputStream input = new FileInputStream("F:\\eclipsework\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\mapbar-fieldwork\\jsp\\customervisit\\excelupload\\2.xls");
		   POIFSFileSystem fs = new POIFSFileSystem(input);
		   HSSFWorkbook wb = new HSSFWorkbook(fs);
		   HSSFSheet sheet = wb.getSheetAt(0);
		   // Iterate over each row in the sheet
		   Iterator rows = sheet.rowIterator();
		   while (rows.hasNext()) {
		    HSSFRow row = (HSSFRow) rows.next();
		    System.out.println("Row #" + row.getRowNum());
		    // Iterate over each cell in the row and print out the cell"s
		    // content
		    Iterator cells = row.cellIterator();
		    while (cells.hasNext()) {
		     HSSFCell cell = (HSSFCell) cells.next();
		     System.out.println("Cell #" + cell.getCellNum());
		     switch (cell.getCellType()) {
		     case HSSFCell.CELL_TYPE_NUMERIC:
		      System.out.println(cell.getNumericCellValue());
		      break;
		     case HSSFCell.CELL_TYPE_STRING:
		      System.out.println(cell.getStringCellValue());
		      break;
		     case HSSFCell.CELL_TYPE_BOOLEAN:
		      System.out.println(cell.getBooleanCellValue());
		      break;
		     case HSSFCell.CELL_TYPE_FORMULA:
		      System.out.println(cell.getCellFormula());
		      break;
		     default:
		      System.out.println("unsuported sell type");
		      break;
		     }
		    }
		   }
		  } catch (IOException ex) {
		   ex.printStackTrace();
		  }
 }
 /**
  *  xlsx 
  * @param filePath
  */
 public void loadXlsxText(String filePath){
	 File inputFile = new File("D://test.xlsx");   
	 try {
		POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);
		System.out.println(extractor.getText());
	} catch (InvalidFormatException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} catch (OpenXML4JException e) {
		e.printStackTrace();
	} catch (XmlException e) {
		e.printStackTrace();
	}   
 }
 /**
  *  office 2007 xlsx
  * @param filePath
  */
 public void loadXlsx(String filePath){
	 //   XSSFWorkbook  ,strPath     
	XSSFWorkbook xwb = null;
	try {
		xwb = new XSSFWorkbook("F:\\eclipsework\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\mapbar-fieldwork\\jsp\\customervisit\\excelupload\\2.xls");
	} catch (IOException e) {
		System.out.println(" ");
		e.printStackTrace();
	}   
	 //     
	 XSSFSheet sheet = xwb.getSheetAt(0);   
	 xwb.getSheetAt(1);
	//   row、cell   
	 XSSFRow row;   
	 String cell;   
	 //     
	 for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {   
	     row = sheet.getRow(i);   
	     
	     for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {   
	        //   row.getCell(j).toString()  ,   
	        cell = row.getCell(j).toString();  
	        System.out.print(cell + "/t");   
	    }   
	     System.out.println("");   
	 }   
 }
 public static void main(String[] args) {
	 Test readExcel =new Test();
	 //readExcel.loadXlsx("");
	 readExcel.loadXls("");
 }
}

좋은 웹페이지 즐겨찾기