0
votes

The code below prints the content from the excel file (tried in eclipse) but I am not able to run it in Jmeter 3.1 using Groovy.

I throws error:

Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed

This is my code:

public static void main (String args[]) throws IOException
            {  
              GetExcelTableInto2DArrayListString("C:\\Users\\val1\\Desktop\\Book1.xlsx", true);


             }


            public static void GetExcelTableInto2DArrayListString(String excelFile, boolean debug) throws IOException{

            ArrayList<String> OUT = new ArrayList<String>();  
            File myFile = new File(excelFile); 
            FileInputStream fis = null;

                fis = new FileInputStream(myFile);

                String columnWanted = "PhysicalIDs";
                Integer columnNo = null;

            XSSFWorkbook myWorkBook = null;

                myWorkBook = new XSSFWorkbook (fis);


            // Return first sheet from the XLSX workbook 
            XSSFSheet mySheet = myWorkBook.getSheetAt(0); 

            // Get iterator to all the rows in current sheet 

            List<Cell> cells = new ArrayList<Cell>();

                Row firstRow = mySheet.getRow(0);   //rowIterator.next();


                for(Cell cell:firstRow){
                    if (cell.getStringCellValue().equals(columnWanted)){
                        columnNo = cell.getColumnIndex();
                    }
                }
                System.out.println(columnNo);
                DataFormatter formatter = new DataFormatter(Locale.US);

                if (columnNo != null){
                for (Row row : mySheet) {
                   Cell c = row.getCell(columnNo);
                   System.out.println(formatter.formatCellValue(c));
                   if (c == null) {
                      // Nothing in the cell in this row, skip it
                   } else {
                      cells.add(c);
                   }
                }
                }else{
                    System.out.println("could not find column " + columnWanted + " in first row of " + myFile.toString());
                }


            }
1
You have to fix all Compilation Errors. The error message is not full. If you want anybody to help you - edit your question and provide full error message.daggett
99% probability that the excel-related jars are not provided in jmeter's classpathinjecteer
I have these jars: xmlbeans-2.6.0.jar, poi-scratchpad-3.17.jar, poi-excelant-3.17.jar, commons-collections4-4.1.jar, ooxml-schemas-1.3.jar, poi-ooxml-3.17.jar, poi-3.17.jarVidz

1 Answers

0
votes

I strongly doubt that your code works in eclipse (whatever it is) and anywhere else because this line:

public static void main (String args[])

is not syntactically correct neither in Java nor in Groovy.

The "good" declaration of the entry point (although it's not required for Groovy scripts) would be:

public static void main(String[] args)

Full code just in case:

import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.DataFormatter
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook

public static void main(String[] args) throws IOException {
    GetExcelTableInto2DArrayListString("C:\\Users\\val1\\Desktop\\Book1.xlsx", true);


}


public static void GetExcelTableInto2DArrayListString(String excelFile, boolean debug) throws IOException {

    ArrayList<String> OUT = new ArrayList<String>();
    File myFile = new File(excelFile);
    FileInputStream fis = null;

    fis = new FileInputStream(myFile);

    String columnWanted = "PhysicalIDs";
    Integer columnNo = null;

    XSSFWorkbook myWorkBook = null;

    myWorkBook = new XSSFWorkbook(fis);


    // Return first sheet from the XLSX workbook
    XSSFSheet mySheet = myWorkBook.getSheetAt(0);

    // Get iterator to all the rows in current sheet

    List<Cell> cells = new ArrayList<Cell>();

    Row firstRow = mySheet.getRow(0);   //rowIterator.next();


    for (Cell cell : firstRow) {
        if (cell.getStringCellValue().equals(columnWanted)) {
            columnNo = cell.getColumnIndex();
        }
    }
    System.out.println(columnNo);
    DataFormatter formatter = new DataFormatter(Locale.US);

    if (columnNo != null) {
        for (Row row : mySheet) {
            Cell c = row.getCell(columnNo);
            System.out.println(formatter.formatCellValue(c));
            if (c == null) {
                // Nothing in the cell in this row, skip it
            } else {
                cells.add(c);
            }
        }
    } else {
        System.out.println("could not find column " + columnWanted + " in first row of " + myFile.toString());
    }


}

More information: