|
|
@@ -12,6 +12,7 @@ import com.usoftchina.saas.commons.service.ExcelService;
|
|
|
import com.usoftchina.saas.context.BaseContextHolder;
|
|
|
import com.usoftchina.saas.exception.BizException;
|
|
|
import com.usoftchina.saas.utils.CollectionUtils;
|
|
|
+import com.usoftchina.saas.utils.DateUtils;
|
|
|
import com.usoftchina.saas.utils.StringUtils;
|
|
|
import org.apache.poi.hssf.util.HSSFColor;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
@@ -45,6 +46,9 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
SXSSFWorkbook workbook = new SXSSFWorkbook();
|
|
|
DataTemplet dataTemplet = dataTempletMapper.selectByCaller(caller, companyId);
|
|
|
+ if (null == dataTemplet) {
|
|
|
+ throw new BizException(9876, "没有查询到对应的excel配置");
|
|
|
+ }
|
|
|
//列
|
|
|
String cols = dataTemplet.getDt_columns();
|
|
|
JSONArray array = (JSONArray) JSONArray.parse(cols);
|
|
|
@@ -70,7 +74,6 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Transactional
|
|
|
public Integer parseTemplet(Workbook wb, String caller) {
|
|
|
if (wb == null || StringUtils.isEmpty(caller)) {
|
|
|
throw new BizException(BizExceptionCode.NULL_DATA);
|
|
|
@@ -136,7 +139,6 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
StringBuilder err = new StringBuilder();
|
|
|
JSONArray mains = new JSONArray();
|
|
|
JSONArray details = new JSONArray();
|
|
|
- boolean flag = false;
|
|
|
//数据行循环
|
|
|
for (int i = 0; i < datas.size(); i++) {
|
|
|
Map<String,String> data = datas.get(i);
|
|
|
@@ -177,25 +179,21 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
if (difference) {
|
|
|
dd.setDd_codevalue(codeValue);
|
|
|
if ("true".equals(set.getNecessary()) && data.get("*" + set.getDescription()).equals("")) {
|
|
|
- err.append("第" + (i + 2) + "行 " + set.getDescription() + " 必填字段未填写! ");
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 必填字段未填写!<br/> ");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
//检测日期类型是否规范
|
|
|
- if ("date".equals(set.getType())) {
|
|
|
- flag = validateDateFormat(value);
|
|
|
- if (!flag) {
|
|
|
- err.append("第" + (i + 2) + "行 " + set.getDescription() + " 日期格式不正确! ");
|
|
|
+ if ("date".equals(set.getType()) && !StringUtils.isEmpty(value)) {
|
|
|
+ value = praseDate(value);
|
|
|
+ if (null == value) {
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 日期格式不正确!<br/> ");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
//插入主表数据
|
|
|
if (null != mainData) {
|
|
|
- if ("true".equals(set.getNecessary())) {
|
|
|
- mainData.put(set.getField(), value);
|
|
|
- } else {
|
|
|
- mainData.put(set.getField(), value);
|
|
|
- }
|
|
|
+ mainData.put(set.getField(), value);
|
|
|
}
|
|
|
}
|
|
|
//所有主表数据进行分类
|
|
|
@@ -214,24 +212,20 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
} else {
|
|
|
value = data.get(set.getDescription());
|
|
|
}
|
|
|
- //拼从表数据
|
|
|
- if ("true".equals(set.getNecessary())) {
|
|
|
- detailData.put(set.getField(), data.get("*" + set.getDescription()));
|
|
|
- } else {
|
|
|
- detailData.put(set.getField(), data.get(set.getDescription()));
|
|
|
- }
|
|
|
- if ("true".equals(set.getNecessary()) && data.get("*" + set.getDescription()).equals("")) {
|
|
|
- err.append("第" + (i + 2) + "行 " + set.getDescription() + " 必填字段未填写! ");
|
|
|
+ if ("true".equals(set.getNecessary()) && value.equals("")) {
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 必填字段未填写!<br/> ");
|
|
|
break;
|
|
|
}
|
|
|
//检测日期类型是否规范
|
|
|
- if ("date".equals(set.getType())) {
|
|
|
- flag = validateDateFormat(value);
|
|
|
- if (!flag) {
|
|
|
- err.append("第" + (i + 2) + "行 " + set.getDescription() + " 日期格式不正确! ");
|
|
|
+ if ("date".equals(set.getType()) && !StringUtils.isEmpty(value)) {
|
|
|
+ value = praseDate(value);
|
|
|
+ if (null == value) {
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 日期格式不正确!<br/> ");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+ //拼从表数据
|
|
|
+ detailData.put(set.getField(), value);
|
|
|
}
|
|
|
if (detailData.size() > 0) {
|
|
|
dd.setDd_codevalue(codeValue);
|
|
|
@@ -257,7 +251,7 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
if (StringUtils.isEmpty(date)) {
|
|
|
return false;
|
|
|
}
|
|
|
- String regEx1 = "[0-9]{8}";
|
|
|
+ String regEx1 = "[0-9]{4}/[0-9]{2}/[0-9]{2}";
|
|
|
String regEX2 = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
|
|
|
// 编译正则表达式
|
|
|
Pattern pattern1 = Pattern.compile(regEx1);
|
|
|
@@ -271,6 +265,15 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
return flag;
|
|
|
}
|
|
|
|
|
|
+ private String praseDate(String num) {
|
|
|
+ try {
|
|
|
+ Integer days = Integer.valueOf(num);
|
|
|
+ return DateUtils.plusDay(days, "1899-12-30");
|
|
|
+ }catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//保证先遍历主表
|
|
|
private List<String> SetToList(Set<String> sets) {
|
|
|
List<String> list = new ArrayList<>();
|
|
|
@@ -290,6 +293,10 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
switch(cell.getCellType()){
|
|
|
case Cell.CELL_TYPE_NUMERIC:{
|
|
|
cellValue = String.valueOf(cell.getNumericCellValue());
|
|
|
+ //判断是否为INT类型
|
|
|
+ if (Double.valueOf(cellValue.toString()).intValue() - Double.valueOf(cellValue.toString()) == 0) {
|
|
|
+ return cellValue.toString().substring(0, cellValue.toString().indexOf("."));
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
case Cell.CELL_TYPE_FORMULA:{
|
|
|
@@ -336,9 +343,10 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
*/
|
|
|
private void createWorkbook(SXSSFWorkbook workbook, int sheetIdx, JSONArray cols, JSONArray datas, String remark){
|
|
|
Sheet sheet = workbook.createSheet("sheet" + sheetIdx);
|
|
|
- CellStyle style = getCellStyle(workbook);
|
|
|
- //sheet.autoSizeColumn((short) 2);
|
|
|
- sheet.createFreezePane(0, 1);// 固定列
|
|
|
+ CellStyle style = getCellStyle(workbook, true);
|
|
|
+ CellStyle detailStyle = getCellStyle(workbook, false);
|
|
|
+ //sheet.autoSizeColumn(2);
|
|
|
+ // sheet.createFreezePane(0, 1);// 固定列
|
|
|
Cell cell = null;
|
|
|
int rIdx = 0;
|
|
|
int cIdx = 0;
|
|
|
@@ -347,8 +355,8 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
Row row = null;
|
|
|
if (remark != null) {
|
|
|
row = sheet.createRow(rIdx);
|
|
|
- row.setHeightInPoints((short) 100);
|
|
|
- sheet.setColumnWidth(cIdx, 1000);
|
|
|
+ row.setHeightInPoints((short) 50);
|
|
|
+ sheet.setColumnWidth(cIdx, 500);
|
|
|
cell = getCell(sheet, rIdx, cIdx);
|
|
|
cell.setCellValue(remark);
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, cols.size() - 1));
|
|
|
@@ -365,19 +373,23 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
width = (short) (obj.get("width") == null ? 0 : (int)obj.get("width")*35.7);
|
|
|
width = width == 0 ? 4000 : width;
|
|
|
sheet.setColumnWidth(cIdx, width);
|
|
|
- sheet.createFreezePane(cols.size() + 1, 2);
|
|
|
+ // sheet.createFreezePane(cols.size() + 1, 2);
|
|
|
cell = getCell(sheet, rIdx, cIdx);
|
|
|
value = obj.get("description") == null ? value : obj.get("description").toString();
|
|
|
if ("true".equals(obj.get("necessary"))) {
|
|
|
value = "*" + value;
|
|
|
}
|
|
|
- cell.setCellValue(value);
|
|
|
- keys.add(String.valueOf(obj.get("description")));
|
|
|
if ("date".equals(obj.get("type"))) {
|
|
|
short df= workbook.createDataFormat().getFormat("yyyy-MM-dd");
|
|
|
style.setDataFormat(df);
|
|
|
}
|
|
|
- cell.setCellStyle(style);
|
|
|
+ if ("main".equals(obj.get("position"))) {
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ } else {
|
|
|
+ cell.setCellStyle(detailStyle);
|
|
|
+ }
|
|
|
+ cell.setCellValue(value);
|
|
|
+ keys.add(String.valueOf(obj.get("description")));
|
|
|
cIdx++;
|
|
|
value = "";
|
|
|
}
|
|
|
@@ -410,7 +422,7 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private CellStyle getCellStyle(SXSSFWorkbook workbook) {
|
|
|
+ private CellStyle getCellStyle(SXSSFWorkbook workbook, boolean main) {
|
|
|
CellStyle style = workbook.createCellStyle();
|
|
|
style.setFillBackgroundColor((short) 18);
|
|
|
style.setFillPattern(FillPatternType.LEAST_DOTS);
|
|
|
@@ -421,7 +433,7 @@ public class ExcelServiceImpl implements ExcelService{
|
|
|
style.setFont(font);
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
style.setFillForegroundColor(HSSFColor.GREEN.index);
|
|
|
- style.setFillBackgroundColor(HSSFColor.PALE_BLUE.index);
|
|
|
+ style.setFillBackgroundColor(main ? HSSFColor.LIGHT_YELLOW.index : HSSFColor.PALE_BLUE.index);
|
|
|
style.setBorderBottom(BorderStyle.MEDIUM);
|
|
|
style.setBorderLeft(BorderStyle.MEDIUM);
|
|
|
style.setBorderRight(BorderStyle.MEDIUM);
|