利用Jmeter自动生成导入的Excel文件

一、环境准备

1.下载jxl.ar

下载地址:https://files.cnblogs.com/files/wangyi0419/jxl.jar.zip   下载后请去掉.zip后缀。

(转自:https://www.cnblogs.com/wangyi0419/p/12001258.html)

 

2.下载IDEA(社区版即可)

下载地址:https://www.jetbrains.com/idea/download/#section=windowsIDEA

 

二、思路分析

1,Excell一般有三个最重要的元素:workbook,sheet,cell
2,想要把结果储存到指定的单元格,那就必须依照这个三个元素来定位。
3,先获取excel的文件名称
4,获取表单名
5,获取单元格的坐标
6,获取结果,写入到对应的单元格去
7,需要利用beanshell写java代码,获取对应的数据写入到Excel里面去。

 

三、代码编写


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import jxl.Cell;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class CWOutputFile {

public static void main(String[] args) throws RowsExceededException, WriteException, BiffException, IOException {
// CWOutputFile t = new CWOutputFile();
// String File = t.cOutputFile("导入模板下载","Jmeter测试文件夹");
}

/**
* wOutputFile 作用:把内容写入到Excel文件中。
* wOutputFile写结果文件 wOutputFile
*
* @throws IOException
* @throws BiffException
* @throws WriteException
*/
public void wOutputFile(String filepath, String pocId, String podName, String exchangeType, String contractWholesalerId, String contractWholesalerName, String writeOffWholesalerId,String writeOffWholesalerName,String StartTime) throws BiffException, IOException, WriteException {
File output = new File(filepath);
String result = "";
InputStream instream = new FileInputStream(filepath);
Workbook readwb = Workbook.getWorkbook(instream);
WritableWorkbook wbook = Workbook.createWorkbook(output, readwb); //根据文件创建一个操作对象
WritableSheet readsheet = wbook.getSheet(0); //定位到文件的第一个sheet页签
int rsRows = readsheet.getRows(); //获取sheet页签的总行数
//获取sheet表中所包含的总行数

/******************设置字体样式***************************/
WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD);
WritableCellFormat wcf = new WritableCellFormat(font);
/****************************************************/

Cell cell = readsheet.getCell(0, rsRows); //获取sheet页的单元格
if (cell.getContents().equals("")) {
Label labetest1 = new Label(0, rsRows, pocId); //第一列:售点ID
Label labetest2 = new Label(1, rsRows, podName); //第二列:售点名称
Label labetest3 = new Label(2, rsRows, exchangeType); //第三列:履约类型
Label labetest4 = new Label(3, rsRows, contractWholesalerId); //第四列:履约经销商ID
Label labetest5 = new Label(4, rsRows, contractWholesalerName); //第五列:履约经销商名称
Label labetest6 = new Label(5, rsRows, writeOffWholesalerId); //第六列:核销经销商ID
Label labetest7 = new Label(6, rsRows, writeOffWholesalerName); //第七列:核销经销商名称
Label labetest8 = new Label(7, rsRows, StartTime); //第八列:生效时间
readsheet.addCell(labetest1);
readsheet.addCell(labetest2);
readsheet.addCell(labetest3);
readsheet.addCell(labetest4);
readsheet.addCell(labetest5);
readsheet.addCell(labetest6);
readsheet.addCell(labetest7);
readsheet.addCell(labetest8);
}
wbook.write();
wbook.close();
}

/**
* cOutputFile 作用:创建Excel文件
* tradeType为文件名称前缀
* dirName为文件夹名称
*
* @throws IOException
* @throws WriteException
*/
public String cOutputFile(String tradeType,String dirName) throws IOException, WriteException {
String temp_str = "";
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
temp_str = sdf.format(dt); //获取时间戳
// 以时间戳命名结果文件,确保唯一
// 生成文件夹
File dirfile = new File("D:\\" + dirName);
if(!dirfile.exists() && !dirfile.isDirectory()){
dirfile.mkdir();
}
    
        // 生成文件路径
        String filepath = "D:\\" +dirName + "\\" + tradeType + "_output_" + "_" + temp_str + ".xls";
File output = new File(filepath);
if (!output.isFile()) {
// 如果指定的文件不存在,创建新该文件
output.createNewFile();
// 写文件
// 新建一个writeBook,在新建一个sheet
WritableWorkbook writeBook = Workbook.createWorkbook(output);
//命名sheet // createsheet(sheet名称,第几个sheet)
WritableSheet sheet = writeBook.createSheet("UTC导入模板", 0);
//设置首行字体为宋体,11号,加粗
WritableFont headfont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.BOLD);
WritableCellFormat headwcf = new WritableCellFormat(headfont);
headwcf.setBackground(Colour.GRAY_25); // 灰色颜色
// 设置列宽度setcolumnview(列号,宽度)
       // 设置列宽度setcolumnview(列号,宽度)
       for(int i = 0;i<=7;i++){
     sheet.setColumnView(i, 20); //设置列宽
       }
            headwcf.setAlignment(Alignment.CENTRE); //设置文字居中对齐方式;//文字居中
headwcf.setVerticalAlignment(VerticalAlignment.CENTRE); // 设置垂直居中;
Label labe = new Label(0, 0, "填写说明:Jmeter测试文件");
Label labe00 = new Label(0, 1, "售点ID", headwcf); //写入内容:Label(列号,行号,内容)
Label labe10 = new Label(1, 1, "售点名称", headwcf);
Label labe20 = new Label(2, 1, "履约类型", headwcf);
Label labe30 = new Label(3, 1, "履约经销商ID", headwcf);
Label labe40 = new Label(4, 1, "履约经销商名称", headwcf);
Label labe50 = new Label(5, 1, "核销经销商ID", headwcf);
Label labe60 = new Label(6, 1, "核销经销商名称", headwcf);
Label labe70 = new Label(7, 1, "生效时间", headwcf);
sheet.addCell(labe);
sheet.addCell(labe00);
sheet.addCell(labe10);
sheet.addCell(labe20);
sheet.addCell(labe30);
sheet.addCell(labe40);
sheet.addCell(labe50);
sheet.addCell(labe60);
sheet.addCell(labe70);
writeBook.write();
writeBook.close();
}
return filepath;
}
}

 

