java读取excel时间格式出现数字怎么处理

如题所述

在Excel中的日期格式,比如2009-12-24将其转化为数字格式时变成了40171,在用java处理的时候,读取的也将是40171。如果使用POI处理Excel中的日期类型的单元格时,如果仅仅是判断它是否为日期类型的话,最终会以NUMERIC类型来处理。

正确的处理方法是先判断单元格的类型是否则NUMERIC类型,然后再判断单元格是否为日期格式,如果是的话,创建一个日期格式,再将单元格的内容以这个日期格式显示出来。如果单元格不是日期格式,那么则直接得到NUMERIC的值就行了。

具体代码如下:

主要是判断NUMERIC 的时候 同事判断下 单元格是不是日期格式 如果是 日期格式直接 转成日期格式字符串返回值就ok了。

if (0 == cell.getCellType()) {    //判断是否为日期类型   if(HSSFDateUtil.isCellDateFormatted(cell)){   //用于转化为日期格式  Date d = cell.getDateCellValue();   DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   str[k] = formater.format(d);  }else{   // 用于格式化数字,只保留数字的整数部分   DecimalFormat df = new DecimalFormat("########");   str[k] = df.format(cell.getNumericCellValue());   }

JSch连接SSH问题Exception:Algorithm negotiation fail

使用Jenkins配置完远程SSH服务器,发生无法连接问题,查看Log后找到:

[SSH] Exception:Algorithm negotiation fail
com.jcraft.jsch.JSchException: Algorithm negotiation fail
at com.jcraft.jsch.Session.receive_kexinit(Session.java:520)
at com.jcraft.jsch.Session.connect(Session.java:286)
at com.jcraft.jsch.Session.connect(Session.java:150)
at org.jvnet.hudson.plugins.SSHSite.createSession(SSHSite.java:141)
at org.jvnet.hudson.plugins.SSHSite.executeCommand(SSHSite.java:151)
at org.jvnet.hudson.plugins.SSHBuildWrapper.executePreBuildScript(SSHBuildWrapper.java:75)
at org.jvnet.hudson.plugins.SSHBuildWrapper.setUp(SSHBuildWrapper.java:59)
at hudson.model.Build$BuildExecution.doRun(Build.java:154)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:533)
at hudson.model.Run.execute(Run.java:1754)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:89)
at hudson.model.Executor.run(Executor.java:240)
Finished: FAILURE

原因分析:OpenSSH和 JSch支持的交换算法不同,需要一方打开另一方支持的交换算法。

OpenSSH enables only the following key exchange algorithms by default:
- [email protected]
- ecdh-sha2-nistp256
- ecdh-sha2-nistp384
- ecdh-sha2-nistp521
- diffie-hellman-group-exchange-sha256
- diffie-hellman-group14-sha1

Where as JSch claims to support these algorithms for key exchange:
- diffie-hellman-group-exchange-sha1
- diffie-hellman-group1-sha1

解决办法:

在SSH的配置文件/etc/ssh/sshd_config增加以下两行,让SSH支持相应的算法和MACs。

KexAlgorithms [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1

MACs [email protected],[email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,hmac-ripemd160,[email protected],hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96

完成后重启SSH即可解决问题。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-06-28

正确的处理方法是先判断单元格的类型是否则NUMERIC类型,判断单元格是否为日期格式,如果是的话,创建一个日期格式,将单元格的内容以这个日期格式显示出来。如果单元格不是日期格式,那么则直接得到NUMERIC的值就行了。

在Excel中的日期格式,比如2009-12-24将其转化为数字格式时变成了40171,在用java处理的时候,读取的也将是40171。如果使用POI处理Excel中的日期类型的单元格时,如果仅仅是判断它是否为日期类型的话,最终会以NUMERIC类型来处理。

三维引用样式

如果要分析同一工作簿中多张工作表上的相同单元格或单元格区域中的数据,就要用到三维引用。三维引用包含单元格或区域引用,前面加上工作表名称的范围。Excel 使用存储在引用开始名和结束名之间的任何工作表。例如,=SUM(Sheet2:Sheet13!B5) 将计算包含在 B5 单元格内所有值的和,单元格取值范围是从工作表2 到工作表 13。

以上内容参考:百度百科-单元格

本回答被网友采纳
第2个回答  2016-06-16
java读取excel时间格式出现数字的处理方法:
Excel存储日期、时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化
1、数值格式(CELL_TYPE_NUMERIC):
1.纯数值格式:getNumericCellValue() 直接获取数据
2.日期格式:处理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式
1).判断是否是日期格式:HSSFDateUtil.isCellDateFormatted(cell)
2).判断是日期或者时间
cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")
OR: cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd")
3.自定义日期格式:处理yyyy年m月d日,h时mm分,yyyy年m月等含文字的日期格式
判断cell.getCellStyle().getDataFormat()值,解析数值格式
yyyy年m月d日----->31
m月d日---->58
h时mm分--->32
举例说明:
private String parseExcel(Cell cell) {
String result = new String();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
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 HSSFCell.CELL_TYPE_STRING:// String类型
result = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result;
}本回答被网友采纳
第3个回答  推荐于2017-11-22
那就全部按照字符串读取,然后用Integer.parseInt()方法和new SimpleDateFormat().format()方法操作获得的字符串,没报异常就是对应的数字或时间,全报异常就是字符串本回答被提问者采纳
第4个回答  2016-05-24
那就全部按照字符串读取,然后用Integer.parseInt()方法和new SimpleDateFormat().format()方法操作获得的字符串,没报异常就是对应的数字或时间,全报异常就是字符串
相似回答