Java 解决错误的请求无法从void转换为HSSFCell

Java 解决错误的请求无法从void转换为HSSFCell,java,excel,apache-poi,Java,Excel,Apache Poi,我试图从包含公式的excel文件中读取数据,并将数据写入另一个excel的特定列,但通过使用以下代码,我收到一条错误消息,如:无法在第68行从void转换为HSSFCell @Test public void SampleCustNumFormat() throws Exception { String [] myXL = getExcelData(); //Write Data into Excel. //See what has been rea

我试图从包含公式的excel文件中读取数据,并将数据写入另一个excel的特定列,但通过使用以下代码,我收到一条错误消息,如:无法在第68行从void转换为HSSFCell

 @Test 

    public void SampleCustNumFormat() throws Exception { 

    String [] myXL = getExcelData();

    //Write Data into Excel.


   //See what has been read from Excel
    System.out.println (" Before Loop " + myXL[1]);

    for (int i=0; i<xRows; i++){
          System.out.println (" Cust Num " + myXL[i]);
    }

    FileOutputStream out = new FileOutputStream ("C:\\SCE docs\\Automation\\TestExcelData.xls");
    HSSFWorkbook myWB = new HSSFWorkbook(); 
    HSSFSheet sheet = myWB.getSheetAt(0); 

    for (int k=1; k<=sheet.getLastRowNum(); k++)
    {
    HSSFCell cell = sheet.getRow(1).createCell(2).setCellValue(myXL[k]);
    }
    myWB.write(out);
    out.close();
 }




public  String [] getExcelData() throws Exception{

 String [] tabArray=null;

            FileInputStream fi = new FileInputStream("C:\\SCE docs\\Automation\\CustomerAccount_Information.xls"); 
            HSSFWorkbook myWB = new HSSFWorkbook(fi); 
            HSSFSheet mySheet = myWB.getSheetAt(0); 

            FormulaEvaluator evaluator = myWB.getCreationHelper().createFormulaEvaluator();

            xRows = mySheet.getLastRowNum()+1; 
           tabArray = new String [xRows]; 

            for (int i=0;i<xRows;i++) 
            { 
            HSSFRow row = mySheet.getRow(i); 
                HSSFCell cell = row.getCell(3); 
                CellValue cellValue = evaluator.evaluate(cell);
                String value = evaluateFormula(cellValue);
                tabArray[i]=value;
            }
            return tabArray;

            }





private String evaluateFormula(CellValue cellValue) throws Exception{

int type = cellValue.getCellType();

Object result=null;

switch (type) {

case HSSFCell.CELL_TYPE_BOOLEAN:
    result = cellValue.getBooleanValue();
     break;
 case HSSFCell.CELL_TYPE_NUMERIC:
     result = cellValue.getNumberValue();
     break;
 case HSSFCell.CELL_TYPE_STRING:
     result = cellValue.getStringValue();
     break;
 case HSSFCell.CELL_TYPE_BLANK:
     break;
 case HSSFCell.CELL_TYPE_ERROR:
     break;


 // CELL_TYPE_FORMULA will never happen
 case HSSFCell.CELL_TYPE_FORMULA: 
     break;
  }

return result.toString();
 }
            }
