All Downloads are FREE. Search and download functionalities are using the official Maven repository.

holmos.webtest.junitextentions.parameters.excel.HolmosExcelUtils Maven / Gradle / Ivy

There is a newer version: 1.0.2u10
Show newest version
package holmos.webtest.junitextentions.parameters.excel;

import holmos.webtest.exceptions.HolmosFailedError;

import java.util.ArrayList;

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;

/**
 * Excel工具类
 * 
 * @author 吴银龙([email protected])
 * */
public class HolmosExcelUtils {
	/**
	 *根据excel文件地址获取第一列的值,这个就是参数组的ID值,这是这个excel文件的第一个Sheet的第一列的值
	 * */
	public static ArrayListgetFistColumnVlauesByFilePath(String excelFilePath){
		HolmosWorkBook workBook=new HolmosWorkBook(excelFilePath);
		return getFirstColumnValuesByWorkBook(workBook);
	}
	/**
	 * 根据workbook对应的excel文件,获取此文件第一个sheet的第一个列的值
	 * */
	public static ArrayListgetFirstColumnValuesByWorkBook(HolmosWorkBook workBook){
		return getFirstColumnValuesByWorkBookAndIndex(workBook, 0);
	}
	/**
	 * 根据workbook对应的excel文件和sheet index,或者此sheet的第一列的值
	 * */
	public static ArrayList getFirstColumnValuesByWorkBookAndIndex(HolmosWorkBook workBook,int sheetIndex){
		ArrayListfirstColumnValues=new ArrayList();
		try{
			HolmosSheet sheet=workBook.getSheetByIndex(sheetIndex);
			firstColumnValues=getFirstColumnBySheet(sheet);
			}catch(Exception e){
				throw new HolmosFailedError("指定的列索引不存在");
			}
		return firstColumnValues;
	}
	/**
	 * 获取指定列的的第一行的值
	 * */
	public static ArrayListgetFirstColumnBySheet(HolmosSheet sheet){
		return getColumnValuesBySheetAndIndex(sheet, 0);
	}
	/**
	 * 获取指定sheet的指定索引的列的值
	 * */
	public static ArrayListgetColumnValuesBySheetAndIndex(HolmosSheet sheet,int columnIndex){
		ArrayListfirstColumnValues=new ArrayList();
		ArrayListrows=sheet.getValidRows();
		for(HolmosRow row:rows){
			firstColumnValues.add(row.getCellByIndex(columnIndex).getValueAsInt());
		}
		if(firstColumnValues.size()>0){
			firstColumnValues.remove(0);
		}
		return firstColumnValues;
	}
	public static void writeInfoToExcel(ArrayList>infos,String filePath){
		HolmosWorkBook holmosBook=new HolmosWorkBook(filePath);
		Workbook workbook=holmosBook.getBook();
		Sheet sheet=workbook.createSheet();
		for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy