You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Groovy使用jxl写入Excel提示Label无匹配构造函数报错求解

错误原因

你遇到的groovy.lang.GroovyRuntimeException: Could not find matching constructor for: jxl.write.Label(java.lang.Integer, java.lang.String, java.lang.String)报错,核心问题是参数类型不匹配:
jxl.write.Label的官方构造器签名为Label(int column, int row, String content),要求前两个入参是原始int类型,但你代码中从Properties测试步骤读取的row、column值默认是String类型,Groovy自动转型为Integer包装类,导致构造器参数匹配失败。

同时代码还存在几个隐性问题:已存在Excel打开后未转为可写模式、重复添加单元格、重复关闭工作簿,都会导致后续运行报错。

修复步骤
  1. 读取行号、列号时显式转为int类型
  2. 修正已存在Excel的打开逻辑,转为可写工作簿
  3. 修复重复添加单元格、重复关闭工作簿的逻辑错误
  4. 数值存回Properties时显式转为字符串类型
修正后完整代码
import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
import jxl.*
import jxl.write.*
import jxl.write.Label

def TestCase = context.testCase
PropertiesTestStep = TestCase.getTestStepByName("Properties")
Stop = PropertiesTestStep.getPropertyValue("End").toString()

if(Stop!="True"){
    def response = testRunner.testCase.getTestStepByName('GetSectors').getPropertyValue("response")
    def samplexmlreq=new XmlHolder(response)
    def count = PropertiesTestStep.getPropertyValue("Counter")
    def result
    // 显式转为int类型
    def row = PropertiesTestStep.getPropertyValue("row").toInteger()
    def column = PropertiesTestStep.getPropertyValue("column").toInteger()
    def flag=0
    WritableWorkbook wb   
    WritableSheet sheet
  
    try{
        def token = samplexmlreq.getNodeValue("/*:Envelope/*:Body/tRisePtz:GetSectorsResponse/tRisePtz:Sectors["+count+"]/tRisePtz:SectorToken")
        def name = samplexmlreq.getNodeValue("/*:Envelope/*:Body/tRisePtz:GetSectorsResponse/tRisePtz:Sectors["+count+"]/tRisePtz:SectorName")      
        def leftLimit = samplexmlreq.getNodeValue("/*:Envelope/*:Body/tRisePtz:GetSectorsResponse/tRisePtz:Sectors["+count+"]/tRisePtz:LeftLimit")
        def rightLimit = samplexmlreq.getNodeValue("/*:Envelope/*:Body/tRisePtz:GetSectorsResponse/tRisePtz:Sectors["+count+"]/tRisePtz:RightLimit")
        def TokenFromProperies = PropertiesTestStep.getPropertyValue("SectorToken")
        def NameFromProperies = PropertiesTestStep.getPropertyValue("SectorName")
        def LeftLimitFromProperies = PropertiesTestStep.getPropertyValue("LeftLimit")
        def RightLimitFromProperies = PropertiesTestStep.getPropertyValue("RightLimit")    
        if(name == NameFromProperies && leftLimit == LeftLimitFromProperies && rightLimit == RightLimitFromProperies ){
            result  = "Passed"
        }else{
            result  = "Failed"
        }
        def projectPath = new com.eviware.soapui.support.GroovyUtils(context).projectPath
        def folderPath = projectPath + "/SoapUIResults/SectorOSDActions/";
        def resultFolder = new File(folderPath);  
      
        if(!resultFolder.exists()){
            resultFolder.mkdirs();
        }
        def reportFile = new File(resultFolder,"TestSuite_Report.xls")
        // 修复已存在文件的可写转换逻辑
        if(!reportFile.exists()){
            wb = Workbook.createWorkbook(reportFile) 
        }else{
            Workbook existingWb = Workbook.getWorkbook(reportFile)
            wb = existingWb.createWritableWorkbook()
        }
        // 校验sheet是否存在
        if (wb.getNumberOfSheets() != 0) {
            for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                if (wb.getSheetName(i).equals("GoToSector")) {
                    sheet = wb.getSheet("GoToSector");
                    flag=1
                    break;
                } 
            }
        }
  
        if(flag == 0){
            sheet = wb.createSheet("GoToSector",0);
            Label label = new Label(0, 0, "Type");
            sheet.addCell(label);
            Label label1 = new Label(1, 0, "SectorName");
            sheet.addCell(label1);
            Label label2 = new Label(2, 0, "LeftLimit");
            sheet.addCell(label2);
            Label label3 = new Label(3, 0, "RightLimit");
            sheet.addCell(label3);     
        }
          
        Label label4 = new Label(0,row,"Request")
        sheet.addCell(label4);
        Label label5 = new Label(column+1,row,NameFromProperies)
        sheet.addCell(label5);
        Label label6 = new Label(column+2,row,LeftLimitFromProperies)
        sheet.addCell(label6);
        Label label7 = new Label(column+3,row,RightLimitFromProperies)
        sheet.addCell(label7);
        Label label8 = new Label(0,row,"Response")
        sheet.addCell(label8);
        Label label9 = new Label(column,row+1,name)
        sheet.addCell(label9);
        Label label10 = new Label(column,row+2,leftLimit)
        sheet.addCell(label10);
        Label label11 = new Label(column,row+3,rightLimit)
        sheet.addCell(label11);
        // 修复重复添加label11的错误
        Label label12 = new Label(0,row,"Status")
        sheet.addCell(label12);
        Label label13 = new Label(column,row,result)
        sheet.addCell(label13);
        
        // 数值存回Properties时转为字符串
        PropertiesTestStep.setPropertyValue("row",(row+1).toString())  
        PropertiesTestStep.setPropertyValue("column",(column+1).toString())             
    }catch(exc){
        log.error("Exception happened: " + exc.toString());
    }finally{
        // 只在finally中执行一次写入和关闭,避免重复操作空指针
        if(wb != null){
            wb.write();
            wb.close();
        }
    }
}

内容的提问来源于stack exchange,提问作者user9037825

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 11:54:00