|
@@ -10,6 +10,9 @@ import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
|
|
|
+import org.apache.poi.ss.usermodel.CellType;
|
|
|
|
|
+
|
|
|
|
|
+import static org.apache.poi.ss.usermodel.CellType.*;
|
|
|
|
|
|
|
|
public class ReadExcelUtil {
|
|
public class ReadExcelUtil {
|
|
|
public static HSSFWorkbook getWorkBook(String filePath){
|
|
public static HSSFWorkbook getWorkBook(String filePath){
|
|
@@ -41,7 +44,7 @@ public class ReadExcelUtil {
|
|
|
String msg = getCellStringValue(sfCell);
|
|
String msg = getCellStringValue(sfCell);
|
|
|
return msg;
|
|
return msg;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public static void reader(String filePath) {
|
|
public static void reader(String filePath) {
|
|
|
try {
|
|
try {
|
|
|
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));
|
|
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));
|
|
@@ -49,7 +52,7 @@ public class ReadExcelUtil {
|
|
|
HSSFSheet sheet = wb.getSheetAt(0);
|
|
HSSFSheet sheet = wb.getSheetAt(0);
|
|
|
HSSFRow row = sheet.getRow(3);
|
|
HSSFRow row = sheet.getRow(3);
|
|
|
HSSFCell cell = row.getCell((short) 0);
|
|
HSSFCell cell = row.getCell((short) 0);
|
|
|
- int type = cell.getCellType();
|
|
|
|
|
|
|
+ CellType type = cell.getCellType();
|
|
|
String msg = getCellStringValue(cell);
|
|
String msg = getCellStringValue(cell);
|
|
|
System.out.println(type + ":" + msg);
|
|
System.out.println(type + ":" + msg);
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
@@ -59,28 +62,29 @@ public class ReadExcelUtil {
|
|
|
|
|
|
|
|
public static String getCellStringValue(HSSFCell cell) {
|
|
public static String getCellStringValue(HSSFCell cell) {
|
|
|
String cellValue = "";
|
|
String cellValue = "";
|
|
|
- switch (cell.getCellType()) {
|
|
|
|
|
- case HSSFCell.CELL_TYPE_STRING:
|
|
|
|
|
|
|
+ CellType cellType = cell.getCellType();
|
|
|
|
|
+ switch (cellType) {
|
|
|
|
|
+ case STRING:
|
|
|
cellValue = cell.getStringCellValue();
|
|
cellValue = cell.getStringCellValue();
|
|
|
if (cellValue.trim().equals("") || cellValue.trim().length() <= 0) {
|
|
if (cellValue.trim().equals("") || cellValue.trim().length() <= 0) {
|
|
|
cellValue = " ";
|
|
cellValue = " ";
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
- case HSSFCell.CELL_TYPE_NUMERIC:
|
|
|
|
|
|
|
+ case NUMERIC:
|
|
|
// cellValue = String.valueOf(cell.getNumericCellValue());
|
|
// cellValue = String.valueOf(cell.getNumericCellValue());
|
|
|
DecimalFormat formatter = new DecimalFormat("######");
|
|
DecimalFormat formatter = new DecimalFormat("######");
|
|
|
cellValue = formatter.format(cell.getNumericCellValue());
|
|
cellValue = formatter.format(cell.getNumericCellValue());
|
|
|
break;
|
|
break;
|
|
|
- case HSSFCell.CELL_TYPE_FORMULA:
|
|
|
|
|
- cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
|
|
|
|
|
|
|
+ case FORMULA:
|
|
|
|
|
+ cell.setCellType(NUMERIC);
|
|
|
cellValue = String.valueOf(cell.getNumericCellValue());
|
|
cellValue = String.valueOf(cell.getNumericCellValue());
|
|
|
break;
|
|
break;
|
|
|
- case HSSFCell.CELL_TYPE_BLANK:
|
|
|
|
|
|
|
+ case BLANK:
|
|
|
cellValue = " ";
|
|
cellValue = " ";
|
|
|
break;
|
|
break;
|
|
|
- case HSSFCell.CELL_TYPE_BOOLEAN:
|
|
|
|
|
|
|
+ case BOOLEAN:
|
|
|
break;
|
|
break;
|
|
|
- case HSSFCell.CELL_TYPE_ERROR:
|
|
|
|
|
|
|
+ case ERROR:
|
|
|
break;
|
|
break;
|
|
|
default:
|
|
default:
|
|
|
break;
|
|
break;
|