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

时间:2013-08-15 19:05:28

标签: java excel apache-poi

我正在尝试从excel文件中读取数据,该文件具有公式并将数据写入另一个excel的特定列,但是使用下面的代码,我收到如下错误消息:无法从void转换为HSSFCell at第68行。

 @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();
 }
            }

2 个答案:

答案 0 :(得分:3)

这些行造成了麻烦:

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

在这里,您将从工作表中获取第一行,在索引2处创建一个单元格,然后设置单元格值...并将所有这些分配给Cell!但是,setCellValue会返回void(如API中所示)。所以基本上,你需要将这些行分成两行:

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

这样做,您将首先创建单元格并将其分配给cell。然后,您可以设置值。

编辑:或者,您可以(正如Sankumarsingh所指出的那样)不分配该值并在一行中完成所有操作:

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

答案 1 :(得分:0)

在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();
    }
}