java读取excel

直接上代码:

public static Map<String, String> read() throws IOException {
//用流的方式先读取到你想要的excel的文件
FileInputStream fis=new FileInputStream(new File("C:/Users/86159/Desktop/数据.xls"));
//解析excel
POIFSFileSystem pSystem=new POIFSFileSystem(fis);
//获取整个excel
HSSFWorkbook hb=new HSSFWorkbook(pSystem);
System.out.println(hb.getNumCellStyles());
//获取第一个表单sheet
HSSFSheet sheet=hb.getSheetAt(0);
//获取第一行
int firstrow=sheet.getFirstRowNum();
//获取最后一行
int lastrow=sheet.getLastRowNum();
//循环行数依次获取列数
Map<String, String> result=new HashMap<String, String>();
String allorderid="";
for (int i = firstrow; i < lastrow+1; i++) {
//获取哪一行i
Row row=sheet.getRow(i);
if (row!=null) {
//获取这一行的第一列
int firstcell=row.getFirstCellNum();
//获取这一行的最后一列
int lastcell=row.getLastCellNum();
//创建一个集合,用处将每一行的每一列数据都存入集合中
List<String> list=new ArrayList<String>();
for (int j = firstcell; j <lastcell; j++) {
//获取第j列
Cell cell=row.getCell(j);

if (cell!=null) {
System.out.print(cell+"\t");
list.add(cell.toString());
}
}

if(i==0) {
result.put(list.get(0), list.get(1));
allorderid=list.get(0);
}else {
if(result.containsKey(list.get(0))) {
result.put(list.get(0), result.get(list.get(0))+","+list.get(1));
}else {
result.put(list.get(0), list.get(1));
}
if(!allorderid.contains(list.get(0))) {
allorderid=allorderid+","+list.get(0);
}
}
System.out.println("所有orderid"+allorderid);
}
}
fis.close();

result.put("allorderid", allorderid);
return result;
}

posted @ 2020-08-06 08:59  java程序猴  阅读(996)  评论(0编辑  收藏  举报