`

java读取excel文件并读取数据到map

    博客分类:
  • java
阅读更多
 
public Map<Integer, String[]> toMap(String path) {
Map<Integer,String[]> map = null;
try {
Workbook wb = new HSSFWorkbook(new FileInputStream(new File(path)));
//对excel文件的处理
Sheet st = wb.getSheetAt(0);
if(st != null){
map = new HashMap<Integer, String[]>(st.getLastRowNum());

for(int i = 0;i < st.getLastRowNum();i++){
//行
Row row = st.getRow(i);
if(row != null){
int rows = row.getLastCellNum();
String[] strs = null;
if(rows > 0){
strs = new String[rows];
for(int j = 0;j < rows;j++){
//列
Cell c = row.getCell(j);
strs[j] = c.toString();

if(c.getCellType() == Cell.CELL_TYPE_STRING){
strs[j] = c.toString();
}else if(c.getCellType() == Cell.CELL_TYPE_NUMERIC){
double d = c.getNumericCellValue();

String s = String.valueOf(d);
s = s.substring(0, s.indexOf("E")).replace(".", "");
strs[j] = s;
}
}
}
map.put(i, strs);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("文件未找到.." + e.getClass().getName() + "\t 信息:" + e.getMessage());
} catch (IOException e) {
System.out.println("Io异常:" + e.getClass().getName() + "\t 信息:" + e.getMessage());
}
return map;
}

 ps:使用poi工具进行解析的

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics