poi导出Excel,设置数据有效性

创建时间:2017/10/26 17:36
更新时间:2017/10/26 17:37
来源:about:blank

private DataValidation getDataValidationByFormula(String[] formulaString, int firstRow, int lastRow, int firstCol) {
//加载下拉列表内容
DVConstraint constraint = DVConstraint.createExplicitListConstraint(formulaString);
//设置数据有效性加载在哪个单元格上。
//四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, firstCol);
DataValidation data_validation_list = new HSSFDataValidation(regions, constraint);
return data_validation_list;
}

private void exportExcell(HttpServletResponse response, String cold, String coln, Vector v) {

try {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet();
//设置数据有效性
String orderType = "GOA,PrePay";
String cardType = "Plat,Cent";
String hotelType = "INTL,DOM";
sheet.addValidationData(getDataValidationByFormula(hotelType.split(","), 1, v.size(), 10));
sheet.addValidationData(getDataValidationByFormula(cardType.split(","), 1, v.size(), 3));
sheet.addValidationData(getDataValidationByFormula(orderType.split(","), 1, v.size(), 9));

// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);

/* // 声明一个画图的顶级管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
// 设置注释内容
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
comment.setAuthor("leno");*/

// 产生表格标题行
HSSFRow row = sheet.createRow(0);
String[] headerName = cold.split(",");
for (short i = 0; i < headerName.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headerName[i]);
cell.setCellValue(text);
}
String[] headers = coln.split(",");
for (int i = 0; i < v.size(); i++) {
Hashtable ht = (Hashtable) v.get(i);
row = sheet.createRow(i + 1);
for (int k = 0; k < headers.length; k++) {
HSSFCell cell = row.createCell(k);
cell.setCellStyle(style2);
String textValue = "";
if (isNumeric(String.valueOf(ht.get(headers[k]))) && !"CardNo".equals(headers[k]) && !"TravellerPhone".equals(headers[k]) && !"CellPhone".equals(headers[k])) {
textValue = String.valueOf(ht.get(headers[k]));
if (("null".equals(textValue)) || ("".equals(textValue))) {
textValue = "0";
}
} else {
textValue = String.valueOf(ht.get(headers[k]));
if ("null".equals(textValue)) {
textValue = "";
}
}
cell.setCellValue(textValue);
}
}
try {
workbook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}

}

private static Pattern p = Pattern.compile("^//d+(//.//d+)?$");