1. 程式人生 > >Java使用poi讀取Excel

Java使用poi讀取Excel

在做之前新說明一下遇到的坑吧。(這裡給H1標題)

  1. 從excel檔案裡顯示的資料為 "1" 結果通過 row.getCell(0) 讀出來變成了 1.0 在轉換成int型別居然需要 Double.valueOf(row.getCell(0).toString().trim())),真夠麻煩的。。。跟C#不太一樣。
  2. Excel檔案裡面空白行達到1000000W條+ 我不知道這個Excel經歷了什麼,結果讀到空資料轉換又報錯,之後使用IsRowEmpty()這個方法判斷遇到空白行 break 了,前提是知道Excel結構後面全是空白行的話可以break不然需要改成continue。

下面就是記錄一下。

  • 第一步:使用Maven建立專案
  • 第二步:在pom裡新增poi的依賴
  • 第三步:程式碼如下
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class App 
{
    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";
    public static void main( String[] args ) throws IOException
    {
    	System.out.println("開始執行");
    	List<Testcont> testconts= GetAllByExcel("C:\\Users\\Vito\\Desktop\\資料測試test.xlsx");
    	System.out.println("讀取結束");
    	for(Testcont testcont : testconts)
    	{
    		System.out.println(testcont.toString());
    	}
		System.out.println("列印結束");
    }
     
    public static Workbook GetWorkbok(File file) throws IOException{
        Workbook wb = null;
        FileInputStream in = new FileInputStream(file);
        if(file.getName().endsWith(EXCEL_XLS)){           //Excel&nbsp;2003
            wb = new HSSFWorkbook(in);
        }else if(file.getName().endsWith(EXCEL_XLSX)){    // Excel 2007/2010
            wb = new XSSFWorkbook(in);
        }
        return wb;
    }
    
    public static List<Testcont> GetAllByExcel(String file) throws IOException{
    	Workbook wb =null;
        Sheet sheet = null;
        Row row = null;
        List<Testcont> list = null ;
        wb = GetWorkbok(new File(file));
        if(wb != null){
            //用來存放表中資料
            list = new ArrayList<Testcont>();
            //獲取第一個sheet
            sheet = wb.getSheetAt(0);
            //獲取最大行數
            int rownum = sheet.getPhysicalNumberOfRows();
            //獲取第一頁sheet
            row = sheet.getRow(0);
            for (int i = 1; i<rownum; i++) {
                row = sheet.getRow(i);
                if(row !=null){
                	if(IsRowEmpty(row)) 
                	{
                		System.out.println("空白行");
                		break;
                	}
					//Testcont是自己建立的物件裡面有 id 和 name 兩個欄位用來儲存讀到的資料
                    list.add(new Testcont((int)Math.ceil(Double.valueOf(row.getCell(0).toString().trim())),row.getCell(1).toString()));
                }               	                           
            }
        }     
      return list;
    }
	
    public static boolean IsRowEmpty(Row row) {
    	for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
            Cell cell = row.getCell(c);
            if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK){
                return false;
            }
        }
        return true;
 	} 
}