讀取導入內容

2019-03-06  本文已影响0人  蚁丶点
public static Map<String,Object> readExcel(String filePath) throws InvalidFormatException, IOException{
    File file = new File(filePath);
    FileInputStream inStream = new FileInputStream(file);
    Workbook workbook = WorkbookFactory.create(inStream);
    Sheet sheet = workbook.getSheetAt(0);
    Row row = sheet.getRow(0);
    Map<String,Object> excel = new HashMap<String,Object>();
    Map<Integer, String[]> content = null;
    if(row==null){
        content = new HashMap();
    }else{
        String[] title = readTitle(workbook);
        excel.put("title", title);
        content = readContent(workbook);
    }
    excel.put("content", content);
    if(file.exists()){
        file.delete();
    }
    return excel;
}
private static String[] readTitle(Workbook workbook){
    
    Sheet sheet = workbook.getSheetAt(0);
    Row row = sheet.getRow(0);
     //得到总列数
    int colNum = row.getPhysicalNumberOfCells();
    
    String[] title = new String[colNum];
    for (int i = 0; i < colNum; i++) {
        title[i] = getCellFormatValue(row.getCell((short) i));
    }
    return title;
}

/**
 * @創建目的:读取Excel数据内容
 * @param filePath
 * @returnMap 包含单元格数据内容的Map对象
 * @throws InvalidFormatException
 * @throws IOException
 * @創建人 H2408236
 * @創建時間 2017年6月17日 下午2:12:57
 * @修改人 
 * @修改時間
 */
private static Map<Integer, String[]> readContent(Workbook workbook){
    Map<Integer, String[]> content = new HashMap<Integer, String[]>();
    Sheet sheet = workbook.getSheetAt(0);
    Row row = sheet.getRow(0);
    //得到总列数
    int colNum = row.getPhysicalNumberOfCells();
    // 得到总行数
    int rowNum = sheet.getLastRowNum();
    // 正文内容应该从第二行开始,第一行为表头的标题
    for (int i = 1; i <= rowNum; i++) {
        row = sheet.getRow(i);
        String[] colValues = new String[colNum];
        for(int j = 0; j< colNum; j ++){
            colValues[j] = getCellFormatValue(row.getCell((short) j)).trim();
        }
        content.put(i, colValues);
    }
    return content;
}


private static String getCellFormatValue(Cell cell){
    String cellvalue = "";
    
    if (cell != null) {
        // 判断当前Cell的Type
        switch (cell.getCellType()) {
        // 如果当前Cell的Type为NUMERIC
        case Cell.CELL_TYPE_NUMERIC:
        case Cell.CELL_TYPE_FORMULA: {
            // 判断当前的cell是否为Date
            if (DateUtil.isCellDateFormatted(cell)) {
                // 如果是Date类型则,转化为Data格式
                Date date = cell.getDateCellValue();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                cellvalue = sdf.format(date);
            } else {
                // 如果是纯数字,取得当前Cell的数值
                cellvalue = String.valueOf(cell.getNumericCellValue());
            }
            break;
        }
        // 如果当前Cell的Type为String
        case Cell.CELL_TYPE_STRING:
            // 取得当前的Cell字符串
            cellvalue = cell.getRichStringCellValue().getString();
            break;
        // 默认的Cell值
        default:
            cellvalue = " ";
        }
    }
    return cellvalue;
}
上一篇下一篇

猜你喜欢

热点阅读