importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.text.SimpleDateFormat;importjava.util.Date;importjxl.Cell;importjxl.LabelCell;importjxl.Workbook;importjxl.format.Alignment;importjxl.format.Colour;importjxl.format.VerticalAlignment;importjxl.read.biff.BiffException;importjxl.write.Label;importjxl.write.WritableCellFormat;importjxl.write.WritableFont;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;importjxl.write.WriteException;public classCWOutputFile {/*** wOutputFile写结果文件 wOutputFile(文件路径、用例编号、用例标题、预期结果、实际结果、测试结果)
*@throwsIOException
*@throwsBiffException
*@throwsWriteException*/
public void wOutputFile(String filepath,String caseNo,String testPoint,String testData,String preResult,String fresult) throwsBiffException, IOException, WriteException{
File output=newFile(filepath);
String result= "";
InputStream instream= newFileInputStream(filepath);
Workbook readwb=Workbook.getWorkbook(instream);
WritableWorkbook wbook= Workbook.createWorkbook(output, readwb); //根据文件创建一个操作对象
WritableSheet readsheet = wbook.getSheet(0); //定位到文件的第一个sheet页签
int rsRows = readsheet.getRows(); //获取sheet页签的总行数
/******************设置字体样式***************************/WritableFont font= new WritableFont(WritableFont.createFont("宋体"),10,WritableFont.NO_BOLD);
WritableCellFormat wcf= newWritableCellFormat(font);/****************************************************/Cell cell= readsheet.getCell(0,rsRows); //获取sheet页的单元格
if(cell.getContents().equals("")){
Label labetest1= new Label(0,rsRows,caseNo); //第一列:用例编号
Label labetest2 = new Label(1,rsRows,testPoint);//第二列:用例标题
Label labetest3 = new Label(2,rsRows,testData); //第三列:测试数据
Label labetest4 = new Label(3,rsRows,preResult);//第四列:预期结果
Label labetest5 = new Label(4,rsRows,fresult); //第五列:实际结果
if(preResult.equals(fresult)){
result= "通过";
wcf.setBackground(Colour.BRIGHT_GREEN);//预期结果和实际结果相同,测试通过
}else{
result= "不通过";
wcf.setBackground(Colour.RED);//预期结果和实际结果不相同,测试不通过
}
Label labetest6= new Label(5,rsRows,result,wcf);//第六列:测试结果
readsheet.addCell(labetest1);
readsheet.addCell(labetest2);
readsheet.addCell(labetest3);
readsheet.addCell(labetest4);
readsheet.addCell(labetest5);
readsheet.addCell(labetest6);
}
wbook.write();
wbook.close();
}/**cOutputFile 创建输出Excel文件 cOutputFile,tradeType为文件名称前缀,返回结果:文件路径,作为wOutputFile写入结果文件的入参
*@throwsIOException
*@throwsWriteException*/
public String cOutputFile(String tradeType) throwsIOException, WriteException{
String temp_str= "";
Date dt= newDate();
SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");
temp_str= sdf.format(dt); //获取时间戳
String filepath = "D:\\\\"+tradeType+"_output_" + "_" + temp_str + ".xls";
File output= newFile(filepath);if(!output.isFile()){//文件不存在,创建新文件
output.createNewFile();//写文件
WritableWorkbook writeBook =Workbook.createWorkbook(output);
WritableSheet sheet= writeBook.createSheet("输出结果", 0); //命名sheet
WritableFont headfont = new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.BOLD);//设置首行字体为宋体,11号,加粗
WritableCellFormat headwcf = newWritableCellFormat(headfont);
headwcf.setBackground(Colour.GRAY_25);
sheet.setColumnView(0, 11); //设置列宽
sheet.setColumnView(1, 20);
sheet.setColumnView(2, 40);
sheet.setColumnView(3, 10);
sheet.setColumnView(4, 10);
sheet.setColumnView(5, 10);
headwcf.setAlignment(Alignment.CENTRE);//文字居中
headwcf.setVerticalAlignment(VerticalAlignment.CENTRE);
Label labe00= new Label(0,0,"用例编号",headwcf); //写入内容:Label(列号,行号,内容)
Label labe10 = new Label(1,0,"用例标题",headwcf);
Label labe20= new Label(2,0,"测试数据",headwcf);
Label labe30= new Label(3,0,"预期结果",headwcf);
Label labe40= new Label(4,0,"实际结果",headwcf);
Label labe50= new Label(5,0,"执行结果",headwcf);
sheet.addCell(labe00);
sheet.addCell(labe10);
sheet.addCell(labe20);
sheet.addCell(labe30);
sheet.addCell(labe40);
sheet.addCell(labe50);
writeBook.write();
writeBook.close();
}returnfilepath;
}
}