[Groovy] 创建Excel,追加Excel
package ScriptLibrary import java.awt.Color
import java.awt.GraphicsConfiguration.DefaultBufferCapabilities;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle
import org.apache.poi.xssf.usermodel.XSSFColor
import org.apache.poi.xssf.usermodel.XSSFDataFormat
import org.apache.poi.xssf.usermodel.XSSFFont
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem class WriteExcel { def testRunner
def context
def log WriteExcel(testRunner,context,log) {
this.testRunner = testRunner
this.context = context
this.log = log
} def parseFailMessage(String failMessage,def extraInfoMap){
//获取Test Suite和Test Case的名字
def currentStepIndex = context.currentStepIndex
def testCaseName = testRunner.testCase.getTestStepAt(currentStepIndex).getParent().getName()
def testSuiteName = testRunner.testCase.getTestStepAt(currentStepIndex).getParent().getParent().getName() //解析extraInfoMap
def valueList = extraInfoMap.values() //根据;分隔每一条错误信息
ArrayList failMessageList = new ArrayList()
String[] failMessageArray = failMessage.split(";") //遍历每一条错误信息,解析出每一列的值,便于写入Excel
for(int i=0;i<failMessageArray.size();i++){
//获取第i条错误信息
String oneFailMessage = failMessageArray[i] //从Expected:处进行分隔
int expectedIndex = oneFailMessage.indexOf("Expected:")
//从got:处进行分隔
int gotIndex = oneFailMessage.indexOf("got:") //解析错误信息中打印的JsonPath
String detailedJsonPath = oneFailMessage.substring(0,expectedIndex)
//解析JsonPath中的叶子节点
String lastLeafNode = detailedJsonPath.reverse().split("\\.")[0].reverse()
//解析Expected的值
String expectedValue = oneFailMessage.substring(expectedIndex,gotIndex).split(":")[1]
//解析Actual的值
String actualValue = oneFailMessage.substring(gotIndex).split(":")[1] //把需要输出的所有数据点拼接成一个list
def errorMessage = [testSuiteName,testCaseName]
errorMessage = errorMessage+valueList
errorMessage = errorMessage+[lastLeafNode,expectedValue,actualValue,detailedJsonPath] //把每条单独的list拼接成一个总的list返回
failMessageList.add(errorMessage)
}
return failMessageList
} def createExcel(File createFile, def topRow) {
String sheetName = "Automation Test";
try { XSSFWorkbook workbook = new XSSFWorkbook();
XSSFCellStyle cellStyleString = setCellStyleString(workbook)
XSSFCellStyle cellStyleTitle = setCellStyleTitle(workbook) int columnsNum = topRow.size();// calculate columns size by topRow
XSSFSheet sheet = workbook.createSheet(sheetName); int rownum = 0;
Row row = sheet.createRow(rownum++); int cellnum = 0;
for (String cellString : topRow) { Cell cell = row.createCell(cellnum++);
cell.setCellStyle(cellStyleString);
if (rownum == 1) {
cell.setCellStyle(cellStyleTitle);
}
// remove DIV style
if (cellString != null)
cellString = cellString.replaceAll('<(\\S*?)[^>]*>',"")
cell.setCellValue(cellString);// insert value
}
sheet = setSheetStyle(sheet, columnsNum -1);// setting sheet style FileOutputStream out = new FileOutputStream(createFile);
workbook.write(out);
out.flush();
out.close(); } catch (Exception e) {
e.printStackTrace();
}
} //向Excel中追加数据
def addExcel(String excelPath, ArrayList<String[]> failMessageList) throws IOException{
int columnsNum = failMessageList[0].size();// calculate columns size first row
FileInputStream fs = new FileInputStream(excelPath);//获取excel
XSSFWorkbook wb = new XSSFWorkbook(fs);
XSSFSheet sheet = wb.getSheetAt(0);//获取工作表
XSSFRow row = sheet.getRow(0);//获取第一行(即:字段列头,便于赋值)
int lastRowNum = sheet.getLastRowNum()
FileOutputStream out = new FileOutputStream(excelPath);//向excel中添加数据
for (int i = 0; i < failMessageList.size(); i++) {
row = sheet.createRow(++lastRowNum);//在现有行号后追加数据
String[] addOneRowData = failMessageList[i];
for(int j=0;j<addOneRowData.size();j++){
String str = addOneRowData[j];
row.createCell(j).setCellValue(str);//设置单元格的数据
}
}
sheet = setSheetStyle(sheet, columnsNum -1);// setting sheet style
wb.write(out);
out.flush();
out.close();
} def getExcelName( ) {
def currentStepIndex = context.currentStepIndex
def testCaseName = testRunner.testCase.getTestStepAt(currentStepIndex).getParent().getName()
def testSuiteName = testRunner.testCase.getTestStepAt(currentStepIndex).getParent().getParent().getName()
def excelName = testSuiteName + " _ " + testCaseName
excelName = excelName.replace(",","").replace(":","=").replace("/","").replace("*","")
return excelName
} def fillInFailMessage(File createFile, ArrayList<String[]> failMessageList, String[] topRow) {
String sheetName = "Automation Test";
try { XSSFWorkbook workbook = new XSSFWorkbook();
XSSFCellStyle cellStyleString = setCellStyleString(workbook)
XSSFCellStyle cellStyleTitle = setCellStyleTitle(workbook)
XSSFCellStyle cellStyleFail = setCellStyleFail(workbook)
XSSFCellStyle cellStyleNull = setCellStyleNull(workbook) failMessageList.add(0, topRow);
int columnsNum = topRow.length;// calculate columns size by topRow
XSSFSheet sheet = workbook.createSheet(sheetName); int rownum = 0;
for (int i = 0; i < failMessageList.size(); i++) { String[] rowStrings = failMessageList.get(i);
Row row = sheet.createRow(rownum++); int cellnum = 0;
for (String cellString : rowStrings) { Cell cell = row.createCell(cellnum++);
cell.setCellStyle(cellStyleString);
if (rownum == 1) {
cell.setCellStyle(cellStyleTitle); } else if (cellString != null && cellString.contains("%") && cellnum == columnsNum) {
Number number = NumberFormat.getInstance().parse(cellString); // If the actual deviation > 10%, change the cell color to red in the excel
if (number.doubleValue() > 10) {
cell.setCellStyle(cellStyleFail);
}
} else if ("null".equals(cellString) || "not null".equals(cellString)) {
// If cell value is "null" or "not null" change color to yellow in the excel
cell.setCellStyle(cellStyleNull);
}
// remove DIV style
if (cellString != null)
cellString = cellString.replaceAll('<(\\S*?)[^>]*>',"")
cell.setCellValue(cellString);// insert value
}
}
sheet = setSheetStyle(sheet, columnsNum -1);// setting sheet style FileOutputStream out = new FileOutputStream(createFile);
workbook.write(out);
out.flush();
out.close(); } catch (Exception e) {
e.printStackTrace();
}
} def XSSFSheet setSheetStyle(XSSFSheet sheet, int columnsNum) { sheet.createFreezePane(0, 1, 0, 1);
String columnRange = "A1:" + (char) (65 + columnsNum) + "1";
sheet.setAutoFilter(CellRangeAddress.valueOf(columnRange)); for (int i = 0; i <= columnsNum; i++)
sheet.autoSizeColumn(i); return sheet;
} def setCellStyleString(XSSFWorkbook workbook) {
XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFDataFormat dataFormat = workbook.createDataFormat();
cellStyle.setDataFormat(dataFormat.getFormat("@"));
return cellStyle;
} def setCellStyleTitle(XSSFWorkbook workbook) {
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(new XSSFColor(new Color(131, 191, 90)));
return cellStyle;
} def setCellStyleFail(XSSFWorkbook workbook) {
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(new XSSFColor(new Color(255, 0, 0)));
return cellStyle;
} def setCellStyleNull(XSSFWorkbook workbook) {
XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFFont fontStyle = workbook.createFont();
fontStyle.setColor(new XSSFColor(new Color(255, 0, 0)));
fontStyle.setBold(true);
cellStyle.setFont(fontStyle);
return cellStyle;
}
}
[Groovy] 创建Excel,追加Excel的更多相关文章
- C# 处理Excel公式(一)——创建、读取Excel公式
对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便.此外,对于某些数值的信息来源,我们也可以通过读取数据中包含 ...
- C# -- 使用Aspose.Cells创建和读取Excel文件
使用Aspose.Cells创建和读取Excel文件 1. 创建Excel Aspose.Cells.License li = new Aspose.Cells.License(); li.SetLi ...
- 使用poi读写excel、向excel追加数据等,包括.xls和.xlsx文档
1.使用maven引入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>p ...
- JS+Selenium+excel追加写入,使用python成功爬取京东任何商品~
之前一直是requests库做爬虫,这次尝试下使用selenium做爬虫,效率不高,但是却没有限制,文章是分别结合大牛的selenium爬虫以及excel追加写入操作而成,还有待优化,打算爬取更多信息 ...
- [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- c#导出Excel 使用EXCEL进程
private void exportExcel(string filename, string path,string title, List<ArchivedWcsTask> wcst ...
- Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和导出. 该 ...
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel ,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和 导出 ...
随机推荐
- K-means算法的实现
K-MEANS算法是一种经典的聚类算法,在模式识别得到了广泛的应用.算法中有两个关键问题需要考虑:一是如何评价对象的相似性,通常用距离来度量,距离越近越相似:另外一个是如何评价聚类的效果,通常采用误差 ...
- nltk的使用
1. 命令行; import nltk nltk.download() #下载相关模型等,...
- spring boot 代理(not eligible for auto-proxying)
spring 事务机制网上的案例很多,关于事务 不能回滚也有很多的类型,不同的问题有不同的处理方案,本篇博客主要介绍两种事务不能回滚的问题解决方案: 问题一: 在同一个对象中有两个方法,分别未方 ...
- Animator
[Animator] 1.State Machine Behaviours A State Machine Behaviour is a special class of script. In a s ...
- 第二篇:Jmeter功能概要
一.jmeter工具组成部分: 1.资源生成器:用于生成测试过程中服务器,负载机的资源代码: 2.用户运行器:通常是一个脚本运行引擎,根据脚本的要求模拟指定用户行为,(lr中的controller) ...
- 截图软件FastStone
屏幕截图软件(FastStone Capture) 好用,可以粘贴 / 复制. 可以做页面设计,有屏幕标尺,取色器.
- 调用高德地图API(热力图)详解
具体脚本语言如下: <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- jakson
Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行JSON和Java对象转换,下面给出一些Jackson的J ...
- windows上安装Anaconda和python
下载并安装 anaconda 先到https://www.continuum.io/downloads 下载anaconda, 现在的版本有python2.7版本和python3.5版本,下载好对应版 ...
- TZOJ 3305 Hero In Maze II(深搜)
描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开 ...