乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > java <em>poi读取excel</em>日期格式数据-poi 读取excel 时间,po

java <em>poi读取excel</em>日期格式数据-poi 读取excel 时间,po

作者:乔山办公网日期:

返回目录:excel表格制作


import org.apache.poi.xssf.usermodel.*; //import needed

XSSFWorkbook wb = new XSSFWorkbook (); // Create workbook
XSSFSheet sheet = wb.createSheet(); // Create spreadsheet in workbook
XSSFRow row = sheet.createRow(rowIndex); // Create the row in the spreadsheet
//1. Create the date cell style
XSSFCreationHelper createHelper = wb.getCreationHelper();
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("MMMM dd, yyyy"));

//2. Apply the Date cell style to a cell

//This example sets the first cell in the row using the date cell style
cell = row.createCell(0);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);

public String getCell(HSSFCell cell) {
7a686964616fe78988e69d83330DecimalFormat df = new DecimalFormat("#");
if (cell == null)
return "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
}
return df.format(cell.getNumericCellValue());
case HSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
return cell.getStringCellValue();
case HSSFCell.CELL_TYPE_FORMULA:
return cell.getCellFormula();
case HSSFCell.CELL_TYPE_BLANK:
return "";
case HSSFCell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue() + "";
case HSSFCell.CELL_TYPE_ERROR:
return cell.getErrorCellValue() + "";
}
return "";
}

试试这个、
数值.toString()就变成字符串了嘛。

Excel 是用数字存贮日期,所以会出现这样的错误,你要先在Excel里面把日期转换成文本格式,用这个公式

=text(a1,"yyyy-mm-dd")

A1就是你原来的存放地点,转换后,复制,特殊粘贴,粘贴值到原位置,删除不需要的列,再导入试试

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网