//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 |
时间: 2024-11-10 01:29:54