@测试
public void SampleCustNumFormat()引发异常{
字符串[]myXL=getExcelData();
//将数据写入Excel。
//查看从Excel中读取的内容
System.out.println(“循环前”+myXL[1]);

对于(int i=0;i,这些线导致了故障:

for (int k=1; k<=sheet.getLastRowNum(); k++)
{
HSSFCell cell = sheet.getRow(1).createCell(2).setCellValue(myXL[k]);
}
通过这种方式,您将首先创建单元格并将其分配给
单元格
。然后,您可以设置值

编辑:或者,您可以(正如Sankumarsingh所指出的)不指定该值,而是在一行中完成所有操作,如下所示:

for (int k=1; k<=sheet.getLastRowNum(); k++)
{
HSSFCell cell = sheet.getRow(1).createCell(2);
cell.setCellValue(myXL[k]);
}
for (int k=1; k<=sheet.getLastRowNum(); k++)
{
sheet.getRow(1).createCell(2).setCellValue(myXL[k]);
}

for(int k=1;k在Akoksis之后,我必须重新编写您的程序。请检查它是否对您有效

@Test 

public void SampleCustNumFormat() throws Exception { 
    FileInputStream fi = new FileInputStream("C:\\SCE docs\\Automation\\CustomerAccount_Information.xls"); 
    HSSFWorkbook myWB = new HSSFWorkbook(fi); 
    HSSFSheet mySheet = myWB.getSheetAt(0); 
    String [] myXL = getExcelData(myWB);
   //See what has been read from Excel
    System.out.println (" Before Loop " + myXL[1]);
    for (int i=0; i<xRows; i++){
          System.out.println (" Cust Num " + myXL[i]);
    }
    FileInputStream file = new FileInputStream("C:\\SCE docs\\Automation\\TestExcelData.xls");
    HSSFWorkbook wb = new HSSFWorkbook(file); 

    for (int k=1; k<=sheet.getLastRowNum(); k++){
          wb.getSheet(0).getRow(1).createCell(2).setCellValue(myXL[k]);
    }
    //Write Data into Excel.
    FileOutputStream out = new FileOutputStream ("C:\\SCE docs\\Automation\\TestExcelData.xls");
    wb.write(out);
    out.close();
}



public  String [] getExcelData(HSSFWorkbook myWB) throws Exception{
    String [] tabArray=null;
    HSSFSheet mySheet = myWB.getSheetAt(0); 
    FormulaEvaluator evaluator = myWB.getCreationHelper().createFormulaEvaluator();
    xRows = mySheet.getLastRowNum()+1; 
    tabArray = new String [xRows]; 
    for (int i=0;i<xRows;i++){ 
        HSSFRow row = mySheet.getRow(i); 
        HSSFCell cell = row.getCell(3); 
        CellValue cellValue = evaluator.evaluate(cell);
        String value = evaluateFormula(cellValue);
        tabArray[i]=value;
    }
    return tabArray;
}





private String evaluateFormula(CellValue cellValue) throws Exception{
    int type = cellValue.getCellType();
    Object result=null;
    switch (type) {
        case HSSFCell.CELL_TYPE_BOOLEAN:
             result = cellValue.getBooleanValue();
             break;
        case HSSFCell.CELL_TYPE_NUMERIC:
             result = cellValue.getNumberValue();
             break;
        case HSSFCell.CELL_TYPE_STRING:
             result = cellValue.getStringValue();
             break;
        case HSSFCell.CELL_TYPE_BLANK:
             break;
        case HSSFCell.CELL_TYPE_ERROR:
             break;
         // CELL_TYPE_FORMULA will never happen
         case HSSFCell.CELL_TYPE_FORMULA: 
             break;
    }
    return result.toString();
    }
}
@测试
public void SampleCustNumFormat()引发异常{
FileInputStream fi=新的FileInputStream(“C:\\SCE docs\\Automation\\CustomerAccount\u Information.xls”);
HSSF工作手册myWB=新HSSF工作手册(fi);
HSSFSheet mySheet=myWB.getSheetAt(0);
字符串[]myXL=getExcelData(myWB);
//查看从Excel中读取的内容
System.out.println(“循环前”+myXL[1]);

对于(inti=0;iAkokskis是对的。我在你之前的帖子中建议你的是我的错误。我已经更新了那里的代码。或者你可以编写
sheet.getRow(1).createCell(2).setCellValue(myXL[k]);
代替
HSSFCell cell=sheet.getRow(1).createCell(2).setCellValue(myXL[k]);
上述建议消除了无效错误,但我在将数据写入excel时遇到了问题。我遇到非法参数异常:工作表索引(0)超出范围(0..-1)在这里,我正在从现有文件读取数据,并将数据写入另一个现有文件的特定列。请帮助我获得结果。提前感谢。感谢您的建议。上述建议消除了无效错误,但我在将数据写入excel时面临问题。我遇到非法参数异常:工作表索引(0)超出范围(0..-1)这里,我正在从一个现有文件读取数据,并将数据写入另一个现有文件的特定列。请帮助我获得结果。提前感谢。@user1564024-好吧,您实际上并没有创建工作表!而不是
HSSFSheet sheet sheet=myWB.getSheetAt(0);
,请尝试:
HSSFSheet sheet=myWB.createSheet()
。这一行也会出现问题:
的(int k=1;kI有一个ooxml jar解决了这个问题。感谢您的宝贵建议。非常感谢您的帮助。您好,上面的代码将数据写入一个新的excel,而不是现有的excel。请帮助我在现有excel中写入数据。这应该从
CustomerAccount\u Information.xls
读取数据,然后在
测试中写入ExcelData.xls
。这不是您想要的吗?我想从customeraccount\u information.xls读取数据,并将数据写入现有文件(TestExcelData)的特定列中它在另一列中有数据,但当我使用上述方法时,它从customeraccount_information.xls读取数据,并将数据写入testexceldata,该testexceldata是新文件更新的
SampleCustNumFormat()
方法。请检查一次。我使用了workbook factory,在那里我可以将数据正确写入所需的excel。感谢您花时间更正代码。非常感谢您的帮助。