原文地址https://blog.csdn.net/qq_15283475/article/details/55517292
单元测试结果写入Excel文件
report.writeExcel(packageName,className,methodName,remark,”success”,reason);
这句代码就是把结果写入excel文件的。
import java.io.FileInputStream
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.Date
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.CellStyle
import org.apache.poi.ss.usermodel.IndexedColors
public class ExcelJunitReport {
public static int rowNumber = 1
public void writeExcel(String packageName ,String className,String methodName ,String remark ,String result ,String reason){
try{
//report文件的路径
String path = "D:\\excel\\JunitReport.xls"
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(path))
HSSFSheet sheet=wb.getSheetAt(0)
//获得EXCEL行数
int rowNums=sheet.getLastRowNum()
// System.out.println("多少行:" +rowNums)
//往sheet中追加一行数据
int rowCurrentNumber = rowNums+1
sheet.createRow(rowCurrentNumber)
HSSFRow row = sheet.getRow(rowCurrentNumber)
//格式
CellStyle cellStyle2=wb.createCellStyle()
cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex())
cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND)
cellStyle2.setBorderBottom(CellStyle.BORDER_THIN)
if(row != null){
//System.out.println("行不为空!" )
Date now = new Date()
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
String currentTime = dateFormat.format( now )
//创建单元格并赋值
row.createCell(0).setCellValue(currentTime)
row.createCell(1).setCellValue(packageName)
row.createCell(2).setCellValue(className)
row.createCell(3).setCellValue(methodName)
row.createCell(4).setCellValue(remark)
row.createCell(5).setCellValue(result)
if(result.equals("fail")){
row.getCell(5).setCellStyle(cellStyle2)
}
row.createCell(6).setCellValue(reason)
}else{
//System.out.println("行为空!" )
}
FileOutputStream os = new FileOutputStream(path)
wb.write(os)
os.close()
}catch(Exception e){
e.printStackTrace()
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
excel文件:D:\excel\JunitReport.xls