我想将5位数的邮政编码写入Excel文件。 对于前对于数字01803,如果我不将其转换为字符串,它会将其存储为01803字符串但在excel文件上显示错误。所以我想将其存储为大十进制数而不剥离前导0' s。但是默认情况下,大小数将为当前大小写删除它。
在excel中,当我将其存储为字符串并将其转换为excel中的数字时,它将剥离前导零
Address address = new Address();
address.setPostalCode("01803");
String postCode = address.getPostalCode();
if(postCode.matches("^[0-9]*$") && postCode.length() > 2){
if(postCode.charAt(0) == '0'){
System.out.println(postCode.toString());
} else {
System.out.println(new BigDecimal(postCode).doubleValue());
}
}else {
System.out.println(address.getPostalCode());
}
答案 0 :(得分:0)
可以尝试一下
String abc = 0003;
formatToCVSValue(abc);
public static String formatToCVSValue(String s)
{
if(s==null || !hasLength(s))
return "";
else
{
return "\"=\"\""+s+"\"\"\"";
}
}
在excel中打开时,您会看到完全是0003而不是'0003