diff --git a/WebRoot/Excel/10871f6a-3e8d-4dae-8cdf-cea75ba5976e.xlsx b/WebRoot/Excel/10871f6a-3e8d-4dae-8cdf-cea75ba5976e.xlsx new file mode 100644 index 00000000..303aac25 Binary files /dev/null and b/WebRoot/Excel/10871f6a-3e8d-4dae-8cdf-cea75ba5976e.xlsx differ diff --git a/WebRoot/Excel/13b7e863-8dda-4746-b435-b8b31a56dc93.xlsx b/WebRoot/Excel/13b7e863-8dda-4746-b435-b8b31a56dc93.xlsx new file mode 100644 index 00000000..6b6ca485 Binary files /dev/null and b/WebRoot/Excel/13b7e863-8dda-4746-b435-b8b31a56dc93.xlsx differ diff --git a/WebRoot/Excel/1b1a53fa-8dda-4ea9-b74a-6582de99abf9.xlsx b/WebRoot/Excel/1b1a53fa-8dda-4ea9-b74a-6582de99abf9.xlsx new file mode 100644 index 00000000..32882551 Binary files /dev/null and b/WebRoot/Excel/1b1a53fa-8dda-4ea9-b74a-6582de99abf9.xlsx differ diff --git a/WebRoot/Excel/45cb1c45-d3ce-4f79-8da0-5b639f0d8a8e.xlsx b/WebRoot/Excel/45cb1c45-d3ce-4f79-8da0-5b639f0d8a8e.xlsx new file mode 100644 index 00000000..c79b697e Binary files /dev/null and b/WebRoot/Excel/45cb1c45-d3ce-4f79-8da0-5b639f0d8a8e.xlsx differ diff --git a/WebRoot/Excel/4e709f90-ca72-4752-be6a-43784ba5a04f.xlsx b/WebRoot/Excel/4e709f90-ca72-4752-be6a-43784ba5a04f.xlsx new file mode 100644 index 00000000..cd440020 Binary files /dev/null and b/WebRoot/Excel/4e709f90-ca72-4752-be6a-43784ba5a04f.xlsx differ diff --git a/WebRoot/Excel/67208304-ef01-4185-85cb-adbf2c6533ea.xlsx b/WebRoot/Excel/67208304-ef01-4185-85cb-adbf2c6533ea.xlsx new file mode 100644 index 00000000..29433b7b Binary files /dev/null and b/WebRoot/Excel/67208304-ef01-4185-85cb-adbf2c6533ea.xlsx differ diff --git a/WebRoot/Excel/6caf9fce-a1e1-4ea8-8eb9-c135a60427fa.xlsx b/WebRoot/Excel/6caf9fce-a1e1-4ea8-8eb9-c135a60427fa.xlsx new file mode 100644 index 00000000..59cc4625 Binary files /dev/null and b/WebRoot/Excel/6caf9fce-a1e1-4ea8-8eb9-c135a60427fa.xlsx differ diff --git a/WebRoot/Excel/79ed2cdd-2e7c-47d3-a96d-d6ab90fca105.xlsx b/WebRoot/Excel/79ed2cdd-2e7c-47d3-a96d-d6ab90fca105.xlsx new file mode 100644 index 00000000..d95ddbe7 Binary files /dev/null and b/WebRoot/Excel/79ed2cdd-2e7c-47d3-a96d-d6ab90fca105.xlsx differ diff --git a/WebRoot/Excel/7ebee281-880a-49ab-a1bc-af599e7daa9a.xlsx b/WebRoot/Excel/7ebee281-880a-49ab-a1bc-af599e7daa9a.xlsx new file mode 100644 index 00000000..62988b92 Binary files /dev/null and b/WebRoot/Excel/7ebee281-880a-49ab-a1bc-af599e7daa9a.xlsx differ diff --git a/WebRoot/Excel/9fef50fb-471a-453d-b5d3-378d32fc5a3e.xlsx b/WebRoot/Excel/9fef50fb-471a-453d-b5d3-378d32fc5a3e.xlsx new file mode 100644 index 00000000..9154b419 Binary files /dev/null and b/WebRoot/Excel/9fef50fb-471a-453d-b5d3-378d32fc5a3e.xlsx differ diff --git a/WebRoot/Excel/a6a4e3e9-782c-4f82-9b5a-f1cad672e266.xlsx b/WebRoot/Excel/a6a4e3e9-782c-4f82-9b5a-f1cad672e266.xlsx new file mode 100644 index 00000000..6b6ca485 Binary files /dev/null and b/WebRoot/Excel/a6a4e3e9-782c-4f82-9b5a-f1cad672e266.xlsx differ diff --git a/WebRoot/Excel/b005eaf7-8e4d-4451-ae60-1b571cdd35a6.xlsx b/WebRoot/Excel/b005eaf7-8e4d-4451-ae60-1b571cdd35a6.xlsx new file mode 100644 index 00000000..6b6ca485 Binary files /dev/null and b/WebRoot/Excel/b005eaf7-8e4d-4451-ae60-1b571cdd35a6.xlsx differ diff --git a/WebRoot/Excel/cb4c5c90-066f-47e6-91ab-cdf0062aec17.xlsx b/WebRoot/Excel/cb4c5c90-066f-47e6-91ab-cdf0062aec17.xlsx new file mode 100644 index 00000000..6d6dc2b5 Binary files /dev/null and b/WebRoot/Excel/cb4c5c90-066f-47e6-91ab-cdf0062aec17.xlsx differ diff --git a/WebRoot/Excel/ee2bdd2f-f029-44e5-9b48-2baa734b32c6.xlsx b/WebRoot/Excel/ee2bdd2f-f029-44e5-9b48-2baa734b32c6.xlsx new file mode 100644 index 00000000..9076747e Binary files /dev/null and b/WebRoot/Excel/ee2bdd2f-f029-44e5-9b48-2baa734b32c6.xlsx differ diff --git a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java index 2943a788..45a8762d 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java @@ -195,6 +195,34 @@ public class CollectController extends Controller { renderJson(kv); return; } + + //检查字段名称 + String errMsg = ""; + int idx = 1; + for (Object o : ja) { + JSONObject jo = (JSONObject) o; + String column_name = jo.getString("column_name"); + if (StrKit.isBlank(column_name)) { + errMsg += "第" + idx + "列的列名为空!\n"; + continue; + } + if (column_name.length() > 32) { + errMsg += "第" + idx + "列的列名长度大于32位!\n"; + continue; + } + if (column_name.matches("[a-z][a-z0-9_]*")) { + errMsg += "第" + idx + "列的列名只能是小写字母或者数字、下划线,并且列名不能以数字开头!\n"; + continue; + } + idx++; + } + if (!StrKit.isBlank(errMsg)) { + Map map = new HashMap(); + map.put("success", false); + map.put("message", errMsg); + renderJson(map); + } + //保存 List writeList = new ArrayList<>(); for (Object o : ja) { @@ -284,15 +312,36 @@ public class CollectController extends Controller { int colIdx = 0; for (Record record : list) { boolean allow_blank = record.getBoolean("allow_blank"); + XSSFCell cell = wb.getSheetAt(i).getRow(end_row).getCell(colIdx); + if (!allow_blank) { //一行时,星号加在一行 //二行时,星号加在二行 - XSSFCell cell = wb.getSheetAt(i).getRow(end_row).getCell(colIdx); if (StrKit.isBlank(cell.getStringCellValue())) { cell = wb.getSheetAt(i).getRow(start_row).getCell(colIdx); } PoiUtil.addStar(wb, cell); - PoiUtil.addComment(wb, cell, "此单元格内容必须输入!"); + PoiUtil.addComment(wb, cell, "此列内容必须输入!"); + } + + int data_type_id = record.getInt("data_type_id"); + if (data_type_id == 2) { + if (!allow_blank) PoiUtil.addComment(wb, cell, "此列内容必须输入,并且需要输入整数!"); + else PoiUtil.addComment(wb, cell, "此列需要输入整数!"); + } + if (data_type_id == 3) { + if (!allow_blank) { + PoiUtil.addComment(wb, cell, "此列内容必须输入,并且需要输入小数!"); + } else { + PoiUtil.addComment(wb, cell, "此列需要输入小数!"); + } + } + if (data_type_id == 4) { + if (!allow_blank) { + PoiUtil.addComment(wb, cell, "此列内容必须输入,并且需要输入日期,格式为yyyy/mm/dd!"); + } else { + PoiUtil.addComment(wb, cell, "此列需要输入日期,格式为yyyy/mm/dd!"); + } } colIdx++; } @@ -312,10 +361,16 @@ public class CollectController extends Controller { //2 数值 //4 日期(年-月-日) //3 数值(小数) -// int data_type_id = record.getInt("data_type_id"); -// if (data_type_id == 2) PoiUtil.setIntegerStyle(wb, i, colIdx, data_start_row, data_start_row + 20000); -// if (data_type_id == 3) PoiUtil.setFloatStyle(wb, i, colIdx, data_start_row, data_start_row + 20000); -// if (data_type_id == 4) PoiUtil.setDateStyle(wb, i, colIdx, data_start_row, data_start_row + 20000); + int data_type_id = record.getInt("data_type_id"); + if (data_type_id == 2) { + PoiUtil.setIntegerStyle(wb, i, colIdx, data_start_row, data_start_row + 20000); + } + if (data_type_id == 3) { + PoiUtil.setFloatStyle(wb, i, colIdx, data_start_row, data_start_row + 20000); + } + if (data_type_id == 4) { + PoiUtil.setDateStyle(wb, i, colIdx, data_start_row, data_start_row + 20000); + } colIdx++; } } diff --git a/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java b/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java index 8a943607..9ed9d661 100644 --- a/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java +++ b/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java @@ -1,11 +1,14 @@ package com.dsideal.QingLong.Util; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.xssf.usermodel.*; +import java.io.*; import java.text.SimpleDateFormat; import java.util.*; @@ -283,23 +286,21 @@ public class PoiUtil { * @param firstRow * @param lastRow */ - public static void setIntegerStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { - // 设置整数类型的样式 - CellStyle integerStyle = wb.createCellStyle(); - integerStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0")); // 设置为整数类型 - - for (int i = firstRow; i <= lastRow; i++) { - Row row = wb.getSheetAt(sheetIdx).getRow(i); - if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i); - // 设置整数类型的值 - Cell integerCell = row.getCell(colIdx); - if (integerCell == null) { - integerCell = row.createCell(colIdx); - } - integerCell.setCellStyle(integerStyle); // 应用整数类型的样式 - } + public static void setIntegerStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { + XSSFSheet sheet = wb.getSheetAt(sheetIdx); + XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet); + XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint( + DataValidationConstraint.ValidationType.INTEGER, XSSFDataValidationConstraint.OperatorType.BETWEEN, + String.valueOf(Integer.MIN_VALUE), String.valueOf(Integer.MAX_VALUE) + ); + CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx); + XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList); + validation.setSuppressDropDownArrow(false); + validation.setShowErrorBox(true); + sheet.addValidationData(validation); } + /** * 功能:设置浮点数类型格式 * @@ -309,20 +310,18 @@ public class PoiUtil { * @param firstRow * @param lastRow */ - public static void setFloatStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { - // 设置浮点数类型的样式 - CellStyle floatStyle = wb.createCellStyle(); - floatStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00")); // 设置为浮点数类型,保留两位小数 - for (int i = firstRow; i <= lastRow; i++) { - Row row = wb.getSheetAt(sheetIdx).getRow(i); - if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i); - // 设置浮点数类型的值 - Cell floatCell = row.getCell(colIdx); - if (floatCell == null) { - floatCell = row.createCell(colIdx); - } - floatCell.setCellStyle(floatStyle); // 应用浮点数类型的样式 - } + public static void setFloatStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { + XSSFSheet sheet = wb.getSheetAt(sheetIdx); + XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet); + XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint( + DataValidationConstraint.ValidationType.DECIMAL, XSSFDataValidationConstraint.OperatorType.BETWEEN, + String.valueOf(Float.MIN_VALUE), String.valueOf(Float.MAX_VALUE) + ); + CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx); + XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList); + validation.setSuppressDropDownArrow(false); + validation.setShowErrorBox(true); + sheet.addValidationData(validation); } /** @@ -334,19 +333,32 @@ public class PoiUtil { * @param firstRow * @param lastRow */ - public static void setDateStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { - // 设置日期类型的样式 - CellStyle dateStyle = wb.createCellStyle(); - dateStyle.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("yyyy-mm-dd")); // 设置为日期类型,格式为yyyy-mm-dd - for (int i = firstRow; i <= lastRow; i++) { - Row row = wb.getSheetAt(sheetIdx).getRow(i); - if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i); - // 设置日期类型的值 - Cell dateCell = row.getCell(colIdx); - if (dateCell == null) { - dateCell = row.createCell(colIdx); - } - dateCell.setCellStyle(dateStyle); // 应用日期类型的样式 - } + public static void setDateStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { + XSSFSheet sheet = wb.getSheetAt(sheetIdx); + XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet); + DataValidationConstraint dvConstraint = dvHelper + .createDateConstraint(DataValidationConstraint.OperatorType.BETWEEN, "date(1900,1,1)", "date(2099,12,31)", "yyyy/MM/dd"); + CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx); + XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList); + validation.setSuppressDropDownArrow(false); + validation.setShowErrorBox(true); + sheet.addValidationData(validation); + } + + public static void main(String[] args) throws IOException { + InputStream is = new FileInputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\sheet.xlsx"); + XSSFWorkbook wb = new XSSFWorkbook(is); + + setIntegerStyle(wb, 0, 2, 0, 20000); + + setFloatStyle(wb, 0, 3, 0, 20000); + + setDateStyle(wb,0,4,0,20000); + //保存 + FileOutputStream fileOut = new FileOutputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx"); + wb.write(fileOut); + //关闭Excel + wb.close(); + } } diff --git a/src/main/resource/Excel/b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx b/src/main/resource/Excel/b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx new file mode 100644 index 00000000..b8b9dd4a Binary files /dev/null and b/src/main/resource/Excel/b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx differ diff --git a/src/main/resource/Excel/sheet.xlsx b/src/main/resource/Excel/sheet.xlsx new file mode 100644 index 00000000..fd7feff1 Binary files /dev/null and b/src/main/resource/Excel/sheet.xlsx differ