[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文件的导入和 导出 ...
随机推荐
- iptables禁止别人,允许自己
这里举个例子,以ping为案例:禁止别人ping自己,但允许自己ping别人. [root@localhost ~]# iptables -A INPUT -p icmp --icmp-type 8 ...
- 10.18号java课后动手动脑
问题一结论:类如果提供了一个自定义的构造方法,将导致系统不再提供默认构造方法. 问题二结论:(1)程序运行的结果是100和300,field=200为类的初始化块,可以在类中使用“{”和“}”将语句包 ...
- 1.3.3、CDH 搭建Hadoop在安装之前(端口---CDH组件使用的端口)
列出的所有端口都是TCP. 在下表中,每个端口的“ 访问要求”列通常是“内部”或“外部”.在此上下文中,“内部”表示端口仅用于组件之间的通信; “外部”表示该端口可用于内部或外部通信. Compone ...
- Javascript Property Names
[Javascript Property Names] Property names must be strings. This means that non-string objects canno ...
- BOM 对象--location、navigator、screen、history
1.location 对象 location提供了与当前窗口中加载的文档有关的信息,还有一些导航功能.需要注意的是,window.location 和 document.location 引用的是同一 ...
- xnconvert 图片转换工具
xnconvert是一款简单高效的图片转换工具.xnconvert能够批量地进行图片格式转换,并具有一定的图片处理功能,可以增加水印.特效,支持放大缩小.旋转等. xnconvert功能介绍: 你可以 ...
- jquery mobil 和页面适应
<meta name="viewport" content="width=device-width" />
- 前端基础之css介绍
CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...
- string类的一些函数方法
1.请查看String.equals()方法的实现代码,注意学习其实现方法: (1)源程序: public class StringEquals { /** * @param args the com ...
- TZOJ 2722 Matrix(树状数组区间取反单点查询)
描述 Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row ...