Gatling :- scala 从 excel 文件中读取 JSON 对象

Gatling :- scala Read JSON object from excel file

我想从某个 my.xls 文件中读取 requestPayLoad Json 对象,并且还想将响应写回给它。怎么办呢

我是 scala 的新手,所以 Code spinet 会有所帮助。

我指的是这个库,但它不工作

https://github.com/folone/poi.scala

我写了 java 代码如下,但无法在 Scala 中模拟相同的代码

public class ExcelUtils {

    private static Sheet excelWorkSheet;
    private static Workbook excelWorkBook;
    private static Cell cell;

    public static void setExcelFile(String path, String sheetName) throws Exception {
        InputStream excelFileInputStream = new FileInputStream(path);

        String fileExtensionName = path.substring(path.indexOf("."));

        if (fileExtensionName.equals(".xlsx")) {
            excelWorkBook = new XSSFWorkbook(excelFileInputStream);
        } else if(fileExtensionName.equals(".xls")) {
            excelWorkBook = new HSSFWorkbook(excelFileInputStream);
        }

        excelWorkSheet = excelWorkBook.getSheet(sheetName);
    }

    public static String getCellData(int rowNum, int colNum) throws Exception {
        cell = excelWorkSheet.getRow(rowNum).getCell(colNum);
        String cellData = cell.getStringCellValue();
        return cellData;
    }

    public static Sheet getExcelWorkSheet() throws IOException {
        return excelWorkSheet;
    }
}

终于可以解决了。在 build.gradle 文件

中添加了 gradle 依赖项
dependencies {
//  compile ('commons-collections:commons-collections:3.2')
    compile ('org.scala-lang:scala-library:2.11.8')
    compile ('org.scala-lang:scala-compiler:2.11.8')
    compile group: 'org.apache.poi', name: 'poi', version: '3.9'
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.9'
}

scala代码是

package mypackage

import java.io.FileInputStream

import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook

class ExcelUtils {

//  private var cellData: String = null;

  def getCellData(path: String, sheetName: String, rowNum: Int, colNum: Int): String = {
    val excelFileInputStream = new FileInputStream(path);

    val fileExtensionName = path.substring(path.indexOf("."));
    var cellData: String = null;

    if (fileExtensionName.equals(".xlsx")) {
      val excelWorkBookXlxs = new XSSFWorkbook(excelFileInputStream);
      val excelWorkSheetXlxs = excelWorkBookXlxs.getSheet(sheetName);
      val cell = excelWorkSheetXlxs.getRow(rowNum).getCell(colNum);
      cellData = cell.getStringCellValue();
    } else if (fileExtensionName.equals(".xls")) {
      val excelWorkBookXls = new HSSFWorkbook(excelFileInputStream);
      val excelWorkSheetXls = excelWorkBookXls.getSheet(sheetName);
      val cell = excelWorkSheetXls.getRow(rowNum).getCell(colNum);
      cellData = cell.getStringCellValue();
    }
    return cellData;
  }
}