代码结构:

 

四、将class导出为jar包

1、在IDEA左上角点击File-Project Structure

2.点击Artifacts,点击“+”,选择JAR,选择From modules with dependencies

 

 3.将需要用到的class类导出为jar包

 4.点击顶部菜单Build,选择Build Artifacts

 

五、Jmeter操作

1、将导出的jar包添加到jmeter的   lib/ext  文件夹中

2、在Jmeter的测试计划中导入这个jar包

3、将需要写入到Excel文件中的数据查询出来并参数化

4、创建Excel文件

 

 5、将数据写入Excel文件

filePath:文件路径,已经在创建Excel文件的时候已存储起来并参数化

pocId、pocName:需要写入到Excel中的参数名称(pocid是直接用数据库查询出来的,pocName已定义在用户初始化参数变量)

 

问题延伸:

在利用filepath转为jmeter变量时

调用s.wOutputFile() jmeter报错内容如下:

Error invoking bsh method: eval In file: inline evaluation of: ``CWOutputFile s =.....

 

调试过程:

1.可以加两行打印

log.info("filepath is : " + vars.get("filepath"));
log.info("test is : " + "${filepath}");

 

看到jmeter回显信息如下:

 INFO o.a.j.u.BeanShellTestElement: filePath is : D:\test\test_output__20220221125452.xls

 INFO o.a.j.u.BeanShellTestElement: test is : D:    est    est_output__20220221125452.xls

 

说明打印jmter变量时因为楼主命名的文件夹刚好为D:\test 因此jmeter将\t转义了 导致调用s.wOutputFile()时没有正确传入文件路径

 

解决方案:

用vars.put()将filepath文件路径转string类型

 

具体实现:

String writeReportPah=vars.get("filepath")

s.wOutputFile(writeReportPah, "XX",  "XX",  "XX",  "XX",  "XX");

 

6、执行结果

 

posted @ 2023-03-27 16:21  路易吉  阅读(76)  评论(0编辑  收藏  举报