|
|
@ -1,5 +1,6 @@
|
|
|
|
package com.dsic.gj_erp.util;
|
|
|
|
package com.dsic.gj_erp.util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.time.DateUtils;
|
|
|
|
import org.apache.commons.lang3.time.DateUtils;
|
|
|
@ -9,6 +10,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
@ -187,49 +189,66 @@ public class ExcelUtil {
|
|
|
|
public static JSONObject readDmXbjh(MultipartFile file) {
|
|
|
|
public static JSONObject readDmXbjh(MultipartFile file) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Workbook workbook=isWorkBook(file);
|
|
|
|
Workbook workbook=isWorkBook(file);
|
|
|
|
// 获取第一张表
|
|
|
|
return read(workbook);
|
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Row monthRow=sheet.getRow(2);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
Map<Integer,String> monthMap=new HashMap<>();
|
|
|
|
}
|
|
|
|
for (int index = 0; index < monthRow.getPhysicalNumberOfCells(); index++){
|
|
|
|
|
|
|
|
Cell cell = monthRow.getCell(index);
|
|
|
|
}
|
|
|
|
// 转换为字符串类型
|
|
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
public static JSONObject readDmXbjh(File file) {
|
|
|
|
monthMap.put(index,cell.toString());
|
|
|
|
try {
|
|
|
|
|
|
|
|
Workbook workbook=isWorkBook(file);
|
|
|
|
|
|
|
|
return read(workbook);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static JSONObject read(Workbook workbook){
|
|
|
|
|
|
|
|
// 获取第一张表
|
|
|
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
|
|
|
Row monthRow=sheet.getRow(2);
|
|
|
|
|
|
|
|
Map<Integer,String> monthMap=new HashMap<>();
|
|
|
|
|
|
|
|
for (int index = 0; index < monthRow.getPhysicalNumberOfCells(); index++){
|
|
|
|
|
|
|
|
Cell cell = monthRow.getCell(index);
|
|
|
|
|
|
|
|
// 转换为字符串类型
|
|
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
|
|
monthMap.put(index,cell.toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// String year="";
|
|
|
|
|
|
|
|
// List<Map<String,Object>> mapList=new ArrayList<>();
|
|
|
|
|
|
|
|
// List<DmXbjh> dmXbjhs=new ArrayList<>();
|
|
|
|
|
|
|
|
JSONObject jsonObject=new JSONObject();
|
|
|
|
|
|
|
|
for (int i = 3; i < sheet.getPhysicalNumberOfRows(); i++) {
|
|
|
|
|
|
|
|
Row row = sheet.getRow(i);
|
|
|
|
|
|
|
|
String dcch=row.getCell(0).toString();
|
|
|
|
|
|
|
|
if (dcch.contains("待接")||dcch.contains("已接")||dcch.contains("合计")){
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// String year="";
|
|
|
|
|
|
|
|
// List<Map<String,Object>> mapList=new ArrayList<>();
|
|
|
|
|
|
|
|
// List<DmXbjh> dmXbjhs=new ArrayList<>();
|
|
|
|
|
|
|
|
JSONObject jsonObject=new JSONObject();
|
|
|
|
|
|
|
|
for (int i = 3; i < sheet.getPhysicalNumberOfRows(); i++) {
|
|
|
|
|
|
|
|
Row row = sheet.getRow(i);
|
|
|
|
|
|
|
|
String dcch=row.getCell(0).toString();
|
|
|
|
|
|
|
|
if (dcch.contains("待接")||dcch.contains("已接")||dcch.contains("合计")){
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map<String,Object> map=new HashMap<>();
|
|
|
|
// Map<String,Object> map=new HashMap<>();
|
|
|
|
// map.put("dcCh",dcch);
|
|
|
|
// map.put("dcCh",dcch);
|
|
|
|
List<Map> mapList=new ArrayList<>();
|
|
|
|
List<Map> mapList=new ArrayList<>();
|
|
|
|
for (int index = 1; index < row.getPhysicalNumberOfCells(); index++) {
|
|
|
|
for (int index = 1; index < row.getPhysicalNumberOfCells(); index++) {
|
|
|
|
Cell cell = row.getCell(index);
|
|
|
|
Cell cell = row.getCell(index);
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
|
|
|
|
|
|
String month=monthMap.get(index);
|
|
|
|
String month=monthMap.get(index);
|
|
|
|
if (month.contains("计")){
|
|
|
|
if (month.contains("计")){
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String cl=cell+"";
|
|
|
|
String cl=cell+"";
|
|
|
|
String year=getMergedRegionValue(sheet,1,index).toString();
|
|
|
|
String year=getMergedRegionValue(sheet,1,index).toString();
|
|
|
|
// DmXbjh dmXbjh=new DmXbjh();
|
|
|
|
// DmXbjh dmXbjh=new DmXbjh();
|
|
|
|
Map map=new HashMap();
|
|
|
|
Map map=new HashMap();
|
|
|
|
map.put("zt","0");
|
|
|
|
map.put("zt","0");
|
|
|
|
map.put("nf",year.replace("年",""));
|
|
|
|
map.put("nf",year.replace("年",""));
|
|
|
|
map.put("yf",String.format("%02d", Integer.parseInt(month.replace("月",""))));
|
|
|
|
map.put("yf",String.format("%02d", Integer.parseInt(month.replace("月",""))));
|
|
|
|
if (StringUtils.isNotEmpty(cl)){
|
|
|
|
if (StringUtils.isNotEmpty(cl)){
|
|
|
|
map.put("zl",Double.parseDouble(cl));
|
|
|
|
map.put("zl",Double.parseDouble(cl));
|
|
|
|
}else {
|
|
|
|
}else {
|
|
|
|
map.put("zl",0.0);
|
|
|
|
map.put("zl",0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// dmXbjh.setZt("0");
|
|
|
|
// dmXbjh.setZt("0");
|
|
|
|
// dmXbjh.setNf(year.replace("年",""));
|
|
|
|
// dmXbjh.setNf(year.replace("年",""));
|
|
|
|
// dmXbjh.setYf(String.format("%02d", Integer.parseInt(month.replace("月",""))));
|
|
|
|
// dmXbjh.setYf(String.format("%02d", Integer.parseInt(month.replace("月",""))));
|
|
|
@ -241,20 +260,14 @@ public class ExcelUtil {
|
|
|
|
// dmXbjh.setDcCh(dcch);
|
|
|
|
// dmXbjh.setDcCh(dcch);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// dmXbjhs.add(dmXbjh);
|
|
|
|
// dmXbjhs.add(dmXbjh);
|
|
|
|
mapList.add(map);
|
|
|
|
mapList.add(map);
|
|
|
|
// map.put("cl"+year.replace("年","")+String.format("%02d", Integer.parseInt(month.replace("月",""))),cl);
|
|
|
|
// map.put("cl"+year.replace("年","")+String.format("%02d", Integer.parseInt(month.replace("月",""))),cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// mapList.add(map);
|
|
|
|
|
|
|
|
jsonObject.put(dcch,mapList);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return jsonObject;
|
|
|
|
// mapList.add(map);
|
|
|
|
// return mapList;
|
|
|
|
jsonObject.put(dcch,mapList);
|
|
|
|
// return dmXbjhs;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String date(String strDate){
|
|
|
|
public static String date(String strDate){
|
|
|
@ -300,6 +313,20 @@ public class ExcelUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return workbook;
|
|
|
|
return workbook;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Workbook isWorkBook(File file) throws IOException {
|
|
|
|
|
|
|
|
Workbook workbook;// = new XSSFWorkbook(inputStream);
|
|
|
|
|
|
|
|
// 截取路径名 . 后面的后缀名,判断是xls还是xlsx
|
|
|
|
|
|
|
|
// 如果这个判断不对,就把equals换成 equalsIgnoreCase()
|
|
|
|
|
|
|
|
Pattern pattern = Pattern.compile(".*\\.xls");
|
|
|
|
|
|
|
|
Matcher matcher = pattern.matcher(FileUtil.getSuffix(file.getName()));
|
|
|
|
|
|
|
|
if (matcher.matches()){
|
|
|
|
|
|
|
|
workbook = new HSSFWorkbook(FileUtil.getInputStream(file));
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
workbook = new XSSFWorkbook(FileUtil.getInputStream(file));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return workbook;
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取合并单元格的值
|
|
|
|
* 获取合并单元格的值
|
|
|
|
* @param sheet
|
|
|
|
* @param sheet
|
|
|
|