package poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class GetExcelData { public static List<Map<Integer,Object>> readFile(File file){
// excel中第几列 : 对应的表头
Map<Integer,String> colAndNameMap = new HashMap<>();
List<Map<Integer,Object>> resultList = new ArrayList<>();
FileInputStream fs = null;
XSSFWorkbook wb = null;
try {
fs = new FileInputStream(file);
wb = new XSSFWorkbook(fs);
for(int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++){
//获取sheet数据
XSSFSheet st = wb.getSheetAt(sheetIndex);
//遍历一个sheet中每一行
for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
// 表头:值
Map<Integer,Object> nameAndValMap = new HashMap<>();
// 获取到一行数据
XSSFRow row = st.getRow(rowIndex);
for(int cellIndex = 0; cellIndex < row.getPhysicalNumberOfCells(); cellIndex++){ if(rowIndex==0){
colAndNameMap.put(cellIndex, row.getCell(cellIndex).getStringCellValue());
}else if(!colAndNameMap.isEmpty()){
nameAndValMap.put(cellIndex, buildDate(row.getCell(cellIndex)));
}
}
if(!nameAndValMap.isEmpty()){
resultList.add(nameAndValMap);
}
}
}
return resultList;
} catch (FileNotFoundException e) {
System.out.println(">>>>>>>>>> 读取excel文件时出错了!!!");
e.printStackTrace();
} catch (IOException e) {
System.out.println(">>>>>>>>>> 读取excel文件时出错了!!!");
e.printStackTrace();
} catch (Exception e) {
System.out.println(">>>>>>>>>> 读取excel文件时出错了!!!");
e.printStackTrace();
} finally{
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} // 将excel中时间格式字段进行处理
@SuppressWarnings("deprecation")
public static String buildDate(XSSFCell cell) {
String result = new String();
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_NUMERIC:
if (cell.getCellStyle().getDataFormat() == 176) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 单元格设置成常规
if (temp.equals("General")) {
format.applyPattern("#");
}
result = format.format(value);
}
break;
case XSSFCell.CELL_TYPE_STRING:// String类型
result = cell.getStringCellValue();
break;
default:
result = "";
break;
}
return result;
} }

使用POI读取xlsx文件,包含对excel中自定义时间格式的处理的更多相关文章

  1. java使用poi读取ppt文件和poi读取excel、word示例

    java使用poi读取ppt文件和poi读取excel.word示例 http://www.jb51.net/article/48092.htm

  2. 读取xlsx文件的内容输入到xls文件中

    package com.cn.peitest.excel; import java.io.File; import java.io.FileInputStream; import java.io.Fi ...

  3. 读取xml文件,写入excel

    在上一篇 Python写xml文件已经将所有订单写入xml文件,这一篇我们把xml文件中的内容读出来,写入excel文件. 输入xml格式: <?xml version="1.0&qu ...

  4. Python读取xlsx文件

    Python读取xlsx文件 脚本如下: from openpyxl import load_workbook workbook = load_workbook(u'/tmp/test.xlsx') ...

  5. 人工智能-机器学习之seaborn(读取xlsx文件,小提琴图)

    我们不止可以读取数据库的内容,还可以读取xlsx文件的内容,这个库有在有些情况还是挺实用的 首先我们想读取这个文件的时候必须得现有个seaborn库 下载命令就是: pip install  seab ...

  6. Java处理Excel中的日期格式

    Java处理Excel中的日期格式 2011-12-23 17:34:03|  分类: java |举报 |字号 订阅 下载LOFTER 我的照片书  |   在Excel中的日期格式,其数值为距离1 ...

  7. C#/VB.NET 如何在Excel中使用条件格式设置交替行颜色

    说起高亮数据行,不让人想起了交替颜色行,有的人把交替颜色行也都设置成高亮,不仅不美观,而且对阅读还是个干扰.隔行交替的颜色是为了阅读不串行,这些行只是环境,数据才是主体.那么如何通过C#/VB.NET ...

  8. 【Linux】windows下编写的脚本文件,放到Linux中无法识别格式

    注意:我启动的时候遇到脚本错误 » sh startup.sh -m standalone tanghuang@bogon : command not found : command not foun ...

  9. SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)

    读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...

随机推荐

  1. eleemnt-ui修改主题颜色

    饿了吗的element-ui使用的是淡蓝色的主题,有时候我们可以自定义主题,官方的文档给我们提供了如何修改主题,介绍的很详细,自己试验过后,觉得很不错,一方面怕忘记,一方面写一写. 方法一是在线生成一 ...

  2. 腾讯云主机如何使用root账号登录,不能使用root登录怎么办

    1.先用ubuntu账号登录,执行sudo passwd root 2.按要求输入密码,请牢记. 3.执行sudo vi /etc/ssh/sshd_config 4.找到PermitRootLogi ...

  3. openGL学习----相机

    0.参考:https://learnopengl-cn.github.io/01%20Getting%20started/09%20Camera/ 0.0其实相机就是搞清楚cameraPos,came ...

  4. 用python完成带有进度条的圆周率计算

    代码如下:import math import time scale= s,m,=, print("执行开始".center(scale//2, "-")) s ...

  5. Windows server 2016安装Docker EE

    Windows server 2016安装Docker EE 下载 windows server 2016 180天评估版本. 地址:https://www.microsoft.com/en-us/e ...

  6. JavaScript test() 方法

    JavaScript test() 方法 JavaScript RegExp 对象 定义和用法 test() 方法用于检测一个字符串是否匹配某个模式. 语法 RegExpObject.test(str ...

  7. 【团队】EasyKing的实现_2

    下载开发版 完成情况 完成了碰撞箱的制作 TODO 子弹攻击范围 音效 英雄技能 建筑 双人联机 物品 小兵 地图移动

  8. Learning-Python【28】:基于TCP协议通信的套接字

    什么是 Socket Socket 是应用层与 TCP/IP 协议通信的中间软件抽象层,它是一组接口.在设计模式中,Socket 其实就是一个门面模式,它把复杂的 TCP/IP 协议族隐藏在 Sock ...

  9. C# winform 选择文件保存路径

    1.winform 点击按钮选择文件保存的路径,效果如下图: 具体代码如下: private void button8_Click(object sender, EventArgs e) { Fold ...

  10. 【Python】【自动化测试】【pytest】【常用命令行选项】

    https://www.cnblogs.com/cnkemi/p/9989019.html http://www.cnblogs.com/cnkemi/p/10002788.html pytest 常 ...