- 1 package com.gxxy.team1.yyd.service.impl;
- 2 3 4 import java.io.File;
- 5 import java.io.FileInputStream;
- 6 import java.text.SimpleDateFormat;
- 7 import java.util.ArrayList;
- 8 import java.util.List;
- 9 10 import org.apache.poi.ss.usermodel.Cell;
- 11 import org.apache.poi.ss.usermodel.DateUtil;
- 12 import org.apache.poi.ss.usermodel.Row;
- 13 import org.apache.poi.ss.usermodel.Sheet;
- 14 import org.apache.poi.ss.usermodel.Workbook;
- 15 import org.apache.poi.ss.usermodel.WorkbookFactory;
- 16 import org.springframework.beans.factory.annotation.Autowired;
- 17 import org.springframework.stereotype.Service;
- 18 19 import com.gxxy.team1.yyd.dao.IFileUploadDao;
- 20 import com.gxxy.team1.yyd.service.IFileUploadService;
- 21@Service 22 public class FileUploadServiceImpl implements IFileUploadService {
- 23@Autowired 24 private IFileUploadDao fileDao;
- 25@Override 26 public List readExcel(String path) {
- 27 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
- 28 List < String[] > list = null;
- 29
- try {
- 30 //同时支持Excel 2003、2007
- 31 File excelFile = new File(path); //创建文件对象
- 32 FileInputStream is = new FileInputStream(excelFile); //文件流
- 33 Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
- 34 int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量
- 35 //存储数据容器
- 36 list = new ArrayList < String[] > ();
- 37 //遍历每个Sheet
- 38
- for (int s = 0; s < sheetCount; s++) {
- 39 Sheet sheet = workbook.getSheetAt(s);
- 40 int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
- 41 //遍历每一行
- 42
- for (int r = 0; r < rowCount; r++) {
- 43 Row row = sheet.getRow(r);
- 44 int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
- 45 //用来存储每行数据的容器
- 46 String[] model = new String[cellCount - 1];
- 47 //遍历每一列
- 48
- for (int c = 0; c < cellCount; c++) {
- 49 Cell cell = row.getCell(c);
- 50 int cellType = cell.getCellType();
- 51 52
- if (c == 0) continue; //第一列ID为标志列,不解析
- 53 54 String cellValue = null;
- 55
- switch (cellType) {
- 56
- case Cell.CELL_TYPE_STRING:
- //文本
- 57 cellValue = cell.getStringCellValue();
- 58 //model[c-1] = cellValue;
- 59
- break;
- 60
- case Cell.CELL_TYPE_NUMERIC:
- //数字、日期
- 61
- if (DateUtil.isCellDateFormatted(cell)) {
- 62 cellValue = fmt.format(cell.getDateCellValue()); //日期型
- 63 //model[c-1] = cellValue;
- 64
- }
- 65
- else {
- 66 67 cellValue = String.valueOf(cell.getNumericCellValue()); //数字
- 68 //model[c-1] = cellValue;
- 69
- }
- 70
- break;
- 71
- case Cell.CELL_TYPE_BOOLEAN:
- //布尔型
- 72 cellValue = String.valueOf(cell.getBooleanCellValue());
- 73
- break;
- 74
- case Cell.CELL_TYPE_BLANK:
- //空白
- 75 cellValue = cell.getStringCellValue();
- 76
- break;
- 77
- case Cell.CELL_TYPE_ERROR:
- //错误
- 78 cellValue = "错误";
- 79
- break;
- 80
- case Cell.CELL_TYPE_FORMULA:
- //公式
- 81 cellValue = "错误";
- 82
- break;
- 83
- default:
- 84 cellValue = "错误";
- 85 86
- }
- 87 System.out.print(cellValue + " ");
- 88 89 model[c - 1] = cellValue;
- 90
- }
- 91 //model放入list容器中
- 92 list.add(model);
- 93 System.out.println();
- 94
- }
- 95
- }
- 96 is.close();
- 97
- }
- 98
- catch(Exception e) {
- 99 e.printStackTrace();
- 100
- }
- 101 102
- return list;
- 103
- }
- 104@Override 105 public void save(Object o) {
- 106 fileDao.save(o);
- 107
- }
- 108
- }
来源: http://www.cnblogs.com/sushu-yaya/p/6838135.html