POI 设置某列为时间格式 , 防止格式不一致 (亲测可用)
//创建workbook工作薄
Workbook workbook = new XSSFWorkbook();
//创建工作表
XSSFSheet Sheet = (XSSFSheet) workbook.createSheet();
// 创建单元格样式
CellStyle style= workbook.createCellStyle();
// 设置为文本格式,防止身份证号变成科学计数法
DataFormat format = workbook.createDataFormat();
style.setDataFormat(format.getFormat("m/d/yy"));
//对单独某一列进行样式赋值,第一个参数为列数,第二个参数为样式
Sheet.setDefaultColumnStyle(0, style);
POI 中 Excel设置列的格式
Utility to identify builtin formats. Now can handle user defined data formats also. The following is a list of the formats as returned by this class.
0, "General"
1, "0"
2, "0.00"
3, "#,##0"
4, "#,##0.00"
5, "($#,##0_);($#,##0)"
6, "($#,##0_);[Red]($#,##0)"
7, "($#,##0.00);($#,##0.00)"
8, "($#,##0.00_);[Red]($#,##0.00)"
9, "0%"
0xa, "0.00%"
0xb, "0.00E+00"
0xc, "# ?/?"
0xd, "# ??/??"
0xe, "m/d/yy"
0xf, "d-mmm-yy"
0x10, "d-mmm"
0x11, "mmm-yy"
0x12, "h:mm AM/PM"
0x13, "h:mm:ss AM/PM"
0x14, "h:mm"
0x15, "h:mm:ss"
0x16, "m/d/yy h:mm"
// 0x17 - 0x24 reserved for international and undocumented 0x25, "(#,##0_);(#,##0)"
0x26, "(#,##0_);[Red](#,##0)"
0x27, "(#,##0.00_);(#,##0.00)"
0x28, "(#,##0.00_);[Red](#,##0.00)"
0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"
0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"
0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"
0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"
0x2d, "mm:ss"
0x2e, "[h]:mm:ss"
0x2f, "mm:ss.0"
0x30, "##0.0E+0"
0x31, "@" - This is text format.
0x31 "text" - Alias for "@"
若依项目中代码如下
/**
* 设置单元格校验
* @param attr Excel 对象
* @param row row行
* @param column column列
*/
public void setDataValidation(Excel attr, Row row, int column) {
//时间格式
if (StringUtils.isNotEmpty(attr.dateFormat())) {
sheet.setColumnWidth(column, 6000);
// 创建单元格样式
CellStyle style= wb.createCellStyle();
// 设置为文本格式,防止身份证号变成科学计数法
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("m/d/yy"));
//对单独某一列进行样式赋值,第一个参数为列数,第二个参数为样式
sheet.setDefaultColumnStyle(column, style);
setPromptOrValidation(sheet, attr.combo(), "年/月/日", 1, 100, column, column);
}
}
/**
* 设置 POI XSSFSheet 单元格提示或选择框
*
* @param sheet 表单
* @param textlist 下拉框显示的内容
* @param promptContent 提示内容
* @param firstRow 开始行
* @param endRow 结束行
* @param firstCol 开始列
* @param endCol 结束列
*/
public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
int firstCol, int endCol) {
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
DataValidation dataValidation = helper.createValidation(constraint, regions);
if (StringUtils.isNotEmpty(promptContent)) {
// 如果设置了提示信息则鼠标放上去提示
dataValidation.createPromptBox("", promptContent);
dataValidation.setShowPromptBox(true);
}
// 处理Excel兼容性问题
if (dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
} else {
dataValidation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(dataValidation);
}
标签:sheet,##,0.00,dataValidation,param,mm,POI,格式,亲测
From: https://www.cnblogs.com/zh76412950/p/17152308.html