Note: as written to excel directly or move to the format of the String to get the cell value obtained formula is obtained, currently found only obtain by Number type, the following figure
Such as the actual excel in value is 156.3, but access to the value is not the value, the following excel in specific value
The following code to obtain the specific results of
Formula under normal circumstances, the first time to write, if you don't open the excel formula is not calculated, so can't get specific values, if you open it again to get can get,
Under the great god excuse me, is there anything good way to solve the formula precision lost?????? Urgent urgent
CodePudding user response:
The great god? Come in lookCodePudding user response:
/* **
*
* @ param cell
* @ return according to the type of cell, return the value of the cell
*/
Public static String getCellValue (Cell Cell) {
String cellValuehttps://bbs.csdn.net/topics/="";
If (cell==null) {
Return cellValue;
}
//the number as a String to read, to avoid to read the situation of the 1.0 1
If (cell. GetCellType ()==CellType. NUMERIC) {
Cell. SetCellType (CellType. STRING);
}
//judge the type of data
The switch (cell getCellType ()) {
Case NUMERIC://digital
CellValue=https://bbs.csdn.net/topics/String.valueOf (cell getNumericCellValue ());
break;
//case STRING STRING
CellValue=https://bbs.csdn.net/topics/String.valueOf (cell getStringCellValue ());
break;
In case a BOOLEAN://a BOOLEAN
CellValue=https://bbs.csdn.net/topics/String.valueOf (cell getBooleanCellValue ());
break;
Case FORMULA://FORMULA
CellValue=https://bbs.csdn.net/topics/String.valueOf (cell getCellFormula ());
break;
Case BLANK://null
cellValuehttps://bbs.csdn.net/topics/="";
break;
Case ERROR://fault
cellValuehttps://bbs.csdn.net/topics/="Error".
break;
Default:
cellValuehttps://bbs.csdn.net/topics/="default".
break;
}
Return cellValue;
}
CodePudding user response:
Mark under study, focus on the follow-up great god answersCodePudding user response:
public static String getStringVal (HSSFCell cell) {
DecimalFormat df=new DecimalFormat (" # ");
The switch (cell getCellType ()) {
Case Cell. CELL_TYPE_BOOLEAN:
Return the cell. GetBooleanCellValue ()? "TRUE" : "FALSE";
Case Cell. CELL_TYPE_FORMULA:
Return the cell. GetCellFormula ();
Case Cell. CELL_TYPE_NUMERIC:
If (HSSFDateUtil isCellDateFormatted (cell)) {
SimpleDateFormat SDF=new SimpleDateFormat (" yyyy - MM - dd HH: MM: ss ");
Return SDF. The format (HSSFDateUtil. GetJavaDate (cell. GetNumericCellValue ()));
}
Return df. The format (cell. GetNumericCellValue ());
Case Cell. CELL_TYPE_STRING:
Return the cell. GetStringCellValue ();
Default:
Return "";
}
}
CodePudding user response: