使用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; ...
随机推荐
- 一个HttpClient使用Windows认证请求WCF服务的例子
有个项目需要调用第三方SDK,而SDK获取服务器的已安装的特殊打印机列表返回给调用方. 但我不想依赖这个SDK,因为这个SDK是使用.NET Framework编写的,而我的项目是使用.NET Cor ...
- Linux下启动Oracle服务和监听程序
$ su – oracle $ sqlplus / nolog sql> conn / as sysdba sql> startup #启动Oracle,需 ...
- Bugku-CTF之变量1
Day9 变量1 http://123.206.87.240:8004/index1.php
- 最短路模板|堆优化Dijkstra,SPFA,floyd
Ⅰ:Dijkstra单源点最短路 1.1Dijkstra const int MAX_N = 10000; const int MAX_M = 100000; const int inf = 0x3f ...
- UVA11235 Frequent values
思路 连续的值只会分布在一起成一个块 讨论两边的块,中间就是RMQ了 ST表即可 代码 #include <cstdio> #include <algorithm> #incl ...
- JDK1.8 新特性
jdk1.8新特性知识点: Lambda表达式 函数式接口 *方法引用和构造器调用 Stream API 接口中的默认方法和静态方法 新时间日期API https://blog.csdn.net/qq ...
- CookieHelper
using System.Web: /// <summary> /// CookieHelper /// </summary> public static class Cook ...
- 最新案例铁血军事手机客户端(IOS & Android)
<铁血军事>Android手机客户端由铁血网开发和运营,为网友提供铁血论坛和铁血读书两大产品.使用Android手机客户端,您不仅可以阅读到最新军事资讯,随时参与精彩话题讨论,还可以在线阅 ...
- Ubuntu 14 如何解压 .zip、.rar 文件
.zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压. [解压.zip文件] Ubuntu中貌似已经安装了unzip软件,解压命令如下: unzip ./FileName ...
- read_csv 的 names 和 index_col 参数作用