使用poi讀取xlsx中的數據


excel中的內容見下圖:

詳細代碼:

package dataprovider;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelRead 
{
    public void getValues(String filePath ) 
    {
        String values = null;
        try{
        	InputStream is = new FileInputStream(filePath);
        	// 構造 XSSFWorkbook 對象,strPath 傳入文件路徑  
        	XSSFWorkbook xwb = new XSSFWorkbook(is);  
        	// 讀取第一章表格內容  
        	XSSFSheet sheet = xwb.getSheetAt(0);  
        	// 定義 row、cell  
        	XSSFRow row;  
        	String cell;  
        	// 循環輸出表格中的內容  
        	for (int i = sheet.getFirstRowNum()+1; 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("");  
        	} 
            }catch(Exception e) {
                System.out.println("已運行xlRead() : " + e );
            }
    }
    public static void main(String args[]) 
    {
        String filePath="D:\\eclipse workspace\\TestNg\\tt_test.xlsx";
        ExcelRead er = new ExcelRead();
        er.getValues(filePath);
    }
}

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM