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的更多相关文章

  1. C# 处理Excel公式(一)——创建、读取Excel公式

    对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便.此外,对于某些数值的信息来源,我们也可以通过读取数据中包含 ...

  2. C# -- 使用Aspose.Cells创建和读取Excel文件

    使用Aspose.Cells创建和读取Excel文件 1. 创建Excel Aspose.Cells.License li = new Aspose.Cells.License(); li.SetLi ...

  3. 使用poi读写excel、向excel追加数据等,包括.xls和.xlsx文档

    1.使用maven引入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>p ...

  4. JS+Selenium+excel追加写入,使用python成功爬取京东任何商品~

    之前一直是requests库做爬虫,这次尝试下使用selenium做爬虫,效率不高,但是却没有限制,文章是分别结合大牛的selenium爬虫以及excel追加写入操作而成,还有待优化,打算爬取更多信息 ...

  5. [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!

    引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...

  6. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  7. c#导出Excel 使用EXCEL进程

    private void exportExcel(string filename, string path,string title, List<ArchivedWcsTask> wcst ...

  8. Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能

    1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和导出. 该 ...

  9. 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能

    1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel ,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和 导出  ...

随机推荐

  1. xm数据写入

    reshape有两个参数: 其中,参数:cn为新的通道数,如果cn = 0,表示通道数不会改变. 参数rows为新的行数,如果rows = 0,表示行数不会改变. 注意:新的行*列必须与原来的行*列相 ...

  2. Linux系统下面crontab选择默认编译器

    crontab修改默认编辑器 crontab默认编辑器为nano. 修改crontab默认编辑器为vi或者其他的编辑器. 法一: export EDITOR="/usr/bin/vim&qu ...

  3. Ubuntu系统查看mongo得慢日志,及一些操作

    摘要 在MySQL中,慢查询日志是经常作为我们优化查询的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是开启Profiling功能.该工具在运行的实例上收集有关MongoDB的写操作 ...

  4. yml和properties配置文件区别

    我们可以观察到的格式就是yml文件是梯级呈现的,我们应该注意一下几个方面: 1>在properties文件里面的 “ .”  连接在yml文件里面全部换成 ":" 进行连接, ...

  5. django通过添加session来保存公共变量

    有时候我们需要所有页面都使用同一个变量,比如用户登录信息.那不可能render每一个页面时都去传递一个变量,会非常麻烦 而用session可以解决这个问题   web的session可以通过reque ...

  6. javascript 页面导出功能

    javascript 页面导出功能 <a class="btn" href="javascript:void(0);" onclick="win ...

  7. centos7 mysql主从数据库同步

    主:192.168.2.222:从:192.168.2.223 一:安装mysql 从最新版本的linux系统开始,默认的是 Mariadb而不是mysql!这里依旧以mysql为例进行展示 1.先检 ...

  8. 批量执行命令(SSH)

    for a in {1..6} ; do scp /etc/hosts enc-bigdata0$a:/etc/hosts ; done

  9. Cookie 和Session 简介

    前言 HTTP是一种无状态的协议,为了分辨链接是谁发起的,需自己去解决这个问题.不然有些情况下即使是同一个网站每打开一个页面也都要登录一下.而Session和Cookie就是为解决这个问题而提出来的两 ...

  10. TOJ 3589 likaer的最长点距

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3589 时间限制(普通/Jav a) ...