本文整理汇总了Java中org.apache.poi.hssf.usermodel.HSSFDateUtil.getJavaDate方法的典型用法代码示例。如果您正苦于以下问题:Java HSSFDateUtil.getJavaDate方法的具体用法?Java HSSFDateUtil.getJavaDate怎么用?Java HSSFDateUtil.getJavaDate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.hssf.usermodel.HSSFDateUtil
的用法示例。
在下文中一共展示了HSSFDateUtil.getJavaDate方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCellValue
import org.apache.poi.hssf.usermodel.HSSFDateUtil; //导入方法依赖的package包/类
public String getCellValue(Cell cell) {
if (cell == null)
return ExcelODAConstants.EMPTY_STRING;
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
return resolveFormula(cell);
}
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if( HSSFDateUtil.isCellDateFormatted(cell) ){
Date myjavadate = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
return sdf.format( myjavadate );
}
return ((Double) cell.getNumericCellValue()).toString();
}
return cell.toString();
}
示例2: getCellValue
import org.apache.poi.hssf.usermodel.HSSFDateUtil; //导入方法依赖的package包/类
/**
* 获取单元格值
*
* @param row 获取的行
* @param column 获取单元格列号
* @return 单元格值
*/
public Object getCellValue(Row row, int column) {
Object val = "";
try {
Cell cell = row.getCell(column);
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
// val = cell.getNumericCellValue();
// 当excel 中的数据为数值或日期是需要特殊处理
if (HSSFDateUtil.isCellDateFormatted(cell)) {
double d = cell.getNumericCellValue();
Date date = HSSFDateUtil.getJavaDate(d);
SimpleDateFormat dformat = new SimpleDateFormat(
"yyyy-MM-dd");
val = dformat.format(date);
} else {
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);// true时的格式:1,234,567,890
val = nf.format(cell.getNumericCellValue());// 数值类型的数据为double,所以需要转换一下
}
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
val = cell.getStringCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
val = cell.getCellFormula();
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
val = cell.getBooleanCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
val = cell.getErrorCellValue();
}
}
} catch (Exception e) {
return val;
}
return val;
}
示例3: extractCell
import org.apache.poi.hssf.usermodel.HSSFDateUtil; //导入方法依赖的package包/类
static protected Serializable extractCell(org.apache.poi.ss.usermodel.Cell cell) {
int cellType = cell.getCellType();
if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA) {
cellType = cell.getCachedFormulaResultType();
}
if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR ||
cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK) {
return null;
}
Serializable value = null;
if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN) {
value = cell.getBooleanCellValue();
} else if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC) {
double d = cell.getNumericCellValue();
if (HSSFDateUtil.isCellDateFormatted(cell)) {
value = HSSFDateUtil.getJavaDate(d);
// TODO: If we had a time datatype, we could use something like the following
// to distinguish times from dates (although Excel doesn't really make the distinction)
// Another alternative would be to look for values < 0.60
// String format = cell.getCellStyle().getDataFormatString();
// if (!format.contains("d") && !format.contains("m") && !format.contains("y") ) {
// // It's just a time
// }
} else {
value = d;
}
} else {
String text = cell.getStringCellValue();
if (text.length() > 0) {
value = text;
}
}
return value;
}
示例4: parseXLS
import org.apache.poi.hssf.usermodel.HSSFDateUtil; //导入方法依赖的package包/类
public JSONObject parseXLS(String filename, int sheetNo) throws FileNotFoundException, IOException, JSONException{
JSONObject jobj=new JSONObject();
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filename));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
HSSFSheet sheet = wb.getSheetAt(sheetNo);
int startRow=0;
int maxRow=sheet.getLastRowNum();
int maxCol=0;
int noOfRowsDisplayforSample = 20;
if(noOfRowsDisplayforSample > sheet.getLastRowNum()){
noOfRowsDisplayforSample = sheet.getLastRowNum();
}
JSONArray jArr=new JSONArray();
try {
for(int i=0;i <= noOfRowsDisplayforSample;i++) {
HSSFRow row = sheet.getRow(i);
JSONObject obj=new JSONObject();
JSONObject jtemp1 = new JSONObject();
if(row==null){
continue;
}
if(i==0) {
maxCol=row.getLastCellNum();
}
for(int cellcount=0; cellcount<maxCol; cellcount++){
HSSFCell cell = row.getCell(cellcount);
CellReference cref = new CellReference(i, cellcount);
String colHeader=cref.getCellRefParts()[2];
String val=null;
if(cell!=null){
switch(cell.getCellType()){
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
val=Double.toString(cell.getNumericCellValue());
java.util.Date date1 = HSSFDateUtil.getJavaDate(Double.parseDouble(val));
DateFormat sdf = new SimpleDateFormat(df);
val = sdf.format(date1);
}else{
val=dfmt.format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING: val=cleanHTML(cell.getRichStringCellValue().getString()); break;
}
}
if(i==0){ // List of Headers (Consider first row as Headers)
if(val!=null){
jtemp1 = new JSONObject();
jtemp1.put("header", val==null?"":val);
jtemp1.put("index", cellcount);
jobj.append("Header", jtemp1);
}
}
obj.put(colHeader,val);
}
// if(obj.length()>0){ //Don't show blank row in preview grid[SK]
jArr.put(obj);
// }
}
} catch(Exception ex) {
Logger.getLogger(ImportHandler.class.getName()).log(Level.SEVERE, null, ex);
}
jobj.put("startrow", startRow);
jobj.put("maxrow", maxRow);
jobj.put("maxcol", maxCol);
jobj.put("index", sheetNo);
jobj.put("data", jArr);
jobj.put("filename", filename);
jobj.put("msg", "XLS has been successfully uploaded");
jobj.put("lsuccess", true);
jobj.put("valid", true);
return jobj;
}
示例5: parseXLS1
import org.apache.poi.hssf.usermodel.HSSFDateUtil; //导入方法依赖的package包/类
public JSONObject parseXLS1(String filename, int sheetNo,int startindex) throws FileNotFoundException, IOException, JSONException{
JSONObject jobj=new JSONObject();
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(filename));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
HSSFSheet sheet = wb.getSheetAt(sheetNo);
ArrayList<String> arr = new ArrayList<String>();
int startRow=0;
int maxRow=sheet.getLastRowNum();
int maxCol=0;
JSONArray jArr=new JSONArray();
try {
for(int i=startindex;i<=sheet.getLastRowNum();i++) {
HSSFRow row = sheet.getRow(i);
JSONObject obj=new JSONObject();
JSONObject jtemp1 = new JSONObject();
if(row==null){
continue;
}
if(i==startindex){
maxCol=row.getLastCellNum();
}
for(int j=0; j<maxCol; j++){
HSSFCell cell = row.getCell(j);
String val=null;
if(cell==null){
arr.add(val);
continue;
};
String colHeader=new CellReference(i, j).getCellRefParts()[2];
switch(cell.getCellType()){
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
val=Double.toString(cell.getNumericCellValue());
java.util.Date date1 = HSSFDateUtil.getJavaDate(Double.parseDouble(val));
DateFormat sdf = new SimpleDateFormat(df);
val = sdf.format(date1);
}else{
val=dfmt.format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING: val=cleanHTML(cell.getRichStringCellValue().getString()); break;
}
if(i==startindex){ // List of Headers (consider startindex row as a headers)
if(val!=null){
jtemp1 = new JSONObject();
jtemp1.put("header", val);
jtemp1.put("index", j);
jobj.append("Header", jtemp1);
obj.put(colHeader,val);
}
arr.add(val);
} else {
if(arr.get(j)!=null)
obj.put(arr.get(j),val);
}
}
if(obj.length()>0){
jArr.put(obj);
}
}
} catch(Exception ex) {
Logger.getLogger(ImportHandler.class.getName()).log(Level.SEVERE, null, ex);
}
jobj.put("startrow", startRow);
jobj.put("maxrow", maxRow);
jobj.put("maxcol", maxCol);
jobj.put("index", sheetNo);
jobj.put("data", jArr);
jobj.put("filename", filename);
jobj.put("msg", "XLS has been successfully uploaded");
jobj.put("lsuccess", true);
jobj.put("valid", true);
return jobj;
}
示例6: readNext
import org.apache.poi.hssf.usermodel.HSSFDateUtil; //导入方法依赖的package包/类
/**
* @see fll.util.CellFileReader#readNext()
*/
@SuppressFBWarnings(value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS", justification = "Return null rather than zero length array so that we know when we hit EFO")
public String[] readNext() throws IOException {
if (lineNumber >= sheet.getLastRowNum()) {
return null;
}
++lineNumber;
final Row row = sheet.getRow(lineNumber);
if (null == row) {
return new String[0];
}
final List<String> data = new LinkedList<String>();
for (int cellIdx = 0; cellIdx < row.getLastCellNum(); ++cellIdx) {
final Cell cell = row.getCell(cellIdx, Row.RETURN_NULL_AND_BLANK);
if (null == cell) {
data.add(null);
} else {
final String str;
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
final double d = cell.getNumericCellValue();
// test if a date!
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// make sure to format times like we expect them
final Date date = HSSFDateUtil.getJavaDate(d);
str = TournamentSchedule.DATE_FORMAT_AM_PM_SS.get().format(date);
} else {
// check for integer
if (FP.equals(d, Math.round(d), 1e-10)) {
str = String.valueOf((int) d);
} else {
str = formatter.formatCellValue(cell, formulaEvaluator);
}
}
} else {
str = formatter.formatCellValue(cell, formulaEvaluator);
}
data.add(str);
}
}
return data.toArray(new String[data.size()]);
}
示例7: toDate
import org.apache.poi.hssf.usermodel.HSSFDateUtil; //导入方法依赖的package包/类
@Override
public Date toDate() {
return value == null ? null : HSSFDateUtil.getJavaDate(toDouble());
}
示例8: getDateCellValue
import org.apache.poi.hssf.usermodel.HSSFDateUtil; //导入方法依赖的package包/类
/**
* Get the value of the cell as a date. For strings we throw an exception. For
* blank cells we return a null.
*
* @return the value of the cell as a date
* @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CELL_TYPE_STRING
* @throws NumberFormatException if the cell value isn't a parsable <code>double</code>.
*/
@Override
public Date getDateCellValue() {
if(getCellType() == CELL_TYPE_STRING){
throw new IllegalStateException("Cell type cannot be CELL_TYPE_STRING");
}
return rawContents == null ? null : HSSFDateUtil.getJavaDate(getNumericCellValue(), use1904Dates);
}