Java + Selenium-- use POI to read and write excel file (due to environmental Cipian reference)

Reference website: https://blog.csdn.net/u011541946/article/details/74726045

 

This introduction a third-party tool POI, due to the cross-platform Java, so Java does not directly support the operation of the library excel file.

In automated testing, some of our test data into excel file, you need to understand how to operate within the content acquisition excel.

1. Download the POI

Open website http://poi.apache.org/download.html, select the following download version 3.16.

 

 

 

Select ZIP package download.

 

 

 

Once downloaded unzip the file structure is as follows

 

 

 

2. Add the POI file in your library Selenium current project

First create a new folder, extract out all the xx.jar are placed in a new folder,

Right then your eclipse project> Build path> Configure build path> Click lib> All xxx.jar Add external jar, create a new folder are selected.

Or, you only need the following jar files on it.

 

 

 

 

 

Note that these jar files are not in the same path, you need to go a different path copy copy to a new folder, then copy all the jar files in this folder to eclipse Files files in your project folder,

 

You need the right project, create a new folder called Files. My project, take this approach, I do not want to copy all the jar to the project lib.

 

 

3. Prepare a test-data.xlsx file

Desktop to create a new test-data.xlsx, and then enter the following, and put the file into eclipse project.

 

 

 

 

4 .. ReadExcel.java a new file, enter the following code.

 

package lessons;
 
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
public class ReadExcel {
 
    public static void main(String[] args) {
        
        try{
            
            // 指定excel的路径
            File src = new File(".\\Files\\test-data.xlsx");
            
            // 加载文件
            FileInputStream fis = new FileInputStream(src);
            
            // 加载workbook
            @SuppressWarnings("resource")
            XSSFWorkbook wb=new XSSFWorkbook(fis);
            
            //加载sheet,这里我们只有一个sheet,默认是sheet1
            XSSFSheet sh1= wb.getSheetAt(0);
            
            // 开始读取第一行第一列的数据
            System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());
            // 读取第一行第二列内容
            System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());
            // 读取第二行第二列内容
            System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());
            // 读取第二行第二列内容
            System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());
            
            // 获取实际总行数
            System.out.println(sh1.getPhysicalNumberOfRows());
            
            // 获取实际总列数
            System.out.println(sh1.getPhysicalNumberOfRows());
            
        } catch (Exception e){
            
            System.out.println(e.getMessage());
            
        }
 
    }
 
}

 

 

 

 

我尝试找了很久,发现有获取excel内sheet总行数的方法,我说的行是值水平上一行数据,叫行。但是没有获取总列数的方法。POI确实没有获取总列数方法。上面的测试文件一共有4行2列数据。
6.换成循环打印excel内数据

由于我们知道获取总行数的方法,至于列数,我们只能事先确定,例如上面事先确定是2列,但是不确定有多少行。这个只是循环遍历当前sheet的内所有有数据的单元格。

package lessons;
 
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
public class ReadExcel {
 
    public static void main(String[] args) {
        
        try{
            
            // 指定excel的路径
            File src = new File(".\\Files\\test-data.xlsx");
            
            // 加载文件
            FileInputStream fis = new FileInputStream(src);
            
            // 加载workbook
            @SuppressWarnings("resource")
            XSSFWorkbook wb=new XSSFWorkbook(fis);
            
            //加载sheet,这里我们只有一个sheet,默认是sheet1
            XSSFSheet sh1= wb.getSheetAt(0);
            
            // i 控制行
            for(int i=0;i<sh1.getPhysicalNumberOfRows();i++){
                // j是控制列,我们上面数据是2列
                for(int j=0;j<2;j++){
                    
                    System.out.println(sh1.getRow(i).getCell(j).getStringCellValue());
                }
            }
        
        } catch (Exception e){
            
            System.out.println(e.getMessage());
            
        }
 
    }
 
}

 

 

 

 

 

7. 写入内容到excel

例如,我们需要在上面test-data.xlsx文件第三列标上Fail或者Pass,请看下面脚本如何实现。

 

 

package lessons;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
public class ReadExcel {
 
    public static void main(String[] args) {
        
        try{
            
            // 指定excel的路径
            File src = new File(".\\Files\\test-data.xlsx");
            
            // 加载文件
            FileInputStream fis = new FileInputStream(src);
            
            // 加载workbook
            @SuppressWarnings("resource")
            XSSFWorkbook wb=new XSSFWorkbook(fis);
            
            //加载sheet,这里我们只有一个sheet,默认是sheet1
            XSSFSheet sh1= wb.getSheetAt(0);
            
            //写入excel数据
            sh1.getRow(0).createCell(2).setCellValue("Pass");
            sh1.getRow(1).createCell(2).setCellValue("Fail");
            sh1.getRow(2).createCell(2).setCellValue("N/A");
            sh1.getRow(3).createCell(2).setCellValue("Pass");
        
            // 保存文件
            FileOutputStream fout=new FileOutputStream(new File(".\\Files\\test-data.xlsx"));
             
            //覆盖写入内容 
            wb.write(fout);
            
            // 关闭文件
            fout.close();
            
        } catch (Exception e){
            
            System.out.println(e.getMessage());
            
        }
 
    }
 
}

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12221696.html