/**
* 资料导入方法
* @param filePath 资料文件路径
* @return 资料信息
*/
public List<Map<String, String>> infomationImport(String filePath) {
//存储 资料集合
List<Map<String, String>> infomationList = new ArrayList<Map<String,String>>();
try{
//创建文件输入流
FileInputStream is= new FileInputStream(new File(filePath));
HSSFWorkbook workbook = new HSSFWorkbook(is); //创建对Excel工作簿文件的引用
/**
* 循环sheet页
*/
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet sheet = workbook.getSheetAt(numSheets); //获得一个sheet
/**
* 循环sheet页所有行
*/
for (int rowNumOfSheet = 1; rowNumOfSheet <= sheet.getLastRowNum(); rowNumOfSheet++) {
//存储每一行的资料
Map<String,String> map = new HashMap<String,String>();
if (null != sheet.getRow(rowNumOfSheet)) {
//获得一行
HSSFRow row = sheet.getRow(rowNumOfSheet);
/**
* 循环每一行的所有列
*/
for (short cellNumOfRow = 0; cellNumOfRow <= row.getLastCellNum(); cellNumOfRow++) {
if (null != row.getCell(cellNumOfRow)) {
//获得列值
HSSFCell cell = row.getCell(cellNumOfRow);
//数字类型
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
NumberFormat fomatter = new DecimalFormat();
String[] dataList=fomatter.format(Double.parseDouble(String.valueOf(cell.getNumericCellValue()))).split(Constants.CONSTANT_DATA_SEPARATOR);
StringBuffer sb=new StringBuffer();
for (int i = 0; i < dataList.length; i++) {
sb.append(dataList[i]);
}
map.put(sheet.getRow(0).getCell(cellNumOfRow).getStringCellValue(), sb.toString());
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
map.put(sheet.getRow(0).getCell(cellNumOfRow).getStringCellValue(), String.valueOf(cell.getBooleanCellValue()));
}else {
map.put(sheet.getRow(0).getCell(cellNumOfRow).getStringCellValue(),cell.getStringCellValue());
}
}
}
}
infomationList.add(map);
}
}
}
}catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException ioexception){
ioexception.printStackTrace();
}
return infomationList;
}
java poi excel 导入
最新推荐文章于 2024-07-25 22:42:19 发布