Commit cec94096 authored by liuxinben's avatar liuxinben

修改通过excel批量取消邮寄的poi依赖

parent b8190823
......@@ -91,6 +91,17 @@
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
......
......@@ -3,6 +3,7 @@ package com.yxproject.start.api;
import com.yxproject.start.dto.ReadCardDto;
import com.yxproject.start.entity.PersonPostEntity;
import com.yxproject.start.service.PersonPostService;
import com.yxproject.start.utils.Excel;
import com.yxproject.start.utils.ExportExcel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
......@@ -149,7 +150,7 @@ public class PersonPostApi {
try {
file = File.createTempFile("prefix", "_" + item.getOriginalFilename());
item.transferTo(file);
list = ExportExcel.rearXlsAndXlsx(file, filename);
list = Excel.rearXlsAndXlsx(file, filename);
for (int i = 0; i < list.size(); i++) {
Map<String, Object> noteMap = list.get(i);
String uploadNo = noteMap.get("身份证受理号").toString();
......
package com.yxproject.start.utils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.ObjectUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Excel {
/**
* 解析excel
* @param file
* @return
*/
public static List<Map<String,Object>> rearXlsAndXlsx(File file, String filePath){
Workbook wb = readExcel(file,filePath);
List<Map<String,Object>> outerList= new ArrayList<>();
if (wb != null) {
int sheetSize = wb.getNumberOfSheets();
for (int index = 0; index < sheetSize; index++) {
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheetAt(index);
int rowNum = sheet.getLastRowNum();
int headerNum = sheet.getRow(2).getLastCellNum();
String[] s = new String[headerNum];
for (int o = 0; o < headerNum; o++){
Cell headerCell = sheet.getRow(2).getCell(o);
// headerCell.setCellType(CellType.STRING);
s[o] = headerCell.getStringCellValue();
}
// sheet.getRows()返回该页的总行数
for (int nowRowNum = 3; nowRowNum <= rowNum ; nowRowNum++) {
Map<String,Object> map = new HashMap<>();
Row row = sheet.getRow(nowRowNum);
// sheet.getColumns()返回该页的总列数
int columnNum = row.getLastCellNum();
for (int nowColumnNum = 0; nowColumnNum < columnNum; nowColumnNum++) {
Cell columnCell = sheet.getRow(nowRowNum).getCell(nowColumnNum);
// columnCell.setCellType(CellType.STRING);
if (ObjectUtils.isEmpty(columnCell)){
continue;
}else {
String cellinfo = columnCell.getStringCellValue();
if(cellinfo.isEmpty() || cellinfo.equals("")){
continue;
}else {
map.put(s[nowColumnNum],cellinfo);
}
}
}
if (!ObjectUtils.isEmpty(map)){
outerList.add(map);
}
}
}
}
return outerList;
}
private static Workbook readExcel(File file,String filePath){
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
try {
@SuppressWarnings("resource")
InputStream is = new FileInputStream(file);
if(".xls".equals(extString)){
return new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
return new XSSFWorkbook(is);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
......@@ -2,6 +2,7 @@ package com.yxproject.start.utils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.ObjectUtils;
......@@ -366,75 +367,6 @@ public class ExportExcel {
}
/**
* 解析excel
* @param file
* @return
*/
public static List<Map<String,Object>> rearXlsAndXlsx(File file, String filePath){
Workbook wb = readExcel(file,filePath);
List<Map<String,Object>> outerList= new ArrayList<>();
if (wb != null) {
int sheetSize = wb.getNumberOfSheets();
for (int index = 0; index < sheetSize; index++) {
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheetAt(index);
int rowNum = sheet.getLastRowNum();
int headerNum = sheet.getRow(2).getLastCellNum();
String[] s = new String[headerNum];
for (int o = 0; o < headerNum; o++){
Cell headerCell = sheet.getRow(2).getCell(o);
// headerCell.setCellType(CellType.STRING);
s[o] = headerCell.getStringCellValue();
}
// sheet.getRows()返回该页的总行数
for (int nowRowNum = 3; nowRowNum <= rowNum ; nowRowNum++) {
Map<String,Object> map = new HashMap<>();
Row row = sheet.getRow(nowRowNum);
// sheet.getColumns()返回该页的总列数
int columnNum = row.getLastCellNum();
for (int nowColumnNum = 0; nowColumnNum < columnNum; nowColumnNum++) {
Cell columnCell = sheet.getRow(nowRowNum).getCell(nowColumnNum);
// columnCell.setCellType(CellType.STRING);
if (ObjectUtils.isEmpty(columnCell)){
continue;
}else {
String cellinfo = columnCell.getStringCellValue();
if(cellinfo.isEmpty() || cellinfo.equals("")){
continue;
}else {
map.put(s[nowColumnNum],cellinfo);
}
}
}
if (!ObjectUtils.isEmpty(map)){
outerList.add(map);
}
}
}
}
return outerList;
}
private static Workbook readExcel(File file,String filePath){
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
try {
@SuppressWarnings("resource")
InputStream is = new FileInputStream(file);
if(".xls".equals(extString)){
return new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
throw new FileNotFoundException();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment