一、日期转换
SimpleDateFormat format = new SimpleDateFormat("mm/dd/yy");
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
ParsePosition pos = new ParsePosition(0);
String deliveryDate = "";
try {
System.out.println("Excel日期:"+content);
Date parse = format.parse(content, pos);
deliveryDate = df.format(parse);
System.out.println("Excel转换后日期:"+deliveryDate);
}
catch (Exception e1) {
err.append("日期错误!");
}
二、Apache POI
1.合并单元格
/*
* 设定合并单元格区域范围
* firstRow 0-based
* lastRow 0-based
* firstCol 0-based
* lastCol 0-based
* 第firstRow 到 lastRow 第firstCol 到 lastCol被合并
* eq:从第一行到第五行,第3列到第五列被合并
*/
CellRangeAddress region=new CellRangeAddress(0, 0, 5, 11);
sheet.addMergedRegion(region);
2.给Excel添加数据有效性
/**
* 给某个范围内的单元格添加数据有效性
* 从第3行到第6行 从第2列到第6列
*/
HSSFDataValidation validation = ImportEvaluateIndexUntil.setBoxs(3, 6, 2, 6);
sheet.addValidationData(validation);
/**
*
* @param firstRow 从第几行
* @param lastRow 到第几行
* @param firstCol 从第几列
* @param lastCol 到第几列
* @return
*/
public static HSSFDataValidation setBoxs(int firstRow, int lastRow, int firstCol, int lastCol) {
// 设定在哪个单元格生效
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
final String[] DATA_LIST = new String[] { "评估", "不评估", };
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(DATA_LIST);
HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);
// dataValidation.createPromptBox("输入提示", "请从下拉列表中选择评估/不评估");
dataValidation.setShowPromptBox(true);
return dataValidation;
}