日期也是數值類型,中文日期帶年月日的,在HSSFDateUtil.isCellDateFormatted(cell)中讀取不出來,所以手動判斷
// 數值類型(取值用cell.getNumericCellValue() 或cell.getDateCellValue())
case HSSFCell.CELL_TYPE_NUMERIC:
int styleFormat = cell.getCellStyle().getDataFormat();
if (HSSFDateUtil.isCellDateFormatted(cell)) { //默認日日期格式
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}else if(176 < styleFormat && styleFormat < 185) {//中文日期格式
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
} else {// 數字格式,保留兩位小數
value = df2.format(cell.getNumericCellValue());
}
break;
全部方法
package com.nstc.aims.controller.business.recordmould;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.nstc.aims.model.RecordMap;
import com.nstc.util.StringUtils;
/**
* <p>Title: POIUtil.java</p>
*
* <p>Description: </p>
*
* <p>Company: 北京九恆星科技股份有限公司</p>
*
* @author luhao
*
* @since:2019年6月3日 上午9:04:13
*
*/
public class POIUtil {
public static void main(String[] args) {
String fileName = "C:\\Users\\Administrator\\Desktop\\aaa.xls";
List<RecordMap> list = getTitleList(fileName, 0, 0, 0);
for (RecordMap recordMap : list) {
System.out.println(recordMap.getColName());
}
}
/**
*
* @Description:組裝Excel中的標題行
* @param fileName 文件路徑
* @param sheetId sheet頁,從0開始
* @param beginRow 始讀行,從0開始
* @param beginCol 始讀列,從0開始
* @return List<RecordMap>
* @author luhao
* @since:2019年6月3日 上午10:08:53
*/
public static List<RecordMap> getTitleList(String fileName,int sheetId,int beginRow,int beginCol) {
List<RecordMap> result = new ArrayList<RecordMap>();
FileInputStream fis = null;
POIFSFileSystem pfs = null;
try {
fis = new FileInputStream(fileName);
pfs = new POIFSFileSystem(fis);
HSSFWorkbook wk = new HSSFWorkbook(pfs);
//最大的的sheet頁數,從0開始
int totleSheetNum = wk.getNumberOfSheets();
Validate.isTrue(sheetId < totleSheetNum,"讀取的sheet頁不能大於sheet的總數量!");
// TODO 讀取sheet,校驗sheet
HSSFSheet sheet = wk.getSheetAt(sheetId);
//表頭行
HSSFRow row = sheet.getRow(beginRow);
for(short i = (short)beginCol ; row != null && i < row.getLastCellNum() ; i++) {
HSSFCell cell = row.getCell(i);
String value = getCellValue(cell);
if(StringUtils.isBlank(value)) {
continue;
}
RecordMap recordMap = new RecordMap();
recordMap.setColName(value);
recordMap.setColIndex((int)cell.getCellNum());
recordMap.setColNo(convertNumToColString(i));
result.add(recordMap);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage());
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
return result;
}
/**
* @Description:返回HSSFCell的字符串值
* @param cell
* @return String
* @author luhao
* @since:2019年6月3日 上午10:09:44
*/
public static String getCellValue(HSSFCell cell) {
// 用String接收所有返回的值
String value = null;
// 日期格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化數字
DecimalFormat df2 = new DecimalFormat("0.00");
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
// String類型的數據
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
// 數值類型(取值用cell.getNumericCellValue() 或cell.getDateCellValue())
case HSSFCell.CELL_TYPE_NUMERIC:
int styleFormat = cell.getCellStyle().getDataFormat();
if (HSSFDateUtil.isCellDateFormatted(cell)) { //默認日日期格式
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}else if(176 < styleFormat && styleFormat < 185) {//中文日期格式
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
} else {// 數字格式,保留兩位小數
value = df2.format(cell.getNumericCellValue());
}
break;
// Boolean類型
case HSSFCell.CELL_TYPE_BOOLEAN:
value = String.valueOf(cell.getBooleanCellValue());
break;
// 表達式類型
case HSSFCell.CELL_TYPE_FORMULA:
value = String.valueOf(cell.getCellFormula());
break;
// 異常類型 不知道何時算異常
case HSSFCell.CELL_TYPE_ERROR:
value = String.valueOf(cell.getErrorCellValue());
break;
// 空,不知道何時算空
case HSSFCell.CELL_TYPE_BLANK:
value = "";
break;
default:
value = "";
break;
}
if(StringUtils.isBlank(value)){
value = "";
}
return value;
}
/**
* 獲得列名,例如A1 B5
* @Description:
* @param col
* @return String
* @author luhao
* @since:2019年6月4日 上午9:46:01
*/
private static String convertNumToColString(int col) {
String retval = null;
int mod = col % 26;
int div = col / 26;
char small=(char)(mod + 65);
char big = (char)(div + 64);
if (div == 0) {
retval = ""+small;
} else {
retval = ""+big+""+small;
}
return retval;
}
}
: