请求parsing错误不能从void转换为HSSFCell

我想从Excel文件中读取具有公式的数据,并将数据写入另一个Excel的特定列,但通过使用下面的代码,我得到一个错误消息,如:不能转换从void到HSSFCell在第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(); } } 

这些线路造成了麻烦:

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

在这里,你得到表格的第一行,在索引2处创build一个单元格,然后设置单元格值…并将所有这些分配给Cell ! 但是, setCellValue返回一个void (如API所示 )。 所以基本上,你需要把这些线条分成两部分:

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

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

编辑:或者,你可以(正如Sankumarsingh所指出的)只是不分配这个值,并在一行这样做:

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

在Akoksis之后,我必须重新构build你的程序。 请检查它是否适合你

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