Read excel and put cell data into HashMap
Posted 木吟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Read excel and put cell data into HashMap相关的知识,希望对你有一定的参考价值。
//Read excel row by row, put cell name and cell value to a map for each row. HashMap getExpectedResult(int rowNum, String filePath, String sheetName){ HashMap<String,String> map = new HashMap<String,String>(); ArrayList<String> cellNames = getCellValues(0,filePath, sheetName); ArrayList<String> cellValues = getCellValues(rowNum,filePath, sheetName); for(int i=0; i<cellNames.size(); i++){ String dataPointName = cellNames.get(i); String dataPointValue = cellValues.get(i); map.put(dataPointName, dataPointValue); } return map; } ArrayList<String> getCellValues(int rowNum, String filePath, String sheetName){ ArrayList<String> cellValues = new ArrayList<String>(); try{ File file = new File(filePath); XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(filePath)); XSSFSheet sheet = wb.getSheet(sheetName); Row row = sheet.getRow(rowNum); String cellValue; if (row != null){ for (int i=8; i<row.getPhysicalNumberOfCells(); i++){ if(rowNum==0){ cellValue = row.getCell(i).getStringCellValue(); }else{
//if the cell value is blank if(row.getCell(i).getCellType()==3){ cellValue = row.getCell(i).getStringCellValue(); }else{ cellValue = formatDecimal(row.getCell(i).getNumericCellValue()); } } cellValues.add(cellValue); } } return cellValues; } catch (Exception e){ e.printStackTrace(); return null; } } String formatDecimal(double value){ DecimalFormat df = new DecimalFormat("#.#####"); return df.format(value); }
accountId | Pref Stock % (Long) | Other % (Long) | Cash % (Short) |
50000642 | 49.127922356 | 14.3256467556 |
以上是关于Read excel and put cell data into HashMap的主要内容,如果未能解决你的问题,请参考以下文章
python xlrd 读取中文内容excel. 读出来一个cell内容是\u6a21\u5757\u540d\u79f0 怎么转中文print出来啊