【问题标题】:How to write data at particular cell in excel/csv file in jmeter using beanshell?如何使用beanshell在jmeter的excel/csv文件中的特定单元格写入数据?
【发布时间】:2018-06-13 14:06:33
【问题描述】:

我正在尝试使用 beanshell 将数据写入 excel 或 CSV 文件。但我可以在 excel 表中写入数据,但无法在 CSV 文件的特定单元格中写入数据。

下面是代码。

var response = prev.getResponseDataAsString(); 
f = new FileOutputStream("C:/Users/adityak/Desktop/K/app.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(response);
f.close();

任何帮助将不胜感激。

【问题讨论】:

  • 您有任何错误吗?为什么您“无法写入数据”。你能在里面写吗?请说明问题。

标签: jmeter qa jmeter-plugins beanshell


【解决方案1】:
  1. CSV 文件没有“单元格”,它是带有分隔符的纯文本文件,主要是逗号,因为 CSV 代表comma-separated values
  2. JMeter 3.1 it is recommended to use Groovy for scripting
  3. 假设以上所有:

例如,如果您有类似的文件:

1,2,3
4,5,6
7,8,9

如果想用hello 替换5,相关的Groovy 代码将类似于:

def csvFile = new File('/home/dtikhanski/Desktop/test.csv')
def lines = csvFile.readLines()


def secondLine = lines.get(1)
def entries = secondLine.split(",")
entries[1] = 'hello'
secondLine = entries.join(',')
lines.set(1, secondLine)

csvFile.withWriter{ out ->
    lines.each {out.println it}
}

演示:

【讨论】:

    猜你喜欢
    • 2016-06-23
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多