码迷,mamicode.com
首页 > 其他好文 > 详细

Read excel and put cell data into HashMap

时间:2016-05-23 18:59:11      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

//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

标签:

原文地址:http://www.cnblogs.com/testing-life/p/5520766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!