Ven*_*kat 57 java apache excel apache-poi
我正在使用Apache POI 3.6,我想阅读一个excel文件,其中包含这样的日期8/23/1991.
switch (cell.getCellType()) {
...
...
case HSSFCell.CELL_TYPE_NUMERIC:
value = "NUMERIC value=" + cell.getNumericCellValue();
break;
...
}
Run Code Online (Sandbox Code Playgroud)
但它采用数值类型并返回这样的值33473.0.
我试过使用数字细胞类型虽然没有运气.
dbltemp=row.getCell(c, Row.CREATE_NULL_AS_BLANK).getNumericCellValue();
if (c == 6 || c == 9) {
strTemp= new String(dbltemp.toString().trim());
long tempDate = Long.parseLong(strTemp);
Date date = new Date(tempDate);
strVal = date.toString();
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决我的问题?
Jos*_*seK 97
如果您知道哪一个单元格,即列位置在每行中说0将是一个日期,您可以row.getCell(0).getDateCellValue()直接进入
.
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html#getDateCellValue()
更新:这是一个示例 - 您可以在上面的开关案例代码中应用它.我正在检查并打印Numeric以及Date值.在这种情况下,我的工作表中的第一列有日期,因此我使用row.getCell(0).
您可以if (HSSFDateUtil.isCellDateFormatted ..直接在开关盒中使用代码块.
if (row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
System.out.println ("Row No.: " + row.getRowNum ()+ " " +
row.getCell(0).getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(row.getCell(0))) {
System.out.println ("Row No.: " + row.getRowNum ()+ " " +
row.getCell(0).getDateCellValue());
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
Row No.: 0 39281.0
Row No.: 0 Wed Jul 18 00:00:00 IST 2007
Row No.: 1 39491.0
Row No.: 1 Wed Feb 13 00:00:00 IST 2008
Row No.: 2 39311.0
Row No.: 2 Fri Aug 17 00:00:00 IST 2007
Run Code Online (Sandbox Code Playgroud)
Chi*_*tan 11
是的,我理解你的问题.如果难以识别,则单元格具有数值或数据值.
如果您希望在Excel中显示格式的数据,则只需使用DataFormatter类格式化单元格.
DataFormatter dataFormatter = new DataFormatter();
String cellStringValue = dataFormatter.formatCellValue(row.getCell(0));
System.out.println ("Is shows data as show in Excel file" + cellStringValue); // Here it automcatically format data based on that cell format.
// No need for extra efforts
Run Code Online (Sandbox Code Playgroud)
小智 6
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
if(cell.getCellTypeEnum() == CellType.NUMERIC||cell.getCellTypeEnum() == CellType.FORMULA)
{
String cellValue=String.valueOf(cell.getNumericCellValue());
if(HSSFDateUtil.isCellDateFormatted(cell))
{
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date date = cell.getDateCellValue();
cellValue = df.format(date);
}
System.out.println(cellValue);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
124474 次 |
| 最近记录: |