【待解决】Typed variable declaration : Class: Workbook not found in namespace

发布时间 2023-08-21 16:00:19作者: 全栈测试笔记

beanshell中写excel,遇到如下问题:

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

idea中可以成功

 

有没有哪位小伙伴遇到过?

代码:

//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 = "test.xlsx";
        FileOutputStream fos = new FileOutputStream(filename);
        workbook.write(fos);
        fos.close();

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