|
@@ -1,207 +0,0 @@
|
|
|
-package com.example.utils.excel;
|
|
|
-
|
|
|
-import com.example.utils.RRException;
|
|
|
-import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
-import org.apache.poi.ss.usermodel.Cell;
|
|
|
-import org.apache.poi.ss.usermodel.CellType;
|
|
|
-import org.apache.poi.ss.usermodel.Row;
|
|
|
-import org.apache.poi.ss.usermodel.Sheet;
|
|
|
-import org.apache.poi.ss.usermodel.Workbook;
|
|
|
-import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
-import org.json.JSONObject;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * 读取EXCEL表的信息
|
|
|
- * @author
|
|
|
- * @date 2020-05-04 13:10
|
|
|
- */
|
|
|
-public class ExcelTool {
|
|
|
-
|
|
|
- /**
|
|
|
- * 读取EXCEL表信息
|
|
|
- *
|
|
|
- * @param file
|
|
|
- * @param title
|
|
|
- * @return
|
|
|
- */
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static List<String[]> readExcel(MultipartFile file, String[] title) throws RRException, IOException, InvalidFormatException {
|
|
|
- return readExcel(file, title, 1);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 读取EXCEL表信息(从第几行开始)
|
|
|
- *
|
|
|
- * @param file
|
|
|
- * @param title
|
|
|
- * @param fromRow
|
|
|
- * @return
|
|
|
- */
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static List<String[]> readExcel(MultipartFile file, String[] title, int fromRow) throws RRException, IOException, InvalidFormatException {
|
|
|
- Workbook wb;
|
|
|
- List<String[]> list = new ArrayList();
|
|
|
- InputStream is = file.getInputStream();
|
|
|
- wb = WorkbookFactory.create(is);
|
|
|
- Sheet sheet = wb.getSheetAt(0);
|
|
|
- Row row;
|
|
|
- for (int i = sheet.getFirstRowNum() + fromRow; i <= sheet.getLastRowNum(); i++) {
|
|
|
- row = sheet.getRow(i);
|
|
|
- System.out.println(i);
|
|
|
- if (i >= 1 && row != null && row.getCell(0) != null && !row.getCell(0).toString().equals("")) {
|
|
|
- String[] t = new String[title.length];
|
|
|
- for (int m = 0; m < title.length; m++) {
|
|
|
- Cell cell = row.getCell(m);
|
|
|
- t[m] = getCellValue(cell);
|
|
|
- }
|
|
|
- list.add(t);
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static List<String[]> readExcel(MultipartFile file,int fromRow) throws RRException, IOException, InvalidFormatException {
|
|
|
- Workbook wb;
|
|
|
- List<String[]> list = new ArrayList();
|
|
|
- InputStream is = file.getInputStream();
|
|
|
- wb = WorkbookFactory.create(is);
|
|
|
- Sheet sheet = wb.getSheetAt(0);
|
|
|
- Row row;
|
|
|
- //必须是连续的数据
|
|
|
- for (int i = sheet.getFirstRowNum() + fromRow; i <= sheet.getLastRowNum(); i++) {
|
|
|
- row = sheet.getRow(i);
|
|
|
- if (i >= 1 && row != null && row.getCell(0) != null && !row.getCell(0).toString().equals("")) {
|
|
|
- String[] t = new String[50];
|
|
|
- for (int m = 0; m <50; m++) {
|
|
|
- Cell cell = row.getCell(m);
|
|
|
- t[m] = getCellValue(cell);
|
|
|
- }
|
|
|
- list.add(t);
|
|
|
- }else{
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static String getCellValue(Cell cell) {
|
|
|
- String cellValue = "";
|
|
|
- if (cell == null) {
|
|
|
- return cellValue;
|
|
|
- }
|
|
|
- //判断数据的类型
|
|
|
- switch (cell.getCellTypeEnum()) {
|
|
|
- case NUMERIC: //数字
|
|
|
- String a = "";
|
|
|
- if (HSSFDateUtil.isCellDateFormatted(cell)) {
|
|
|
- Date date = cell.getDateCellValue();
|
|
|
- a = DateFormatUtils.format(date, "yyyy-MM-dd");
|
|
|
- cellValue = a;
|
|
|
- } else {
|
|
|
- cell.setCellType(CellType.STRING);
|
|
|
- cellValue = String.valueOf(cell.getStringCellValue()); }
|
|
|
- break;
|
|
|
- case STRING: //字符串
|
|
|
- cellValue = String.valueOf(cell.getStringCellValue());
|
|
|
- break;
|
|
|
- case BOOLEAN: //Boolean
|
|
|
- cellValue = String.valueOf(cell.getBooleanCellValue());
|
|
|
- break;
|
|
|
- case FORMULA: //公式
|
|
|
- cell.setCellType(CellType.STRING);
|
|
|
- cellValue = String.valueOf(cell.getStringCellValue());
|
|
|
- break;
|
|
|
- case BLANK: //空值
|
|
|
- cellValue = "";
|
|
|
- break;
|
|
|
- case ERROR: //故障
|
|
|
- cellValue = "非法字符";
|
|
|
- break;
|
|
|
- default:
|
|
|
- cellValue = "未知类型";
|
|
|
- break;
|
|
|
- }
|
|
|
- return cellValue;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 读取导入Excel的表头数据
|
|
|
- */
|
|
|
- public static List<String> readExcelTitle(MultipartFile file) throws RRException, IOException, InvalidFormatException {
|
|
|
- Workbook wb;
|
|
|
- List list = new ArrayList();
|
|
|
- InputStream is = file.getInputStream();
|
|
|
- wb = WorkbookFactory.create(is);
|
|
|
- Sheet sheet = wb.getSheetAt(0);
|
|
|
- Row row = sheet.getRow(0);
|
|
|
- int m=0;
|
|
|
-
|
|
|
- while(true){
|
|
|
- Cell cell = row.getCell(m);
|
|
|
- String t=getCellValue(cell);
|
|
|
- if(t!=null&&!t.equals("")){
|
|
|
- list.add(t);
|
|
|
- m++;
|
|
|
- }else{
|
|
|
- break;
|
|
|
- }
|
|
|
- if(m>100){
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- public static List<JSONObject> readExcel(MultipartFile file, List<String> propertyList) throws RRException, IOException, InvalidFormatException {
|
|
|
- Workbook wb;
|
|
|
- List<JSONObject> list = new ArrayList();
|
|
|
- InputStream is = file.getInputStream();
|
|
|
- wb = WorkbookFactory.create(is);
|
|
|
- Sheet sheet = wb.getSheetAt(0);
|
|
|
- Row row;
|
|
|
- for (int i = sheet.getFirstRowNum()+1 ; i <= sheet.getLastRowNum(); i++) {
|
|
|
- row = sheet.getRow(i);
|
|
|
- JSONObject jsonObject=new JSONObject();
|
|
|
- if (i >= 1 && row != null && row.getCell(0) != null && !row.getCell(0).toString().equals("")) {
|
|
|
- for (int m = 0; m < propertyList.size(); m++) {
|
|
|
- Cell cell = row.getCell(m);
|
|
|
- jsonObject.put(propertyList.get(m),getCellValue(cell));
|
|
|
- }
|
|
|
- }
|
|
|
- list.add(jsonObject);
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param file
|
|
|
- * @param title
|
|
|
- * @return
|
|
|
- */
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static List<String[]> readExcelMaxData(MultipartFile file, String[] title) throws RRException {
|
|
|
- try {
|
|
|
- String fileName = file.getOriginalFilename();
|
|
|
- String prefix = fileName.substring(fileName.lastIndexOf("."));
|
|
|
- File excelFile = File.createTempFile("itemAA", prefix);
|
|
|
- file.transferTo(excelFile);
|
|
|
- ExcelReaderUtil.list.clear();
|
|
|
- ExcelReaderUtil.readExcel(excelFile.toString());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return ExcelReaderUtil.list;
|
|
|
- }
|
|
|
-}
|