【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace
  oQNKXeHVeoXq 2023年11月13日 25 0

需求

提升群小伙伴的需求是这样的:使用jmeter测试接口,接口需要导入excel,需要自动生成excel文件以及数据

问题

idea中可以自动生成excel文件以及数据

【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace_apache

依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>

 

把相关依赖放到jmeter的lib/ext下后,把代码复制到beanshell,或者idea中maven项目打jar包放到jmeter的lib/ext目录下,都报错:

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel. . . . '' : Typed variable declaration : Class: Workbook not found in namespace

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval	Sourced file: inline evaluation of: ``import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel. . . . '' : Typed variable declaration : Class: Workbook not found in namespace

 

和这个问题一样:https://stackoverflow.com/questions/54341188/getting-the-follwoing-error-typed-variable-declaration-class-workbook-not-fo

【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace_java_02

 

附:原始代码

//import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;


        // 创建一个新的工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建一个新的工作表
        Sheet sheet = workbook.createSheet("Sheet1");

        // 创建新行
        Row row = sheet.createRow(0);

        // 创建单元格样式
        CellStyle style = workbook.createCellStyle();

        // 给style添加水平居中对齐
        style.setAlignment(HorizontalAlignment.CENTER);

        // ===设置字体样式===
        // 创建字体对象
        Font font = workbook.createFont();
        // 字体加粗
        font.setBold(true);
        // 设置字体大小
        font.setFontHeightInPoints((short) 20);
        // 设置字体类型
        font.setFontName("宋体");
        // 给style添加字体样式
        style.setFont(font);


        // 合并第二行的0-5列
        Cell cell = row.createCell(0);
        // 给单元格设置内容
        cell.setCellValue("供应商导入");
        // 给单元格设置样式
        cell.setCellStyle(style);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));



        // 创建单元格样式
        CellStyle style2 = workbook.createCellStyle();
        // ===设置字体样式===
        // 创建字体对象
        Font font2 = workbook.createFont();
        // 字体加粗
        font2.setBold(true);
        // 设置字体大小
        font2.setFontHeightInPoints((short) 11);
        // 设置字体类型
        font2.setFontName("宋体");
        style2.setFont(font2);

        // 创建新行
        Row row2 = sheet.createRow(1);
        Cell cell0 = row2.createCell(0);
        cell0.setCellStyle(style2);
        cell0.setCellValue("*供应商分类编码");
        Cell cell1=row2.createCell(1);
        cell1.setCellStyle(style2);
        cell1.setCellValue("*供应商编码");
        Cell cell2=row2.createCell(2);
        cell2.setCellStyle(style2);
        cell2.setCellValue("*供应商名称");
        Cell cell3=row2.createCell(3);
        cell3.setCellStyle(style2);
        cell3.setCellValue("*供应商简称");
        Cell cell4=row2.createCell(4);
        cell4.setCellStyle(style2);
        cell4.setCellValue("*助记码");
        Cell cell5=row2.createCell(5);
        cell5.setCellStyle(style2);
        cell5.setCellValue("*分管部门编码");
        Cell cell6=row2.createCell(6);
        cell6.setCellStyle(style2);
        cell6.setCellValue("税率");


        for (int i = 0; i < 1; i++) {
            Row rowi = sheet.createRow(i+2);
            for (int j = 0; j < 7 ; j++) {
                if (j==6){
                    rowi.createCell(j).setCellValue(3);
                    continue;
                }
                rowi.createCell(j).setCellValue("test"+j);
            }
        }


        // 保存工作簿到文件
        String filename = "供应商导入模板.xlsx";
        FileOutputStream fos = new FileOutputStream(filename);
        workbook.write(fos);
        fos.close();

        System.out.println("工作簿已保存到文件:" + filename);

  

解决方案

问题一:报错找不到接口

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.userm . . . '' : Typed variable declaration : Class: Workbook not found in namespace

类型改为类即可,比如:

将Workbook workbook = new XSSFWorkbook();

改为:XSSFWorkbook workbook = new XSSFWorkbook();

 

问题二:样式相关的类找不到

注释样式相关的,如:XSSFCellStyle style = workbook.createCellStyle();

 

问题三:依赖版本

Error invoking bsh method: eval	Sourced file: inline evaluation of: ``import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.userm . . . '' : Method Invocation workbook.write

  

依赖版本改为:

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

  

结果

【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace_lua_03

 

jmeter的bin下生成了文件

【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace_apache_04

 

文件内容

【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace_lua_05

 

【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace_lua_06

 

【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace_lua_07

 

【已解决,一个beanshell问题】Typed variable declaration : Class: Workbook not found in namespace_apache_08

 


__EOF__


本文作者:持之以恒(韧)

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
oQNKXeHVeoXq