zoukankan      html  css  js  c++  java
  • 使用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 + "	");  
            	    }  
            	    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);
        }
    }
    

    结果:

  • 相关阅读:
    c++ 迷宫问题
    linux下恢复删除的文件
    c++ 分解数2
    c++ 平分石头
    多态
    设计模式中类的6种关系
    工厂方法模式
    设计原则之面向接口编程
    封装、继承
    便利构造器、单件模式
  • 原文地址:https://www.cnblogs.com/HCT118/p/5815809.html
Copyright © 2011-2022 走看看