类型转换错误 Java,Apache POI
Type casting error Java, Apache POI
我是 Java 的初级程序员,这是我到目前为止编写的代码:
public class Fastclass {
public static void main (String[] args) throws Exception{
String[] data;
data = excelRead();
for (int i = 1; i < data.length; i++){
findcontact(data[i]);
}
}
public static String[] excelRead() throws Exception{
File excel = new File ("C:\Users\user\Desktop\Folder\subfolder\Data.xlsx");
FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook (fis);
HSSFSheet ws = wb.getSheet("Sheet1");
int rowNum = ws.getLastRowNum() + 1;
String[] data = new String [rowNum];
for (int i=1; i<rowNum; i++){
HSSFRow row = ws.getRow(i);
HSSFCell cell = row.getCell(i);
String value = cellToString(cell);
data[i] = value;
}
**return data[i];**
}
public static String cellToString (HSSFCell cell) {
int type;
Object result;
type = cell.getCellType();
switch(type){
case 0: // numeric value in excel
result = cell.getNumericCellValue();
break;
case 1: //String value in excel
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("There are no support for this type of cell");
}
return result.toString();
}
}
函数excelRead用于读取excel文件,函数cellToString用于转换excel中不同数据类型的单元格。
从主 class 调用的 findcontact 函数是我需要实现的实际逻辑。但是,从这个问题的角度来看,这并不重要。
我在 excel 读取函数的 return 语句中遇到错误,错误是无法将字符串转换为字符串 [](字符串数组)。任何帮助表示赞赏。非常感谢。
您的函数 excelRead
应该 return a String[]
(因为您将结果分配给 data
,a String[]
)。即
return data[i]; // <-- a String
应该是
return data; // <-- a String[]
阅读代码有点困难(可能是格式化)但是如果 return data[i]; 是你的 return 声明那么您只是 returning 数组的一个元素,它是一个字符串。
只需使用 "return data;" 即可
您的方法签名:
public static String[] excelRead() throws Exception
说您想要 return 一个字符串数组,但您正在 return 从您的数组中获取一个元素(字符串)。
也许您想 return 整个数组:
return data;
或将您的方法签名更改为:
public static String excelRead() throws Exception
和return String 数组元素就像您正在尝试做的那样?
根据下面的方法签名你应该return字符串数组
public static String[] excelRead() throws Exception
你在哪里return输入一个字符串
**return data[i];**
要么更改您的方法签名,使 return 类型为字符串,要么 return 字符串 []
我是 Java 的初级程序员,这是我到目前为止编写的代码:
public class Fastclass {
public static void main (String[] args) throws Exception{
String[] data;
data = excelRead();
for (int i = 1; i < data.length; i++){
findcontact(data[i]);
}
}
public static String[] excelRead() throws Exception{
File excel = new File ("C:\Users\user\Desktop\Folder\subfolder\Data.xlsx");
FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook (fis);
HSSFSheet ws = wb.getSheet("Sheet1");
int rowNum = ws.getLastRowNum() + 1;
String[] data = new String [rowNum];
for (int i=1; i<rowNum; i++){
HSSFRow row = ws.getRow(i);
HSSFCell cell = row.getCell(i);
String value = cellToString(cell);
data[i] = value;
}
**return data[i];**
}
public static String cellToString (HSSFCell cell) {
int type;
Object result;
type = cell.getCellType();
switch(type){
case 0: // numeric value in excel
result = cell.getNumericCellValue();
break;
case 1: //String value in excel
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("There are no support for this type of cell");
}
return result.toString();
}
}
函数excelRead用于读取excel文件,函数cellToString用于转换excel中不同数据类型的单元格。
从主 class 调用的 findcontact 函数是我需要实现的实际逻辑。但是,从这个问题的角度来看,这并不重要。
我在 excel 读取函数的 return 语句中遇到错误,错误是无法将字符串转换为字符串 [](字符串数组)。任何帮助表示赞赏。非常感谢。
您的函数 excelRead
应该 return a String[]
(因为您将结果分配给 data
,a String[]
)。即
return data[i]; // <-- a String
应该是
return data; // <-- a String[]
阅读代码有点困难(可能是格式化)但是如果 return data[i]; 是你的 return 声明那么您只是 returning 数组的一个元素,它是一个字符串。
只需使用 "return data;" 即可
您的方法签名:
public static String[] excelRead() throws Exception
说您想要 return 一个字符串数组,但您正在 return 从您的数组中获取一个元素(字符串)。
也许您想 return 整个数组:
return data;
或将您的方法签名更改为:
public static String excelRead() throws Exception
和return String 数组元素就像您正在尝试做的那样?
根据下面的方法签名你应该return字符串数组
public static String[] excelRead() throws Exception
你在哪里return输入一个字符串
**return data[i];**
要么更改您的方法签名,使 return 类型为字符串,要么 return 字符串 []