Java 使用apache poi从excel读取美国邮政编码
我使用以下代码从excel文件中读取邮政编码,其值如下: 06785 excel文件单元格的格式为特殊>>zipcodeJava 使用apache poi从excel读取美国邮政编码,java,excel,Java,Excel,我使用以下代码从excel文件中读取邮政编码,其值如下: 06785 excel文件单元格的格式为特殊>>zipcode File src = new File("C:\\Users\myxl.xlsx"); FileInputStream fis = new FileInputStream(src); XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet sh1 = wb.getSheetAt(0); System.out.println(
File src = new File("C:\\Users\myxl.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh1 = wb.getSheetAt(0);
System.out.println(sh1.getRow(1).getCell(19).getRichStringCellValue());
//Output = Can not get a text value form a numeric cell
wb.close();
fis.close();
我尝试了以下方法:
System.out.println(sh1.getRow(1).getCell(19).getStringCellValue());
//Output = Can not get a text value form a numeric cell
System.out.println(sh1.getRow(1).getCell(19).toString());
//Output = 6785.0
System.out.println(sh1.getRow(1).getCell(19).getNumericCellValue());
//Output = 6785.0
System.out.println(sh1.getRow(1).getCell(19).getRawValue());
//Output = 6785
我无法获得excel(06785)中显示的值,它总是不带0或带.0打印
有谁能帮助我得到一个邮政编码的输出,就像它在excel-06785中显示的那样 这似乎暂时解决了这个问题(使用DecimalFormat) 如果其他人有更好的答案,请在这里提供您的宝贵意见 非常感谢迄今为止所有的投入
String myint = new java.text.DecimalFormat("00000").format(sh1.getRow(1).getCell(19).getNumericCellValue());
//OR
DecimalFormat df= new DecimalFormat("00000");
String formatted = df.format(sh1.getRow(1).getCell(19).getNumericCellValue());
System.out.println(myint);
System.out.println("******");
System.out.println(formatted);
这似乎暂时解决了这个问题(使用DecimalFormat) 如果其他人有更好的答案,请在这里提供您的宝贵意见 非常感谢迄今为止所有的投入
String myint = new java.text.DecimalFormat("00000").format(sh1.getRow(1).getCell(19).getNumericCellValue());
//OR
DecimalFormat df= new DecimalFormat("00000");
String formatted = df.format(sh1.getRow(1).getCell(19).getNumericCellValue());
System.out.println(myint);
System.out.println("******");
System.out.println(formatted);
首先,我们需要将单元格中的值作为字符串,如下所示
cell.setCellType(Cell.CELL_TYPE_STRING);
cellValueAsString = zeroPaddingToZipCode(cell.getStringCellValue(););
然后,我们需要具有与下面类似的功能,以具有填充或非填充的邮政编码
private String zeroPaddingToZipCode(String cellValueAsString) {
if(null != cellValueAsString){
if(!cellValueAsString.contains("-")){
if(cellValueAsString.length() == 4 ){
cellValueAsString = "0"+cellValueAsString;
}
} else {
String[] zipCodes = cellValueAsString.split("-");
if(zipCodes.length == 2 ) {
if(zipCodes[0].length() == 4 || zipCodes[0].length() == 2 ) {
cellValueAsString = "0"+zipCodes[0];
} else {
cellValueAsString = zipCodes[0];
}
cellValueAsString += "-";
if(zipCodes[1].length() == 4 || zipCodes[1].length() == 2 ) {
cellValueAsString += "0"+zipCodes[1];
} else {
cellValueAsString += zipCodes[1];
}
}
}
}
return cellValueAsString;
}
检查它是否工作首先,我们需要从下面的单元格中获取字符串形式的值
cell.setCellType(Cell.CELL_TYPE_STRING);
cellValueAsString = zeroPaddingToZipCode(cell.getStringCellValue(););
然后,我们需要具有与下面类似的功能,以具有填充或非填充的邮政编码
private String zeroPaddingToZipCode(String cellValueAsString) {
if(null != cellValueAsString){
if(!cellValueAsString.contains("-")){
if(cellValueAsString.length() == 4 ){
cellValueAsString = "0"+cellValueAsString;
}
} else {
String[] zipCodes = cellValueAsString.split("-");
if(zipCodes.length == 2 ) {
if(zipCodes[0].length() == 4 || zipCodes[0].length() == 2 ) {
cellValueAsString = "0"+zipCodes[0];
} else {
cellValueAsString = zipCodes[0];
}
cellValueAsString += "-";
if(zipCodes[1].length() == 4 || zipCodes[1].length() == 2 ) {
cellValueAsString += "0"+zipCodes[1];
} else {
cellValueAsString += zipCodes[1];
}
}
}
}
return cellValueAsString;
}
检查它是否工作这可能有助于您在读取单元格值之前将单元格类型设置为cell.cell\u type\u STRING。例如:cell.setCellType(cell.cell\u TYPE\u STRING)从电子表格中获取一个数值,并使用
java.util.Formatter
@Rg90尝试,cell.setCellType(cell.cell\u TYPE\u STRING)添加必要的前导零;System.out.println(sh1.getRow(1.getCell(19.getStringCellValue())代码>输出仍然为空6785@blafasel请你再详细说明一下好吗?我应该如何添加它以及在哪里添加?这可能有助于您在读取单元格值之前将单元格类型设置为cell.cell\u type\u STRING。例如:cell.setCellType(cell.cell\u TYPE\u STRING)从电子表格中获取一个数值,并使用java.util.Formatter
@Rg90尝试,cell.setCellType(cell.cell\u TYPE\u STRING)添加必要的前导零;System.out.println(sh1.getRow(1.getCell(19.getStringCellValue())代码>输出仍然为空6785@blafasel请你再详细说明一下好吗?我该如何添加,请在哪里添加?请查看。在这里,我演示了如何使用CellGeneralFormatter
或CellNumberFormatter
从Excel中获取数字格式的数值。请查看。在这里,我演示了如何使用CellGeneralFormatter
或CellNumberFormatter
从Excel中获取数字格式的数值。