读取 Excel 中日期格式(Apache POI),如何获取字符串值?

10

我有一个xlsx文件,我正在使用Apache POI库进行读取。

例如,在某些行中,我具有以下单元格值:

01-Sep-13 | 15136.00 | Matt | ......


我的目标是:将所有行中的单元格读取为字符串值。 但是,我发现无法将 01-Sep-13 读取为字符串,它只能用 cell.getDateCellValue(); 表示为日期类型 ()。


1)我是否可以以某种方式将01-Sep-13作为字符串读取为 "01-Sep-13"; 2)如果我具有不同的日期模式(ddMMyyyy)和(dd-MMM-yy),我是否可以将日期值转换为字符串;

代码:

当我迭代行和单元格时,Apache POI会分析每个单元格类型:

String normalizeCellType(Cell cell) {

    switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
           return cell.getRichStringCellValue().getString());            
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {

                Date date = cell.getDateCellValue(); ????????????
                System.out.println(date);

            } else {

                ....
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
           return ....
        case Cell.CELL_TYPE_FORMULA:
           return ....
        default:
            System.out.println();
    }
}
8个回答

5

您可以使用cell#getDateCellValue()获取返回值,并使用SimpleDateFormat将其转换为String实例。

 // Create an instance of SimpleDateFormat used for formatting 
    // the string representation of date (month/day/year)
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    // Get the date today using cell.getDateCellValue() 
    Date today = cell.getDateCellValue()       
    // Using DateFormat format method we can create a string 
    // representation of a date with the defined format.
    String reportDate = df.format(today);

您可以以字符串格式获取日期值 - 31/10/2013。希望这有所帮助。

4
你有两个选项:
  • 使用Java的SimpleDateFormat将返回的Date格式化为所需的格式的String。您可以为每种不同的格式需要创建一个SimpleDateFormat实例。
  • 使用Apache POI的DataFormatter直接对Cell进行格式化。

非常感谢这个方法。在DateFormatter类中,+.formatCellValue(cell)会返回单元格的字符串表示形式。 - Sergii Lisnychyi

0

使用Java读取Excel表格中的字符串值非常容易。这是我的方法...

Workbook wb=Workbook.getWorkbook(file);
        Sheet s= wb.getSheet(1);
        int row=s.getRows();
        int col=s.getColumns();
        for(int i=1;i<row;i++){
            for(int j=0;j<col;j++){
                Cell c=s.getCell(j,i);
                String MyString=c.getContents().toString();
                System.out.println(MyString);
               }
          }

0
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
java.util.Date d =  row1.getCell(i).getDateCellValue();
String buy_date = df.format(d);
System.out.println("date is :- "+ buy_date);

输出结果将会是:

date is :- 2014/08/01

现在它将以字符串格式给出日期,您可以将其存储在数据库中。


0

Apache POI有这样的功能,您只需要调用它!您需要使用的类是DataFormatter。您将一个单元格传递给它,它会尽力返回一个包含Excel所显示的该单元格内容的字符串。如果您传递一个字符串单元格,您将得到该字符串。如果您传递一个应用了格式规则的数字单元格,它将根据这些规则格式化数字并将其作为字符串返回。如果您传递一个“日期”单元格(带有日期格式规则的数字单元格),它将按日期格式进行格式化并将其作为字符串返回

您可以通过此代码段看到其运行情况:

 DataFormat df = workBook.createDataFormat();
 CellStyle dateStyle = wb.createCellStye();
 setDataFormat.setDataFormat(df.getFormat("dd-mmm-yy"));

 Cell cell = row.createCell(0);
 cell.setCellStyle(dateStyle);

 // Set it to be a date
 Calendar c = Calendar.getInstance();
 c.set(2013,9-1,1); // Don't forget months are 0 based on Calendar
 cell.setCellValue( c.getTime() );

 // Get it formatted as a string
 DataFormatter formatter = new DataFormatter();
 String cellAsStr = formatter.formatCellValue(cell);

 // cellAsStr will now be "01-Sep-13"
 // based on the format rules applied to the cell

0

以下是您的问题答案:

1)我能以字符串“01-Sep-13”读取01-Sep-13吗?

可以。在读取之前,您需要将单元格类型设置为字符串。像这样:

.
.
.
int fcell = row.getFirstCellNum();// first cell number of excel
int lcell = row.getLastCellNum(); //last cell number of excel
while (rows.hasNext()) {
row = (XSSFRow) rows.next();//increment the row iterator
for (int i = fcell; i < lcell; i++) {
    row.getCell(i).setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
    .
    .
    ..//processing
    .
    .
    }
}

2.)如果我有不同的日期格式(ddMMyyyy和dd-MMM-yy),我能将日期值转换为字符串吗?

既然第一个答案已经给出了String值。 要将String值转换为日期,您可以使用SimpleDateFormat,就像@rgettman和@Keerthi建议的那样。

希望这会对您有所帮助...:)


0

根据rgettman提供的第二个选项,此参考文献提供了最详细的解决方案。

以下是复制的代码片段,希望能有所帮助 参考:https://www.baeldung.com/java-apache-poi-cell-string-value

Cell cell = // a numeric cell with value of 1.234 and format rule "0.00"

DataFormatter formatter = new DataFormatter();
String strValue = formatter.formatCellValue(cell);

0

我的目标是:将所有行中的单元格读取为字符串值。但是我发现,我无法将01-Sep-13读取为字符串,它只能表示为日期类型()并使用cell.getDateCellValue();

您可以使用XSSFExcelExtractor或ExcelExtractor获取正确格式化的单元格值。

注意:- 1)XSSFExcelExtractor用于XLSX文件 2)ExcelExtractor用于XLS文件


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接