Excel不能识别jxl dateformat
我有这个代码写入一个Excel文件。
DateFormat customDateFormat = new DateFormat("yyyy/MM/dd HH:mm:ss"); WritableCellFormat dateFormat = new WritableCellFormat(new WritableFont(WritableFont.ARIAL), customDateFormat);
我可以成功写入excel文件,但有一个问题是Excel无法识别我的date格式(“yyyy / MM / dd HH:mm:ss”)。
但是,当我select细胞,并回车。 格式化为我指定的dateformat的文本。
当我select“格式单元格”选项时,它已经在“yyyy / MM / dd HH:mm:ss”中被预先选定),但还没有以这种格式。
它与Excel或JXL有关吗? 我一直在寻找一整天。 谢谢!
1)将string转换为date和datestring(对于我的情况)
我从我的数据库中接收date作为string。
使用SimpleDateFormatparsing和格式化string。
使用一个stringvariables来返回结果。
public static String convertDateToStringDMY(String input) throws ParseException{ SimpleDateFormat df1 = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy"); String temp,result; if(input!=null){ Date date = df2.parse(input); temp = df1.format(date); result = temp; } else{ result = ""; } return result; }
2)@Mare Geldenhuys是正确的。
jxl不计算公式 ,这意味着date将被正确地插入到Excel文件中,并且Excel会将它们识别为date。 但是你不能对它们执行公式(例如你不能对所有的date+1小时,你必须对每个单元手动+1小时,谢天谢地有这个技巧)。
UPDATE
我find了一个excel预先select单元格的解决scheme。
首先,确保数据是datetypes(不是string,如果是string,excel将预选为自定义types)。
并通过使用DateFormat方法设置CellFormat。
WritableCellFormat dateformat1 = new WritableCellFormat(new DateFormat("d/m/yyyy"));