private List<String[]> rosolveFile(InputStream is, String suffix,
int startRow) throws IOException, FileNotFoundException {
Workbook xssfWorkbook = null;
if ("xls".equals(suffix)) {
xssfWorkbook = new HSSFWorkbook(is);
} else if ("xlsx".equals(suffix)) {
xssfWorkbook = new XSSFWorkbook(is);
}
Sheet xssfSheet = xssfWorkbook.getSheetAt(0);
if (xssfSheet == null) {
return null;
}
ArrayList<String[]> list = new ArrayList<String[]>();
int lastRowNum = xssfSheet.getLastRowNum();
for (int rowNum = startRow; rowNum <= lastRowNum; rowNum++) {
if (xssfSheet.getRow(rowNum) != null) {
Row xssfRow = xssfSheet.getRow(rowNum);
short firstCellNum = xssfRow.getFirstCellNum();
short lastCellNum = xssfRow.getLastCellNum();
if (firstCellNum != lastCellNum) {
String[] values = new String[lastCellNum];
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Cell xssfCell = xssfRow.getCell(cellNum);
if (xssfCell == null) {
values[cellNum] = "";
} else {
values[cellNum] = parseExcel(xssfCell);
}
}
list.add(values);
}
}
}
return list;
}
private String parseExcel(Cell cell) {
String result = new String();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 单元格设置成常规
if (temp.equals("General")) {
format.applyPattern("#");
}
result = format.format(value);
}
break;
case HSSFCell.CELL_TYPE_STRING:// String类型
result = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result;
}
//1、判断是否是数值格式
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
short format = cell.getCellStyle().getDataFormat();
SimpleDateFormat sdf = null;
if(format == 14 || format == 31 || format == 57 || format == 58){
//日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}else if (format == 20 || format == 32) {
//时间
sdf = new SimpleDateFormat("HH:mm");
}
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有