问题描述
我创造了这个code阅读的使用Apache POI Excel文件的内容。我使用Eclipse作为编辑,但是当我跑了code我有,我有大胆的线路问题。有什么问题?
Excel中的内容如下:
的Emp ID名称薪水 1.0约翰2000000.0 2.0院长4200000.0 3.0 SAM 2800000.0 4.0卡斯600000.0
进口java.io. *;
进口的java.util.ArrayList;
进口java.util.Iterator的;
进口的java.util.List;
进口org.apache.poi.hssf.usermodel.HSSFCell;
进口org.apache.poi.hssf.usermodel.HSSFRow;
进口org.apache.poi.hssf.usermodel.HSSFSheet;
进口org.apache.poi.hssf.usermodel.HSSFWorkbook;
进口org.apache.poi.poifs.filesystem.POIFSFileSystem;公共类ExcelRead {公共静态无效的主要(字串[] args)抛出异常{
文件Excel文件=新的文件(C:\\\\ \\\\用户\\\\电喷文件\\\\ TEST.XLS);
FIS的FileInputStream =新的FileInputStream(EXCEL); HSSFWorkbook WB =新HSSFWorkbook(FIS);
HSSFSheet WS = wb.getSheet(输入); INT的rowNum = ws.getLastRowNum()+ 1;
INT colNum = ws.getRow(0).getLastCellNum();
的String [] []数据=新的String [的rowNum] [colNum];
的for(int i = 0; I<的rowNum,我++){
HSSFRow行= ws.getRow(I)
对于(INT J = 0; J< colNum; J ++){
HSSFCell细胞= row.getCell(J);
字符串值= cellToString(单元);
数据[I] [J] =价值;
的System.out.println(的值是+值); }
}
}公共静态字符串cellToString(HSSFCell细胞){int型的;
对象的结果;
键入= cell.getCellType(); 开关(类型){
案件0://在Excel中的数值
结果= cell.getNumericCellValue();
打破;
案例1://在Excel中的字符串值
结果= cell.getStringCellValue();
打破;
案例2:在Excel //布尔值
结果= cell.getBooleanCellValue();
打破;
默认:
***抛出新的RuntimeException(没有这种类型的支持
细胞);***
}返回result.toString();
}}
有的的你在你的开关
语句中捕获的。您有案件 0
( CELL_TYPE_NUMERIC
), 1
( CELL_TYPE_STRING
)和 2
,但 2
是 CELL_TYPE_FORMULA
。下面是更多的可能值:
- 3:
CELL_TYPE_BLANK
- 4
CELL_TYPE_BOOLEAN
- 5
CELL_TYPE_ERROR
使用细胞
常数在switch语句,而不是整数常量的细胞类型,并使用所有6人来捕获所有可能的情况。
和作为@Vash已经建议,包括实际的电池键入
在的RuntimeException
消息。
I have created this code to read the contents of excel files using Apache POI. I am using eclipse as editor but when i ran the code i have problem in the line that I have in bold. What's the problem?The content of excel is the following:
Emp ID Name Salary
1.0 john 2000000.0
2.0 dean 4200000.0
3.0 sam 2800000.0
4.0 cass 600000.0
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExcelRead {
public static void main(String[] args) throws Exception {
File excel = new File ("C:\\Users\\Efi\\Documents\\test.xls");
FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheet("Input");
int rowNum = ws.getLastRowNum()+1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum];
for (int i=0; i<rowNum; i++){
HSSFRow row = ws.getRow(i);
for (int j=0; j<colNum; j++){
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
System.out.println("The value is" + value);
}
}
}
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;
case 2: //boolean value in excel
result = cell.getBooleanCellValue ();
break;
default:
***throw new RunTimeException("There are not support for this type of
cell");***
}
return result.toString();
}
}
There are additional cell types besides the ones you are capturing in your switch
statement. You have cases for 0
(CELL_TYPE_NUMERIC
), 1
(CELL_TYPE_STRING
), and 2
, but 2
is CELL_TYPE_FORMULA
. Here are the additional possible values:
- 3:
CELL_TYPE_BLANK
- 4:
CELL_TYPE_BOOLEAN
- 5:
CELL_TYPE_ERROR
Use the Cell
constants for the cell type in your switch statement instead of integer literals, and use all 6 of them to capture all possible cases.
And as @Vash has already suggested, include the actual cell type
in your RuntimeException
message.
这篇关于阅读使用Apache POI excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!