使用POI读取xlsx文件,包含对excel中自定义时间格式的处理
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中自定义时间格式的处理的更多相关文章
- java使用poi读取ppt文件和poi读取excel、word示例
java使用poi读取ppt文件和poi读取excel.word示例 http://www.jb51.net/article/48092.htm
- 读取xlsx文件的内容输入到xls文件中
package com.cn.peitest.excel; import java.io.File; import java.io.FileInputStream; import java.io.Fi ...
- 读取xml文件,写入excel
在上一篇 Python写xml文件已经将所有订单写入xml文件,这一篇我们把xml文件中的内容读出来,写入excel文件. 输入xml格式: <?xml version="1.0&qu ...
- Python读取xlsx文件
Python读取xlsx文件 脚本如下: from openpyxl import load_workbook workbook = load_workbook(u'/tmp/test.xlsx') ...
- 人工智能-机器学习之seaborn(读取xlsx文件,小提琴图)
我们不止可以读取数据库的内容,还可以读取xlsx文件的内容,这个库有在有些情况还是挺实用的 首先我们想读取这个文件的时候必须得现有个seaborn库 下载命令就是: pip install seab ...
- Java处理Excel中的日期格式
Java处理Excel中的日期格式 2011-12-23 17:34:03| 分类: java |举报 |字号 订阅 下载LOFTER 我的照片书 | 在Excel中的日期格式,其数值为距离1 ...
- C#/VB.NET 如何在Excel中使用条件格式设置交替行颜色
说起高亮数据行,不让人想起了交替颜色行,有的人把交替颜色行也都设置成高亮,不仅不美观,而且对阅读还是个干扰.隔行交替的颜色是为了阅读不串行,这些行只是环境,数据才是主体.那么如何通过C#/VB.NET ...
- 【Linux】windows下编写的脚本文件,放到Linux中无法识别格式
注意:我启动的时候遇到脚本错误 » sh startup.sh -m standalone tanghuang@bogon : command not found : command not foun ...
- SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)
读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...
随机推荐
- js获取本月最后一天
function getLastDay() { var seperator1 = "-"; var date=new Date; var new_mo ...
- Bootstrap日期插件在线使用,引入即可
引入部分 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script& ...
- python爬虫——跟踪登录过程以及意外的发现(4)
新浪微博的消息还是很多的,值得弄个账号去爬.不过都有账号了,还需要特意再搞一个吗? 直接上去跟踪.分别使用www和wap端登录: wap端相对简单,form表单都没有用到前面传的数据.但是我看到表单时 ...
- 【Python65--tkinter:button】
一.需求:在Label页面增加一个按钮,点击此按钮,页面内容进行变化 思路: 1.上面放一个Label,下面放一个Button 2.采用frame框架 from tkinter import * #定 ...
- 腾讯云centos7.2安装jdk1.7 tomcat7.0部署项目示例
说实话win server的性能并不好,所以程序员必须会在Linux上安装环境,部署项目. 第一步,官网下载tomcat和jdk压缩文件*.tar.gz 下载路径如下: jdk:http://www ...
- HTML基础【3】:列表标签
无序列表 作用:给一堆内容添加无序列表语义(一个没有先后顺序整体),列表中的条目不分先后 格式: li 英文是 list item,翻译为列表项 <h4>选择居住城市(CN)</h4 ...
- [POI1999][LOJ10112]原始生物
典型的有向图K笔画的问题 最后答案就是n+1-1+k 1笔画有一点入度比出度少1 k笔画则统计入度比出度少的点中所有少的总和 #include<bits/stdc++.h> using n ...
- 小程序之 tab切换(选项卡)
好久没有写东西了 今天写一个简单的东西 小程序tab切换 (选项卡功能) .wxml <view class="swiper-tab"> <view < ...
- openssh升级,打补丁
以Root用户上传升级包至/home/dou/system openssh-7.5p1.tar.gz openssl-1.0.2l.tar.gz zlib-1.2.11.tar.gz l 安装所需包 ...
- angular2-4 之动效-animation
提示: angular2 时animation代码在核心模块里面(@angular/core里面);到了angular4.0时animation从核心模块中提取出来作为一个单独的模块, 这样可以在 ...