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 d584cd9c..1eb23aa0 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java @@ -12,6 +12,7 @@ import com.dsideal.QingLong.Interceptor.LayUiPageInfoInterface; import com.dsideal.QingLong.LoginPerson.Model.LoginPersonModel; import com.dsideal.QingLong.Util.AsposeUtil; import com.dsideal.QingLong.Util.CommonUtil; +import com.dsideal.QingLong.Util.PoiUtil; import com.dsideal.QingLong.Util.SessionKit; import com.jfinal.aop.Before; import com.jfinal.core.Controller; @@ -286,8 +287,8 @@ public class CollectController extends Controller { if (StrKit.isBlank(cell.getStringCellValue())) { cell = wb.getSheetAt(i).getRow(start_row).getCell(colIdx); } - cm.addStar(wb, cell); - cm.addComment(wb, cell, "此单元格内容必须输入!"); + PoiUtil.addStar(wb, cell); + PoiUtil.addComment(wb, cell, "此单元格内容必须输入!"); } colIdx++; } @@ -301,7 +302,7 @@ public class CollectController extends Controller { for (Record record : list) { String options = record.getStr("options"); if (!StrKit.isBlank(options)) { - cm.addValidation(wb, i, options, data_start_row, data_start_row + 20000, colIdx, colIdx);//范围 + PoiUtil.addValidation(wb, i, options, data_start_row, data_start_row + 20000, colIdx, colIdx);//范围 } colIdx++; } @@ -448,12 +449,12 @@ public class CollectController extends Controller { //Sheet表 XSSFSheet sheet = wb.getSheetAt(i); //移除所有批注 - cm.RemoveAllComment(sheet); + PoiUtil.RemoveAllComment(sheet); //数据起始行 Record r = cm.getSheetConfig(upload_excel_filename, i); int data_start_row = r.getInt("data_start_row"); //恢复背景色 - cm.resetStyle(wb, sheet, data_start_row); + PoiUtil.resetStyle(wb, sheet, data_start_row); //数据有效行数 int lastRowNum = sheet.getLastRowNum(); @@ -469,9 +470,9 @@ public class CollectController extends Controller { // 非空项目未填写 for (int j = data_start_row; j <= lastRowNum; j++) {//行 XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx); - if (StrKit.isBlank(cm.getValue(cell).toString())) { - cm.addComment(wb, cell, "此处不能为空!"); - cm.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); + if (StrKit.isBlank(PoiUtil.getValue(cell).toString())) { + PoiUtil.addComment(wb, cell, "此处不能为空!"); + PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); flag = false; } } @@ -480,11 +481,11 @@ public class CollectController extends Controller { if (!StrKit.isBlank(options)) { for (int j = data_start_row; j <= lastRowNum; j++) {//行 XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx); - String value = cm.getValue(cell).toString(); + String value = PoiUtil.getValue(cell).toString(); if (StrKit.isBlank(value)) value = "&*^&Y&*(&*(&*()*("; if (options.indexOf(value) < 0) { - cm.addComment(wb, cell, "未从指定范围内选择有效值!"); - cm.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); + PoiUtil.addComment(wb, cell, "未从指定范围内选择有效值!"); + PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); flag = false; } } @@ -492,19 +493,19 @@ public class CollectController extends Controller { // 检查数据类型是不是和规定的不兼容 for (int j = data_start_row; j <= lastRowNum; j++) {//行 XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx); - Object obj = cm.getValue(cell); + Object obj = PoiUtil.getValue(cell); if (column_type.equals("Integer") && !CommonUtil.isInteger(obj.toString())) {//要求整数,实际不是整数 - cm.addComment(wb, cell, "要求是整数,实际数据类型不是整数!"); - cm.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); + PoiUtil.addComment(wb, cell, "要求是整数,实际数据类型不是整数!"); + PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); flag = false; } else if (column_type.equals("Double") && !CommonUtil.isDouble(obj.toString())) {//要求是浮点数,实际不是浮点数 - cm.addComment(wb, cell, "要求是小数,实际数据类型不是小数!"); - cm.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); + PoiUtil.addComment(wb, cell, "要求是小数,实际数据类型不是小数!"); + PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); flag = false; } else if (column_type.equals("Date") && !CommonUtil.isDate(obj.toString())) {//要求是日期,实际不是日期 - cm.addComment(wb, cell, "要求是日期格式,实际数据类型不是日期格式!"); - cm.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); + PoiUtil.addComment(wb, cell, "要求是日期格式,实际数据类型不是日期格式!"); + PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); flag = false; } } diff --git a/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java b/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java index bb390b83..bd8bd1b0 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java @@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject; import com.dsideal.QingLong.Collect.Const.DataTypeConst; import com.dsideal.QingLong.Util.ChineseCharacterUtil; import com.dsideal.QingLong.Util.CommonUtil; +import com.dsideal.QingLong.Util.PoiUtil; import com.jfinal.kit.Kv; import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.Db; @@ -485,11 +486,11 @@ public class CollectModel { String colName = _map.get(j).getStr("column_name"); //cell可能是被合并的单元格 - if (isMerged(sheet, cell)) { - String value = getValue(cell).toString(); + if (PoiUtil.isMerged(sheet, cell)) { + String value = PoiUtil.getValue(cell).toString(); if (StrKit.isBlank(value)) { for (int k = i - 1; ; k--) { - String prev = getValue(sheet.getRow(k).getCell(j)).toString(); + String prev = PoiUtil.getValue(sheet.getRow(k).getCell(j)).toString(); if (!StrKit.isBlank(prev)) { value = prev; break; @@ -499,13 +500,13 @@ public class CollectModel { writeRecord.set(colName, value); } else { if (colType.equals("Integer")) - writeRecord.set(colName, (int) Double.parseDouble(getValue(cell).toString())); + writeRecord.set(colName, (int) Double.parseDouble(PoiUtil.getValue(cell).toString())); else if (colType.equals("String")) - writeRecord.set(colName, getValue(cell).toString()); + writeRecord.set(colName, PoiUtil.getValue(cell).toString()); else if (colType.equals("Double")) - writeRecord.set(colName, Double.parseDouble(getValue(cell).toString())); + writeRecord.set(colName, Double.parseDouble(PoiUtil.getValue(cell).toString())); else if (colType.equals("Date")) { - String dateString = getValue(cell).toString(); + String dateString = PoiUtil.getValue(cell).toString(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = dateFormat.parse(dateString); writeRecord.set(colName, date); @@ -703,212 +704,6 @@ public class CollectModel { return Db.findFirst(sql, job_id, bureau_id); } - /** - * 功能:对于指定Cell中文字进行加红色(*)提醒 - * - * @param wb - * @param cell - */ - public void addStar(XSSFWorkbook wb, XSSFCell cell) { - XSSFFont fontRed = wb.createFont();//第一种字体 - fontRed.setBold(true); - fontRed.setColor(IndexedColors.RED.getIndex()); - fontRed.setFontHeightInPoints((short) 14); - fontRed.setFontName("黑体"); - - XSSFFont fontBlack = wb.createFont();//第二种字体 - fontBlack.setColor(IndexedColors.BLACK.getIndex()); - fontBlack.setFontName("宋体"); - fontRed.setBold(true); - fontBlack.setFontHeightInPoints((short) 12); - String txt = cell.getStringCellValue(); - - int len = txt.length(); - if (len == 0) { - System.out.println(cell.getSheet().getSheetName()); - System.out.println(cell.getRowIndex() + " " + cell.getColumnIndex()); - System.out.println(cell.getStringCellValue()); - System.out.println("发现问题:" + txt); - System.exit(0); - } - - txt += "(*)"; - XSSFRichTextString richText = new XSSFRichTextString(txt);//全部文字 - - richText.applyFont(0, len - 1, fontBlack); // 从第0个字符到第2个字符应用font1 - richText.applyFont(len, len + 3, fontRed); // 从第2个字符到第4个字符应用font2 - cell.setCellValue(richText);//设置文字 - } - - /** - * 功能:给指定单元格加边框 - * - * @param wb - * @param cell - */ - public void fillColor(Workbook wb, Cell cell, short colorIdx) { - // 创建单元格样式 - CellStyle style = wb.createCellStyle(); - style.setAlignment(HorizontalAlignment.CENTER); - style.setVerticalAlignment(VerticalAlignment.CENTER); - style.setBorderTop(BorderStyle.THIN); - style.setBorderBottom(BorderStyle.THIN); - style.setBorderLeft(BorderStyle.THIN); - style.setBorderRight(BorderStyle.THIN); - // 创建字体样式 - Font font = wb.createFont(); - font.setFontName("宋体");//字体 - font.setBold(true);//加粗 - font.setFontHeightInPoints((short) 12);//字号 - style.setFont(font); - // 设置背景颜色 - style.setFillForegroundColor(colorIdx); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - // 应用样式到单元格 - cell.setCellStyle(style); - } - - /** - * 功能:移除所有批注信息 - * - * @param sheet - */ - public void RemoveAllComment(XSSFSheet sheet) { - // 遍历所有行和单元格,移除批注 - for (Row row : sheet) { - for (Cell cell : row) { - if (cell.getCellComment() != null) { - cell.removeCellComment(); - } - } - } - } - - /** - * 功能:将指定Sheet表中所有数据区域进行样式还原 - * - * @param wb - * @param sheet - */ - public void resetStyle(XSSFWorkbook wb, XSSFSheet sheet, int dataStartRow) { - // 遍历所有行和单元格,移除批注 - for (int row = dataStartRow; row <= sheet.getLastRowNum(); row++) { - for (int col = 0; col < sheet.getRow(row).getLastCellNum(); col++) { - Cell cell = sheet.getRow(row).getCell(col); - fillColor(wb, cell, IndexedColors.WHITE.getIndex()); - } - } - } - - /** - * 功能:修改指定单元格的样式 - * - * @param wb - * @return - */ - public void addComment(Workbook wb, Cell cell, String str) { - Sheet sheet = cell.getSheet(); - // 判断单元格上是否存在批注 - if (cell.getCellComment() != null) { - // 如果存在批注,先删除 - cell.removeCellComment(); - } - // 创建批注 - Drawing drawing = sheet.createDrawingPatriarch(); - CreationHelper factory = wb.getCreationHelper(); - ClientAnchor anchor = factory.createClientAnchor(); - anchor.setCol1(cell.getColumnIndex()); - anchor.setCol2(cell.getColumnIndex() + 2); - anchor.setRow1(cell.getRow().getRowNum()); - anchor.setRow2(cell.getRow().getRowNum() + 3); - Comment comment = drawing.createCellComment(anchor); - comment.setString(factory.createRichTextString(str)); - // 将批注添加到单元格 - cell.setCellComment(comment); - } - - /** - * 功能:在EXCEL的指定范围内添加上下拉框,内容通过String[]提供 - * - * @param workbook - * @param sheetIdx - * @param firstRow - * @param lastRow - * @param firstCol - * @param lastCol - * @return - */ - public void addValidation(Workbook workbook, int sheetIdx, String options, int firstRow, int lastRow, int firstCol, int lastCol) { - String[] optionArray = options.split(","); - Sheet sheet = workbook.getSheetAt(sheetIdx); - - // 删除已有的校验 - DataValidationHelper dvHelper = sheet.getDataValidationHelper(); - DataValidationConstraint dvConstraint = dvHelper.createCustomConstraint("DELETE_ALL"); - CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); // 设置一个范围,例如整个表格范围 - DataValidation validation = dvHelper.createValidation(dvConstraint, addressList); - validation.setSuppressDropDownArrow(true); - sheet.addValidationData(validation); - - // 创建数据验证对象 - DataValidationHelper validationHelper = sheet.getDataValidationHelper(); - DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(optionArray); - addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); // 指定单元格范围 - validation = validationHelper.createValidation(constraint, addressList); - // 应用数据验证到单元格 - sheet.addValidationData(validation); - } - - /** - * 功能:获取指定Cell的内容值 - * - * @param cell - * @return - */ - public Object getValue(XSSFCell cell) { - switch (cell.getCellType()) { - case CellType.STRING: - String cellValueString = cell.getStringCellValue(); - return cellValueString; - case CellType.NUMERIC: - //日期格式 - if (DateUtil.isCellDateFormatted(cell)) { - Date dateValue = cell.getDateCellValue(); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - String formattedDate = sdf.format(dateValue); - return formattedDate; - } - double cellValueNumeric = cell.getNumericCellValue(); - return cellValueNumeric; - case CellType.BOOLEAN: - boolean cellValueBoolean = cell.getBooleanCellValue(); - return cellValueBoolean; - case CellType.BLANK: - cellValueString = cell.getStringCellValue(); - return cellValueString; - // 其他类型的处理 - default: - cellValueString = cell.getStringCellValue(); - return cellValueString; - } - } - - /** - * 功能:判断一个单元格是不是被合并了 - * - * @param sheet - * @return - */ - public boolean isMerged(XSSFSheet sheet, XSSFCell cell) { - // 判断单元格是否被合并 - for (CellRangeAddress range : sheet.getMergedRegions()) { - if (cell.getRowIndex() >= range.getFirstRow() && cell.getRowIndex() <= range.getLastRow() - && cell.getColumnIndex() >= range.getFirstColumn() && cell.getColumnIndex() <= range.getLastColumn()) { - return true; - } - } - return false; - } /** * 功能:获取任务列表,支持关键字查询 diff --git a/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java b/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java new file mode 100644 index 00000000..d460990f --- /dev/null +++ b/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java @@ -0,0 +1,220 @@ +package com.dsideal.QingLong.Util; + +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.text.SimpleDateFormat; +import java.util.Date; + +public class PoiUtil { + + /** + * 功能:对于指定Cell中文字进行加红色(*)提醒 + * + * @param wb + * @param cell + */ + public static void addStar(XSSFWorkbook wb, XSSFCell cell) { + XSSFFont fontRed = wb.createFont();//第一种字体 + fontRed.setBold(true); + fontRed.setColor(IndexedColors.RED.getIndex()); + fontRed.setFontHeightInPoints((short) 14); + fontRed.setFontName("黑体"); + + XSSFFont fontBlack = wb.createFont();//第二种字体 + fontBlack.setColor(IndexedColors.BLACK.getIndex()); + fontBlack.setFontName("宋体"); + fontRed.setBold(true); + fontBlack.setFontHeightInPoints((short) 12); + String txt = cell.getStringCellValue(); + + int len = txt.length(); + if (len == 0) { + System.out.println(cell.getSheet().getSheetName()); + System.out.println(cell.getRowIndex() + " " + cell.getColumnIndex()); + System.out.println(cell.getStringCellValue()); + System.out.println("发现问题:" + txt); + System.exit(0); + } + + txt += "(*)"; + XSSFRichTextString richText = new XSSFRichTextString(txt);//全部文字 + + richText.applyFont(0, len - 1, fontBlack); // 从第0个字符到第2个字符应用font1 + richText.applyFont(len, len + 3, fontRed); // 从第2个字符到第4个字符应用font2 + cell.setCellValue(richText);//设置文字 + } + + /** + * 功能:给指定单元格加边框 + * + * @param wb + * @param cell + */ + public static void fillColor(Workbook wb, Cell cell, short colorIdx) { + // 创建单元格样式 + CellStyle style = wb.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + style.setBorderTop(BorderStyle.THIN); + style.setBorderBottom(BorderStyle.THIN); + style.setBorderLeft(BorderStyle.THIN); + style.setBorderRight(BorderStyle.THIN); + // 创建字体样式 + Font font = wb.createFont(); + font.setFontName("宋体");//字体 + font.setBold(true);//加粗 + font.setFontHeightInPoints((short) 12);//字号 + style.setFont(font); + // 设置背景颜色 + style.setFillForegroundColor(colorIdx); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + // 应用样式到单元格 + cell.setCellStyle(style); + } + + /** + * 功能:移除所有批注信息 + * + * @param sheet + */ + public static void RemoveAllComment(XSSFSheet sheet) { + // 遍历所有行和单元格,移除批注 + for (Row row : sheet) { + for (Cell cell : row) { + if (cell.getCellComment() != null) { + cell.removeCellComment(); + } + } + } + } + + /** + * 功能:将指定Sheet表中所有数据区域进行样式还原 + * + * @param wb + * @param sheet + */ + public static void resetStyle(XSSFWorkbook wb, XSSFSheet sheet, int dataStartRow) { + // 遍历所有行和单元格,移除批注 + for (int row = dataStartRow; row <= sheet.getLastRowNum(); row++) { + for (int col = 0; col < sheet.getRow(row).getLastCellNum(); col++) { + Cell cell = sheet.getRow(row).getCell(col); + fillColor(wb, cell, IndexedColors.WHITE.getIndex()); + } + } + } + + /** + * 功能:修改指定单元格的样式 + * + * @param wb + * @return + */ + public static void addComment(Workbook wb, Cell cell, String str) { + Sheet sheet = cell.getSheet(); + // 判断单元格上是否存在批注 + if (cell.getCellComment() != null) { + // 如果存在批注,先删除 + cell.removeCellComment(); + } + // 创建批注 + Drawing drawing = sheet.createDrawingPatriarch(); + CreationHelper factory = wb.getCreationHelper(); + ClientAnchor anchor = factory.createClientAnchor(); + anchor.setCol1(cell.getColumnIndex()); + anchor.setCol2(cell.getColumnIndex() + 2); + anchor.setRow1(cell.getRow().getRowNum()); + anchor.setRow2(cell.getRow().getRowNum() + 3); + Comment comment = drawing.createCellComment(anchor); + comment.setString(factory.createRichTextString(str)); + // 将批注添加到单元格 + cell.setCellComment(comment); + } + + /** + * 功能:在EXCEL的指定范围内添加上下拉框,内容通过String[]提供 + * + * @param workbook + * @param sheetIdx + * @param firstRow + * @param lastRow + * @param firstCol + * @param lastCol + * @return + */ + public static void addValidation(Workbook workbook, int sheetIdx, String options, int firstRow, int lastRow, int firstCol, int lastCol) { + String[] optionArray = options.split(","); + Sheet sheet = workbook.getSheetAt(sheetIdx); + + // 删除已有的校验 + DataValidationHelper dvHelper = sheet.getDataValidationHelper(); + DataValidationConstraint dvConstraint = dvHelper.createCustomConstraint("DELETE_ALL"); + CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); // 设置一个范围,例如整个表格范围 + DataValidation validation = dvHelper.createValidation(dvConstraint, addressList); + validation.setSuppressDropDownArrow(true); + sheet.addValidationData(validation); + + // 创建数据验证对象 + DataValidationHelper validationHelper = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(optionArray); + addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); // 指定单元格范围 + validation = validationHelper.createValidation(constraint, addressList); + // 应用数据验证到单元格 + sheet.addValidationData(validation); + } + + /** + * 功能:获取指定Cell的内容值 + * + * @param cell + * @return + */ + public static Object getValue(XSSFCell cell) { + switch (cell.getCellType()) { + case CellType.STRING: + String cellValueString = cell.getStringCellValue(); + return cellValueString; + case CellType.NUMERIC: + //日期格式 + if (DateUtil.isCellDateFormatted(cell)) { + Date dateValue = cell.getDateCellValue(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String formattedDate = sdf.format(dateValue); + return formattedDate; + } + double cellValueNumeric = cell.getNumericCellValue(); + return cellValueNumeric; + case CellType.BOOLEAN: + boolean cellValueBoolean = cell.getBooleanCellValue(); + return cellValueBoolean; + case CellType.BLANK: + cellValueString = cell.getStringCellValue(); + return cellValueString; + // 其他类型的处理 + default: + cellValueString = cell.getStringCellValue(); + return cellValueString; + } + } + + /** + * 功能:判断一个单元格是不是被合并了 + * + * @param sheet + * @return + */ + public static boolean isMerged(XSSFSheet sheet, XSSFCell cell) { + // 判断单元格是否被合并 + for (CellRangeAddress range : sheet.getMergedRegions()) { + if (cell.getRowIndex() >= range.getFirstRow() && cell.getRowIndex() <= range.getLastRow() + && cell.getColumnIndex() >= range.getFirstColumn() && cell.getColumnIndex() <= range.getLastColumn()) { + return true; + } + } + return false; + } +}