Get String value when I read from excel with date type (apache poi)

I have an xlsx file that I am reading using the <- → Apache POI library.

For example, On some line, I have these cell values:

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


My goal: Read all cells in rows as String values. But, as I see it, I cannot read 01-Sep-13 as a string, it only represents the Date () type with cell.getDateCellValue ();


1) Can I somehow read 01-Sep-13 as a line: "01-Sep-13"; 2) Can I convert the Date value to a string if I have different date patterns (ddMMyyyy) and (dd-MMM-yy);

code:

When I iterate over rows and cells, the Apache POI parses each cell type:

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();
    }
}
+4
7

2 :

+2

# 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);

Date String - 31/10/2013. ,

+2

:

1) - 01--13 : "01-Sep-13";

, . String. - :

.
.
.
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.) Date , (ddMMyyyy) (dd-MMM-yy),

String. String , SimpleDateFormat, @rgettman @Keerthi

, ...:)

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

String, .

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

My goal . Read all cells in rows as String values. But, as I see it, I cannot read 01-Sep-13 as a string, it only represents the Date () type with cell.getDateCellValue ();

You can use XSSFExcelExtractor or ExcelExtractor to format the cell value correctly.

Note: - 1) XSSFExcelExtractor for XLSX file 2) ExcelExtractor for XLS file

0
source

It is so easy to read string values ​​in an excel sheet using java. this is my way.

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
source

All Articles