问题描述
我需要在jmeter中为上传场景创建一个excel文件.excel 有 3 列,行数是来自参数文件的动态值.对于不同的 excel,行值不能具有相同的数据.所以我使用随机字符串来创建数据.通过硬编码行数,我可以使用 apache poi 创建具有以下代码的文件,但面临处理动态行数的问题.有人可以提供解决方案吗?
I need to create an excel file for upload scenario in jmeter. The excel has 3 columns and number of rows is a dynamic value coming from parameter file.The row values cannot have same data for different excel. So I am using random string to create data. By hard coding number of rows I am able to create file with below code using apache poi but facing issues to handle dynamic number of rows. Can somebody please provide solution?
下面是创建 5 行的代码.
Below is the code which is working fine for creating 5 rows.
def path = FileServer.getFileServer().getBaseDir;
def separator = File.separator;
def sourceFileName = "CreateDynamicExcel";
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Billing");
Object[] dataTypes = [
["Column1Header","Column2Header","Column3Header"],
["${__RandomString(10,abcdefghij,)}","${__Random(100000000,199999999,)}","${__RandomString(10,abcdefghijklmnopqrst,)}"],
["${__RandomString(10,abcdefghij,)}","${__Random(100000000,199999999,)}","${__RandomString(10,abcdefghijklmnopqrst,)}"],
["${__RandomString(10,abcdefghij,)}","${__Random(100000000,199999999,)}","${__RandomString(10,abcdefghijklmnopqrst,)}"],
["${__RandomString(10,abcdefghij,)}","${__Random(100000000,199999999,)}","${__RandomString(10,abcdefghijklmnopqrst,)}"]];
int rowNum = 0;
for (Object[] datatype:datatypes)
HSSFRow = sheet.createRow(rowNum++);
int colNum = 0;
for(Object filed:datatype){
HSSFCell cell = row.createCell(colNumn+=);
if(filed.instanceof(String){
cell.setCellValue((String) filed);
}
if(filed.instanceof(Integer){
cell.setCellValue((Integer) filed);
}
}
try{
FileOutputStream out = new FileOutputStream(new File(path+separator+sourceFileName+".xls"));
workbook.write(out);
out.close();
}
catch(FileNotFoundException e){
e.printStacktrace();
}
推荐答案
我认为你不应该内联 JMeter 函数或变量 因为:
I don't think you should be inlining JMeter Functions or Variables in Groovy scripts because:
- 它与 Groovy GString 模板引擎语法冲突
- 只有第一次出现才会被缓存并用于后续迭代立>
因此您可以使用以下表达式:
So you can use the following expressions instead:
org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric(10)
org.apache.commons.lang3.RandomUtils.nextInt(100000000, 199999999)
- 等
如果有任何问题 - 请查看 jmeter.log文件,如果出现任何问题,您应该找到根本原因或至少在那里找到线索
In case of any problems - take a look at jmeter.log file, in case of any issues you should find the root cause or at least a clue there
这篇关于jmeter中的Excel处理以添加多个动态行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!