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

Read excel and put cell data into HashMap的相关文章

In excel copy the cell that contain the special text to the new collom

Sub copy_specail_text() 'Dim rl As Integer 'Dim wl As Integer SumRows = Sheet1.UsedRange.Rows.Count MsgBox "sumrows is :" & SumRows wl = 1 For rl = 1 To SumRows If InStr(1, Sheet1.Cells(rl, 1), "ifOutOctets.3 =") Then Sheet1.Cells(

对excel的列cell的一些样式设置

设置样式: HSSFCellStyle cellStyle = wb.createCellStyle();cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//指定单元格居中对齐cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//指定单元格垂直居中对齐cellStyle.setWrapText(true);//指定单元格自动换行 设置字体: HSSFFont font

NetSuite SuiteScript 2.0 export data to Excel file(xls)

In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collect 'encoded' string to Array for column, join them with comma ',' to be a string. Collect each line's data same as column to push to the Array. Join al

Excel Outlines Grouped Data

Outlines Grouped Data Before After Preparing the Workbook In order to make this feature easy to use, you must do two things to your excel set up. (1) Put the group and ungroup buttons into your quick launch area. In MS Excel 2007-2013 open the Data t

POI操作Excel异常Cannot get a text value from a numeric cell

控制台抛出异常:java.lang.IllegalStateException: Cannot get a text value from a numeric cell 在java中用POI解析excel文件时出现以上报错,表示无法从一个数值类型的单元格获得文本类型的值. POI操作Excel时数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串并写入数据库时,就会出现Cannot get a text value from a numeric cell的异常错误. 解决

测试过程中的Excel数据驱动(java实现)

import java.io.FileInputStream;import java.io.InputStream;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.testng.Assert;import jxl.*; /** * Excel放在Data文件夹下 Excel命名方式:测试类名.xls Excel的sheet命名方式:测试方法名 Excel第一行为Map的key *

poi导出Excel报表多表头双层表头、合并单元格

效果图: controller层方法: /**     *      * 导出Excel报表     * @param request     * @return     *      */    @RequestMapping("/export")    @ResponseBody    public void export(HttpServletRequest request,            HttpServletResponse response, String year

实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件

今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多都是以json数据填充的.在导入excel填充json数据到页面时,真的让我差点吐血了.在做这个导入导出的时候,那一个礼拜都是黑暗的. 好了,废话不多说了,我今天就给大家展示这个两个功能的核心点,excel生成json数据和json数据生成excel文件. 一:从上传文件到服务器,后台java解析,

[Java]Create Excel File Using Apache POI API

package com.file.properties; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Has