[英]Request for resolving the error cannot convert from void to HSSFCell
我試圖從具有公式的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();
}
}
這些行引起了麻煩:
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]);
}
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.