Java将测试结果写入Excel文件

原文地址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;
    /**
     * 
     * @param packageName : 包名
     * @param className : 类名
     * @param methodName :方法名
     * @param remark : 注释
     * @param result : 结果 pass fail
     * @param reason :原因 pass 则为空 ,fail则有失败原因
     */
    public void writeExcel(String packageName ,String className,String methodName ,String remark ,String result ,String reason){
        try{
            /**
             * 可以尝试,每次都生成不同的excel文档,往里面添加内容,但是需要office excel
             */
            //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);//一定要写这句代码,否则无法将数据写入excel文档中
            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

这里写图片描述


猜你喜欢

转载自blog.csdn.net/Jay112011/article/details/80102161