java parse excel

來源:互聯網
上載者:User

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
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;

/**
* EXCEL文檔解析工具類
* 該工具能將EXCEL文檔中的表解析為由JAVA基礎類構成的資料集合
* 整個EXCEL表由多個行組成.每行用一個LIST表示.
* EXCEL中的行由一個LIST表示,各列的資料索引從0開始一一對齊存放在這個LIST中;
* 多個行構成整個表,由一個LIST存放多個行.
*
*******************************************
* com.trumptech.common.fileParser.excel
* 2007-6-15
* 16:20:38
* author linfan
*******************************************
*/
public class ExcelSheetParser {

private Logger logger= Logger.getLogger(ExcelSheetParser.class);

private HSSFWorkbook workbook ;

public ExcelSheetParser(File excelFile) throws FileNotFoundException, IOException{

   workbook = new HSSFWorkbook(new FileInputStream(excelFile));
}

/**
* 獲得表中的資料
* @param sheetNumber 表格索引(EXCEL 是多表文檔,所以需要輸入表索引號)
* @return 由LIST構成的行和表
* @throws FileNotFoundException
* @throws IOException
*/
public List<List> getDatasInSheet(int sheetNumber) throws FileNotFoundException, IOException{
   List<List> result = new ArrayList<List>();
  
   //獲得指定的表
   HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
  
   //獲得資料總行數
   int rowCount = sheet.getLastRowNum();
   logger.info("found excel rows count: " + rowCount);
   if (rowCount < 1) {
    return result;
   }
  
   //逐行讀取資料
   for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
   
    //獲得行對象
    HSSFRow row = sheet.getRow(rowIndex);
   
    if (row != null) {
    
     List<Object> rowData = new ArrayList<Object>();
    
     //獲得本行中儲存格的個數
     int columnCount = row.getLastCellNum();
    
     //獲得本行中各儲存格中的資料
     for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
      HSSFCell cell = row.getCell(columnIndex);
     
      //獲得指定儲存格中資料
      Object cellStr = this.getCellString(cell);
     
      rowData.add(cellStr);
     
     }
    
     result.add(rowData);
    }
   }
   return result;
}

/**
* 獲得儲存格中的內容
* @param cell
* @return
*/
protected Object getCellString(HSSFCell cell){
   Object result = null;
   if (cell != null) {

    int cellType = cell.getCellType();
   
    switch(cellType){
   
    case HSSFCell.CELL_TYPE_STRING :
     result = cell.getRichStringCellValue().getString();
     break;
    case HSSFCell.CELL_TYPE_NUMERIC:
     result=cell.getNumericCellValue();
     break;
    case HSSFCell.CELL_TYPE_FORMULA:
     result = cell.getNumericCellValue();
     break;
    case HSSFCell.CELL_TYPE_ERROR:
     result=null;
     break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
     result=cell.getBooleanCellValue();
     break;
    case HSSFCell.CELL_TYPE_BLANK:
     result=null;
     break;
    }
   }
   return result;
}

public static void main(String[] args) throws Exception {
   File file = new File("D://tmp//test.xls");
   ExcelSheetParser parser = new ExcelSheetParser(file);
   List<List> datas = parser.getDatasInSheet(0);

   for(int i=0;i<datas.size();i++){//顯示資料
    List row = datas.get(i);
    for(short n=0;n<row.size() ;n++){
     Object value = row.get(n);
     String data = String.valueOf(value);
     System.out.print(data +"/t");
    }
    System.out.println();
   }
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.