main
黄海 2 years ago
parent 206dea7fe5
commit 04607bd21c

@ -284,16 +284,37 @@ public class CollectController extends Controller {
int colIdx = 0; int colIdx = 0;
for (Record record : list) { for (Record record : list) {
boolean allow_blank = record.getBoolean("allow_blank"); boolean allow_blank = record.getBoolean("allow_blank");
XSSFCell cell = wb.getSheetAt(i).getRow(end_row).getCell(colIdx);
if (!allow_blank) { if (!allow_blank) {
//一行时,星号加在一行 //一行时,星号加在一行
//二行时,星号加在二行 //二行时,星号加在二行
XSSFCell cell = wb.getSheetAt(i).getRow(end_row).getCell(colIdx);
if (StrKit.isBlank(cell.getStringCellValue())) { if (StrKit.isBlank(cell.getStringCellValue())) {
cell = wb.getSheetAt(i).getRow(start_row).getCell(colIdx); cell = wb.getSheetAt(i).getRow(start_row).getCell(colIdx);
} }
PoiUtil.addStar(wb, cell); 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++; colIdx++;
} }
} }
@ -312,10 +333,16 @@ public class CollectController extends Controller {
//2 数值 //2 数值
//4 日期(年-月-日) //4 日期(年-月-日)
//3 数值(小数) //3 数值(小数)
// int data_type_id = record.getInt("data_type_id"); 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 == 2) {
// if (data_type_id == 3) PoiUtil.setFloatStyle(wb, i, colIdx, data_start_row, data_start_row + 20000); PoiUtil.setIntegerStyle(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); }
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++; colIdx++;
} }
} }

@ -1,11 +1,14 @@
package com.dsideal.QingLong.Util; 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.*;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.*;
import java.io.*;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
@ -283,23 +286,21 @@ public class PoiUtil {
* @param firstRow * @param firstRow
* @param lastRow * @param lastRow
*/ */
public static void setIntegerStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { public static void setIntegerStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
// 设置整数类型的样式 XSSFSheet sheet = wb.getSheetAt(sheetIdx);
CellStyle integerStyle = wb.createCellStyle(); XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
integerStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0")); // 设置为整数类型 XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint(
DataValidationConstraint.ValidationType.INTEGER, XSSFDataValidationConstraint.OperatorType.BETWEEN,
for (int i = firstRow; i <= lastRow; i++) { String.valueOf(Integer.MIN_VALUE), String.valueOf(Integer.MAX_VALUE)
Row row = wb.getSheetAt(sheetIdx).getRow(i); );
if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i); CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
// 设置整数类型的值 XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
Cell integerCell = row.getCell(colIdx); validation.setSuppressDropDownArrow(false);
if (integerCell == null) { validation.setShowErrorBox(true);
integerCell = row.createCell(colIdx); sheet.addValidationData(validation);
}
integerCell.setCellStyle(integerStyle); // 应用整数类型的样式
}
} }
/** /**
* *
* *
@ -309,20 +310,18 @@ public class PoiUtil {
* @param firstRow * @param firstRow
* @param lastRow * @param lastRow
*/ */
public static void setFloatStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { public static void setFloatStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
// 设置浮点数类型的样式 XSSFSheet sheet = wb.getSheetAt(sheetIdx);
CellStyle floatStyle = wb.createCellStyle(); XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
floatStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00")); // 设置为浮点数类型,保留两位小数 XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint(
for (int i = firstRow; i <= lastRow; i++) { DataValidationConstraint.ValidationType.DECIMAL, XSSFDataValidationConstraint.OperatorType.BETWEEN,
Row row = wb.getSheetAt(sheetIdx).getRow(i); String.valueOf(Float.MIN_VALUE), String.valueOf(Float.MAX_VALUE)
if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i); );
// 设置浮点数类型的值 CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
Cell floatCell = row.getCell(colIdx); XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
if (floatCell == null) { validation.setSuppressDropDownArrow(false);
floatCell = row.createCell(colIdx); validation.setShowErrorBox(true);
} sheet.addValidationData(validation);
floatCell.setCellStyle(floatStyle); // 应用浮点数类型的样式
}
} }
/** /**
@ -334,19 +333,32 @@ public class PoiUtil {
* @param firstRow * @param firstRow
* @param lastRow * @param lastRow
*/ */
public static void setDateStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) { public static void setDateStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
// 设置日期类型的样式 XSSFSheet sheet = wb.getSheetAt(sheetIdx);
CellStyle dateStyle = wb.createCellStyle(); XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
dateStyle.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("yyyy-mm-dd")); // 设置为日期类型格式为yyyy-mm-dd DataValidationConstraint dvConstraint = dvHelper
for (int i = firstRow; i <= lastRow; i++) { .createDateConstraint(DataValidationConstraint.OperatorType.BETWEEN, "date(1900,1,1)", "date(2099,12,31)", "yyyy/MM/dd");
Row row = wb.getSheetAt(sheetIdx).getRow(i); CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i); XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
// 设置日期类型的值 validation.setSuppressDropDownArrow(false);
Cell dateCell = row.getCell(colIdx); validation.setShowErrorBox(true);
if (dateCell == null) { sheet.addValidationData(validation);
dateCell = row.createCell(colIdx); }
}
dateCell.setCellStyle(dateStyle); // 应用日期类型的样式 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();
} }
} }

Loading…
Cancel
Save