上一篇实现了基本的导出功能,这一篇再给excel加点样式。在上一篇的基础上,修改代码如下:
public String export() throws Exception {
// 文件名
String title = "单点登录信息";
// 设置标题
String headers[] = {"系统名称","IP地址","传输协议","所属单位","是否连通","录入时间"};
// 获取数据库数据
List singleSignons = exportAll();
ExcelUtil.fillExcelData(singleSignons, headers, title, ServletActionContext.getResponse());
return null;
}
/**
* 利用反射机制遍历对象的属性值,并放入单元格中,为excel添加样式
*/
public static void fillExcelData(List list, String[] headers, String title,
HttpServletResponse response) throws Exception {
// 创建工作薄
HSSFWorkbook wb = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = wb.createSheet(title);
// 设置表格默认宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = wb.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 = wb.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = wb.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 = wb.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
// 合并单元格0到headers.length - 1行
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length - 1));
// 标题
int rowIndex = 0;
HSSFRow row0 = sheet.createRow(rowIndex++);
HSSFCell cell1 = row0.createCell(0);
cell1.setCellStyle(style);
cell1.setCellValue(title);
for (short i = 1; i < headers.length; i++) {
HSSFCell cell = row0.createCell(i);
cell.setCellStyle(style);
}
// 产生表格标题行
HSSFRow row = sheet.createRow(rowIndex++);
for (short i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(headers[i]);
}
// 产生表格内容行
for(Object obj : list){
// 利用反射得到对象的属性
Class cls = obj.getClass();
java.lang.reflect.Field[] flds = cls.getDeclaredFields(); // 得到私有属性
row = sheet.createRow(rowIndex++);
for(int j=0; j<headers.length; j++) {
flds[j+1].setAccessible(true);
HSSFCell cell = row.createCell(j);
cell.setCellStyle(style2);
cell.setCellValue((flds[j+1].get(obj)).toString());
}
}
String fileName = title+".xls";
response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso8859-1"));
response.setContentType("application/ynd.ms-excel;charset=UTF-8");
OutputStream out=response.getOutputStream();
wb.write(out);
out.flush();
out.close();
}