
注意事项:同样写入到Excel的时间格式为------> yyyy/m/d h:mm:ss ,且单元格宽度不够的的情况的下:
显示方式1:
显示方式2:
原因分析:“显示方式1”的写入格式是String类型,cellsetCellValue(date),其中date为String类型;
“显示方式2”的写入格式为Date类型,cellsetCellValue(date),其中date为Date类型;
解决办法:
1如果想要时间格式不被压缩成成“###”,可以将我们获取到的时间转换成String类型,
SimpleDateFormat formatter= new SimpleDateFormat("yyyy/M/d H:mm:ss");
Date date= new Date(SystemcurrentTimeMillis());
String time= formatterformat(date);
2如果想要单元格格式为时间,且在单元格宽度不够的情况下显示成“###” ,
CreationHelper createHelper=workbookgetCreationHelper();
CellStyle style= workbookcreateCellStyle();
DataFormat format= workbookcreateDataFormat();
stylesetDataFormat(createHelpercreateDataFormat()getFormat("yyyy/M/d H:mm:ss"));
cellsetCellValue(new Date());
cellsetCellStyle(style);
即可!
备注:将时间字符串timeString转换成Date格式方法:
Date date=null;
SimpleDateFormat formatter=new SimpleDateFormat("yyyy/M/d H:mm:ss");
date=formatterparse(timeString);
以上。
首先
1你要明白poi是java 的第三方类库,用于导出excel,word 等等
2 datepicker 是一个用于html页面的的js日期控件
datepicker 的api
>
方法/步骤
一, ExcelUtils 工具类(也就是解析EXCEL文件,判断EXCEL的类型以及数据的类型)
import javaioIOException;
import javaioInputStream;
import javamathBigDecimal;
import javatextSimpleDateFormat;
import javautilArrayList;
import javautilDate;
import javautilList;
import orgapachepoihssfusermodelHSSFDateUtil;
import orgapachepoihssfusermodelHSSFWorkbook;
import orgapachepoissusermodelCell;
import orgapachepoissusermodelRow;
import orgapachepoissusermodelSheet;
import orgapachepoissusermodelWorkbook;
import orgapachepoixssfusermodelXSSFWorkbook;
public class ExcelUtils {
private final static String excel2003L = “xls” ; // 2003-版本的excel
private final static String excel2007U = “xlsx” ; // 2007 +版本的excel
/
描述:获取IO流中的数据,组装成List <List <Object>对象
@param in,fileName
@返回
@throws IOException
/
public List <List <Object> getBankListByExcel(InputStream in,String fileName) throws Exception {
列表<List <Object> list = null ;
//创建的Excel工作薄
Workbook work = this getWorkbook(in,fileName);
if (null == work){
抛出新的 异常(“创建Excel工作薄为空!” );
}
Sheet sheet = null ; //页数
行row = null ; //行数
Cell cell = null ; //列数
list = new ArrayList <List <Object >>();
//遍历的Excel中所有的片
for (int i = 0 ; i <workgetNumberOfSheets(); i ++){
sheet = workgetSheetAt(i);
if (sheet == null ){ continue ;}
//遍历当前片中的所有行
for (int j = sheetgetFirstRowNum(); j <= sheetgetLastRowNum(); j ++){
row = sheetgetRow(j);
if (row == null || rowgetFirstCellNum()== j){ continue ;}
//遍历所有的列
列表<Object> li = new ArrayList <Object>();
for (int y = rowgetFirstCellNum(); y <rowgetLastCellNum(); y ++){
cell = rowgetCell(y);
liadd(this getValue(cell));
}
listadd(LI);
}
}
return 单
}
/
描述:根据文件后缀,自适应上传文件的版本
@param inStr,fileName
@返回
@throws异常
/
public Workbook getWorkbook(InputStream inStr,String fileName) throws Exception {
工作簿wb = null ;
String fileType = fileNamesubstring(fileNamelastIndexOf(“。” ));
if (excel2003Lequals(fileType)){
wb = new HSSFWorkbook(inStr); // 2003-
} else if (excel2007Uequals(fileType)){
wb = new XSSFWorkbook(inStr); // 2007 +
} else {
抛出新的 异常(“解析的文件格式有误!” );
}
返回 wb;
}
/
描述:对表格中数值进行格式化
@param单元格
@返回
/
//解决擅长类型问题,获得数值
public String getValue(Cell cell){
String value = “” ;
if (null == cell){
返回 值
}
switch (cellgetCellType()){
//数值型
案例 CellCELL_TYPE_NUMERIC:
if (HSSFDateUtilisCellDateFormatted(cell)){
//如果是date类型则,获取该单元格的日期值
Date date = HSSFDateUtilgetJavaDate(cellgetNumericCellValue());
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd” );
value = formatformat(date);;
} else { //纯数字
BigDecimal big = new BigDecimal(cellgetNumericCellValue());
value = bigtoString();
//解决12340去掉后面的0
if (null != value &&!“” equals(valuetrim())){
String [] item = valuesplit(“[。]” );
if (1 <itemlength && “0” equals(item [ 1 ])){
value = item [ 0 ];
}
}
}
break;
//字符串类型
案例 CellCELL_TYPE_STRING:
value = cellgetStringCellValue()。toString();
break;
//公式类型
案例 CellCELL_TYPE_FORMULA:
//读公式计算值
value = StringvalueOf(cellgetNumericCellValue());
if (valueequals(“NaN” )){ //如果获取的数据值为非法值,则转换为获取字符串
value = cellgetStringCellValue()。toString();
}
break;
//布尔类型
案例 CellCELL_TYPE_BOOLEAN:
value = “” + cellgetBooleanCellValue();
break;
默认值:
value = cellgetStringCellValue()。toString();
}
if (“null” endsWith(valuetrim())){
value = “” ;
}
返回 值
}
}
二,定义两个实体类,一个是对于的Excel文件,解析它的数据(ExcelBean),另一个是导入数据库表的实体类(人)
ExcelBeanjava
<strong> <span style = “font-size:18px;” > import orgapachepoixssfusermodelXSSFCellStyle;
公共类 ExcelBean 实现 javaioSerializable {
private String headTextName; //列头(标题)名
private String propertyName; //对应字段名
私有 整数列; //合并单元格数
私人 XSSFCellStyle cellStyle;
public ExcelBean(){
}
public ExcelBean(String headTextName,String propertyName){
这个headTextName = headTextName;
这个propertyName = propertyName;
}
public ExcelBean(String headTextName,String propertyName,Integer cols){
super ();
这个headTextName = headTextName;
这个propertyName = propertyName;
这个cols = cols;
}
public String getHeadTextName(){
return headTextName;
}
public void setHeadTextName(String headTextName){
这个headTextName = headTextName;
}
public String getPropertyName(){
return propertyName;
}
public void setPropertyName(String propertyName){
这个propertyName = propertyName;
}
public Integer getCols(){
返回列 ;
}
公共无效 setCols(Integer cols){
这个cols = cols;
}
上市 XSSFCellStyle getCellStyle(){
返回 cellStyle;
}
公共无效 setCellStyle(XSSFCellStyle cellStyle){
这个 cellStyle = cellStyle;
}
} </ span> </ strong>
peoplejava
import javautilDate;
公共课 人
私有 整数id
private String userName;
私人 字符串密码;
私人 整数年龄;
私人 日期;
public Integer getId(){
返回 id
}
public void setId(Integer id){
这个id = id;
}
public String getUserName(){
return userName;
}
public void setUserName(String userName){
这个userName = userName == null ? null :userNametrim();
}
public String getPassword(){
返回 密码
}
public void setPassword(String password){
这个password = password == null ? null :passwordtrim();
}
public Integer getAge(){
回归 年龄
}
public void setAge(Integer age){
这个age = age
}
public Date getDate(){
退货 日期
}
public void setDate(Date date){
这个date = date
}
}
不是。如果pola买的是日本原产的话,生产日期是看不出来的。pola这类日本的化妆品生产商是不会再批号上打上生产日期,除了fancl的一部分产品日本有生产日期外,其他的品牌都没有。因为日本市场的回收机制很完善,不存在过期销售产品,洁面和护肤用品2年内不能卖出,则全部清架。
java读取excel时间格式出现数字的处理方法:
Excel存储日期、时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化
1、数值格式(CELL_TYPE_NUMERIC):
1纯数值格式:getNumericCellValue() 直接获取数据
2日期格式:处理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式
1)判断是否是日期格式:HSSFDateUtilisCellDateFormatted(cell)
2)判断是日期或者时间
cellgetCellStyle()getDataFormat() == HSSFDataFormatgetBuiltinFormat("h:mm")
OR: cellgetCellStyle()getDataFormat() == HSSFDataFormatgetBuiltinFormat("yyyy-MM-dd")
3自定义日期格式:处理yyyy年m月d日,h时mm分,yyyy年m月等含文字的日期格式
判断cellgetCellStyle()getDataFormat()值,解析数值格式
yyyy年m月d日----->31
m月d日---->58
h时mm分--->32
举例说明:
private String parseExcel(Cell cell) {
String result = new String();
switch (cellgetCellType()) {
case HSSFCellCELL_TYPE_NUMERIC:// 数字类型
if (HSSFDateUtilisCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cellgetCellStyle()getDataFormat() == HSSFDataFormat
getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cellgetDateCellValue();
result = sdfformat(date);
} else if (cellgetCellStyle()getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cellgetNumericCellValue();
Date date = orgapachepoissusermodelDateUtil
getJavaDate(value);
result = sdfformat(date);
} else {
double value = cellgetNumericCellValue();
CellStyle style = cellgetCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = stylegetDataFormatString();
// 单元格设置成常规
if (tempequals("General")) {
formatapplyPattern("#");
}
result = formatformat(value);
}
break;
case HSSFCellCELL_TYPE_STRING:// String类型
result = cellgetRichStringCellValue()toString();
break;
case HSSFCellCELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result;
}
以上就是关于POI采坑系列:POI 导入导出Excel时,需要注意的时间格式####全部的内容,包括:POI采坑系列:POI 导入导出Excel时,需要注意的时间格式####、poi 支持日期控件吗 就是datepicker 怎么调用设置单元格d出日期选择器、poi导入excel等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)