Java 获取字符串值,当我使用日期类型(apache poi)从excel中读取时

Java 获取字符串值,当我使用日期类型(apache poi)从excel中读取时,java,apache-poi,simpledateformat,xssf,Java,Apache Poi,Simpledateformat,Xssf,我有一个xlsx文件,我正在用它阅读-Apache POI库。 比如说,, 在某些行中,我有这样的单元格值: 2013年9月1日| 15136.00 |马特|…… 我的目标是: 将行中的所有单元格读取为字符串值。 但正如我所见,我不能把2013年9月1日的, 它仅用cell.getDateCellValue()表示类似日期类型() 1) 我能不能把01-Sep-13读成字符串:“01-Sep-13”; 2) 如果我有不同的日期模式(ddmmyyy)和(dd MMM yy),我可以将日期值转换

我有一个xlsx文件,我正在用它阅读-Apache POI库。

比如说,, 在某些行中,我有这样的单元格值:

2013年9月1日| 15136.00 |马特|……
我的目标是: 将行中的所有单元格读取为字符串值。 但正如我所见,我不能把2013年9月1日的, 它仅用cell.getDateCellValue()表示类似日期类型()


1) 我能不能把01-Sep-13读成字符串:“01-Sep-13”; 2) 如果我有不同的日期模式(ddmmyyy)和(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();
    }
}
您有两种选择:

  • 使用Java将返回的
    日期
    格式化为
    字符串
    ,格式由您选择。对于所需的每种不同格式,都可以有一个
    SimpleDateFormat
    实例
  • 使用Apache POI直接格式化
    单元格

您可以获取cell#getDateCellValue()的返回值,并使用SimpleDataFormat将其转换为字符串实例

 // 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。希望这有帮助

以下是您的问题答案:

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
    .
    .
    }
}
。
.
.
int fcell=row.getFirstCellNum();//excel的第一个单元格编号
int lcell=row.getLastCellNum()//excel的最后一个单元格编号
while(rows.hasNext()){
row=(XSSFRow)rows.next();//递增行迭代器
对于(int i=fcell;i
2.)如果我有不同的日期模式(ddmmyyy)和(dd MMM yy),我可以将日期值转换为字符串吗

因为第一个答案已经给出了字符串值。 仍然要将字符串值转换为日期,可以使用SimpleDataFormat,如@rgetman和@Keerthi建议的那样

希望这将有助于……:)

输出将是

date is :- 2014/08/01

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

Apache POI具有为您执行此操作的功能,您只需调用它!您需要使用的类是。您将其传递给一个单元格,它会尽力返回一个字符串,其中包含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
我的目标是:将行中的所有单元格读取为字符串值。但正如我所看到的,我不能将01-Sep-13理解为字符串,它只表示类似日期类型()的cell.getDateCellValue()

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

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

,使用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);
               }
          }
Workbook wb=Workbook.getWorkbook(文件);
表s=wb.getSheet(1);
int row=s.getRows();
int col=s.getColumns();

for(int i=1;iThanks方法很多。在类DateFormatter+中。formatCellValue(cell)-返回单元格的字符串表示形式
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);
               }
          }