这里制作介绍controller 的写法
@RequestMapping("analyze")
public R save(@RequestParam("multipartfiles") MultipartFile[] multipartfiles) throws IOException {
for (MultipartFile item : multipartfiles) {
Workbook workbook = getWorkBook(item);
List<String[]> list = new ArrayList<String[]>();
if (workbook != null) {
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {//判断excle有几个sheet页
Sheet sheet = workbook.getSheetAt(sheetNum);//依次读取每一个sheet页
if (sheet == null) {//判断当前的sheet页有没有数据
continue;//如果没有,则跳过当前的循环,进入下次循环
}
int firstRowNum = sheet.getFirstRowNum();//读取第一次出现数据的下标(从哪一行开始有数据)
int lastRowNum = sheet.getLastRowNum();//读取最后一条数据的下标(没有数据的那一行)
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
Row row = sheet.getRow(rowNum);//获取当前行的对象
if (row == null) {
continue;
}
int firstCellNum = row.getFirstCellNum();//取出第一列的下表
int lastCellNum = row.getLastCellNum();//取出最后有数据一列的下表
String[] cells = new String[row.getLastCellNum()];//通过已得打的列数创建对应大小的数组
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {//循环取出每一列的数据
Cell cell = row.getCell(cellNum);//取出该列的数据
cells[cellNum] = getCellValue(cell);//将得到的数据存如刚刚生命的数据中
}
list.add(cells);
}
}
}
}
return R.ok();
}
public static Workbook getWorkBook(MultipartFile file) {
String fileName = file.getOriginalFilename();
Workbook workbook = null;
try {
InputStream is = file.getInputStream();
if (fileName.endsWith("xls")) {
workbook = new HSSFWorkbook(is);
} else if (fileName.endsWith("xlsx")) {
workbook = new XSSFWorkbook(is);
}
} catch (IOException e) {
}
return workbook;
}
public static String stringDateProcess(Cell cell) {
String result = new String();
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 hh:mm:ss");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
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);
}
return result;
}
public static String getCellValue(Cell cell) {
String cellValue = "";
if (cell == null) {
return cellValue;
}
//判断数据的类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: //数字
cellValue = stringDateProcess(cell);
break;
case Cell.CELL_TYPE_STRING: //字符串
cellValue = String.valueOf(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN: //Boolean
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK: //空值
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR: //故障
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
return cellValue;
}