我想从 excel 文件的一个选项卡中读取数据,并使用 java 写入同一个 excel 文件中另一个选项卡的特定列。请您帮我提供一些解决方案。提前致谢。
我的代码看起来像:
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;
xCols = mySheet.getRow(0).getLastCellNum();
tabArray = new String [xRows][xCols];
for (int i=0;i<xRows;i++)
{
HSSFRow row = mySheet.getRow(i);
for (int j=3;j<xCols;j++)
{
HSSFCell cell = row.getCell(j);
CellValue cellValue = evaluator.evaluate(cell);
String value = evaluateFormula(cellValue);
tabArray[i][j]=value;
}
}
return tabArray;
}
private String evaluateFormula(CellValue cellValue) {
// TODO Auto-generated method stub
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();
}
}
我得到的输出是
Cust Num null
Cust Num CustAccNum
Cust Num null
Cust Num 2-23-456-7891
Cust Num null
Cust Num 2-00-006-7891
Cust Num null
Cust Num 2-03-456-7891
Cust Num null
Cust Num 2-00-234-5678
Cust Num null
Cust Num 2-00-023-4891
Cust Num null
Cust Num 2-00-234-7891
Cust Num null
Cust Num 2-00-345-6781
需要帮助:我不希望显示已为对象结果初始化的空值。你能提供一个工作区吗?