Java的selenium代码随笔(1)

package ShareClass;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
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.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

/*
* 此类主要用于编写操作Excel的方法
*/
public class ExcelUtil {

private static Sheet excelSheet;
private static Workbook excelWorkBook;
private static Cell cell;
private static Row row;
private static FileInputStream excelFile;
private static String filePath;

public ExcelUtil(String filePath) throws NoSuchFileException {
filePath = this.filePath;
}

/**
* 判断是否存在“数据读取配置”
*
* @return
* @throws IOException
*/
public List<String[]> SetExcelReadConfig() throws IOException {
List<String[]> excelReadConfigList = new ArrayList<String[]>();
try {
// 实例化Excel文件的FileInputStream对象
excelFile = new FileInputStream(filePath);
// 实例化Excel文件的Workbook对象
String FileExtensionName = filePath.substring(filePath.indexOf("."));
// 判断文件类型如果是.xlsx,则使用XSSFWorkBook对象进行实例化
// 判断文件类型如果是.xls,则使用SSFWorkBook对象进行实力化
if (FileExtensionName.equals(".xlsx")) {
excelWorkBook = new XSSFWorkbook(excelFile);
} else if (FileExtensionName.equals(".xls")) {
excelWorkBook = new HSSFWorkbook(excelFile);
}
for (int i = 0; i < excelWorkBook.getNumberOfSheets(); i++) {
if (excelWorkBook.getSheetAt(i).equals("数据读取配置")) {
excelSheet = excelWorkBook.getSheet("数据读取配置");
int rowNum = excelSheet.getLastRowNum() - excelSheet.getFirstRowNum();
for (int j = 1; j < rowNum; j++) {
String[] configList = new String[6];
row = excelSheet.getRow(j);
for (int k = 0; k < 6; k++) {
if (row.getCell(k).equals(null)) {
row.getCell(k).setCellValue("0");
} else {
configList[k] = row.getCell(k).getStringCellValue();
}
}
excelReadConfigList.add(configList);
}
return excelReadConfigList;
} else {
continue;
}
}
return null;
} catch (NoSuchFileException e) {
// TODO: handle exception
throw (e);
}
}

/*
* 此方法主要用于读取Excel中的数据
*/
public static String[][] excelRead(String sheetName, int startRowNum, int endRowNum, int startCellNum,
int endCellNum) throws IOException {
// 实例化Excel文件的FileInputStream对象
excelFile = new FileInputStream(filePath);
// 实例化Excel文件的Workbook对象
String FileExtensionName = filePath.substring(filePath.indexOf("."));
// 判断文件类型如果是.xlsx,则使用XSSFWorkBook对象进行实例化
// 判断文件类型如果是.xls,则使用SSFWorkBook对象进行实力化
if (FileExtensionName.equals(".xlsx")) {
excelWorkBook = new XSSFWorkbook(excelFile);
} else if (FileExtensionName.equals(".xls")) {
excelWorkBook = new HSSFWorkbook(excelFile);
}
excelSheet = excelWorkBook.getSheet(sheetName);
// 获取Excel数据文件Sheet中的数据行号
// getLastRowNum方法获取数据的的最后行号
// getFirstRowNum方法获取数据的第一行行号
// 相减后算出数据的行号
// 注意:Excel文件的行号和列号都是从0开始的
int rowCount = endRowNum - startRowNum + 1;
// 获取Excel数据文件中的列号
int cellCount = endCellNum - startCellNum + 1;
// 定义各一个二维数组去接收这个数据
String[][] excelDate = new String[rowCount][cellCount];
// 循环遍历获取数据
for (int i = 0; i < rowCount; i++) {
row = excelSheet.getRow(startRowNum);
for (int j = 0; j < cellCount; j++) {
excelDate[i][j] = (String) (row.getCell(startCellNum).getCellTypeEnum() == CellType.STRING
? row.getCell(startCellNum).getStringCellValue()
: "" + row.getCell(startCellNum).getNumericCellValue());
}
startRowNum++;
}
return excelDate;
}

/*
* 此方法主要用于将测试结果写入到Excel中
*/
public static void excelWrite(int rowNum, int cellNum, String result) throws Exception {
try {
// 获取Excel文件中的行对象
int rowNumChange = rowNum + 2;
row = excelSheet.getRow(rowNumChange);
// 如果单元格为空
if (cell == null) {
// 单元格对象是Null的时候,创建单元格
// 如果单元格为空,无法直接调用单元格对象的setCellValue方法设定的单元格的值
cell.setCellValue(result);
} else {
// 单元格中有内容,则可以直接调用单元格对象的setCellValue方法设定单元格的值
cell.setCellValue(result);
}

// 实例化写入Excel文件的文件数出流对象
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
// 将内容写入Excel文件
excelWorkBook.write(fileOutputStream);
// 调用flush方法强制刷新写入文件
fileOutputStream.flush();
// 关闭文件输出流
fileOutputStream.close();
} catch (Exception e) {
// TODO: handle exception
throw (e);
}
}
}

posted @ 2017-10-17 09:28  煦色韶光  阅读(324)  评论(0编辑  收藏  举报