POI 读取excel文档中输入日期格式为字符串

通过cell.getCellStyle().getDataFormat();根据这个值进行时间、日期格式的判断;

时间格式

为了获取用户输入什么格式就显示什么格式,做以下整理:

 

通过对format值的判断调用格式化日期类型值。如下:

 int dformat = hssfCell.getCellStyle().getDataFormat();  
	    		    SimpleDateFormat sdf = null;  
	    		    if(Arrays.asList(14,178,192,194,208,196,210).contains(dformat)) {
	    		    		sdf=new SimpleDateFormat("yyyy-MM-dd");
	    		    }else if(Arrays.asList(190,191).contains(dformat)) {
	    		    	sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
	    		    }else if(Arrays.asList(177,182,185).contains(dformat)) {
	    		      	sdf=new SimpleDateFormat("yyyy年MM月dd日");
	    		    }else if(Arrays.asList(183,186).contains(dformat)) {
	    		    	sdf=new SimpleDateFormat("yyyy年MM月");
	    		    }else if(Arrays.asList(183,200,201,202,203).contains(dformat)) {
	    		      	sdf=new SimpleDateFormat("HH:mm");
	    		    }else if(Arrays.asList(204,205,206,207,208).contains(dformat)) {
	    		      	sdf=new SimpleDateFormat("HH时mm分");
	    		    }else if(Arrays.asList(184,187).contains(dformat)) {
	    		     	sdf=new SimpleDateFormat("MM月dd日");
	    		    }else {
	    		    	sdf=new SimpleDateFormat("yyyy-MM-dd");
	    		    }
	    		return sdf.format(hssfCell.getDateCellValue());

 

读取 Excel 日期,可以使用 POI 的 DataFormatter 类将其换为字符串。下面是一个示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DateUtil; 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 ReadExcel { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("sample.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet sheet = workbook.getSheetAt(0); // Create a DataFormatter object to format the cell values DataFormatter formatter = new DataFormatter(); // Loop through each row and column for (int i = 0; i <= sheet.getLastRowNum(); i++) { XSSFRow row = sheet.getRow(i); if (row != null) { for (int j = 0; j < row.getLastCellNum(); j++) { XSSFCell cell = row.getCell(j); if (cell != null) { String cellValue = formatter.formatCellValue(cell); if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); cellValue = dateFormat.format(date); } System.out.print(cellValue + "\t"); } else { System.out.print("\t"); } } System.out.println(); } } workbook.close(); file.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码,我们首先读取 Excel 文件并获取第一个工作表。然后,我们使用 DataFormatter 类来格式化单元格的值。如果单元格包含日期值,则我们将其换为字符串。最后,我们输出每个单元格的值。 注意:上面的示例代码假设日期格式为 "yyyy-MM-dd",您可以根据需要更改日期格式
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值