导出数据到excel工作簿
OutputStream outputStream = null;
try {
String fileName = "XXX.xls"; //文件名称自定义
//文件路径
String filePath = RuoYiConfig.getDownloadPath() + fileName;
File file = new File(filePath);
//判断文件时候存在,存在就删除重新创建
if (file.exists()) {
file.delete();
}
file.createNewFile();
//创建工作簿及定义工作簿名称
HSSFWorkbook wb = new HSSFWorkbook();
//excel标签名
HSSFSheet sheet = wb.createSheet("风险综合评级系统取数数据表");
//设置列宽及列数
int[] excelHeaderWidth = new int[5];
for (int i = 0; i < excelHeaderWidth.length; i++) {
excelHeaderWidth[i] = 200;
}
//风格设置
//整体风格(单独设置)
HSSFCellStyle style = this.createStyle(wb, false, true, false);
//标题风格 加粗(单独设置)
HSSFCellStyle styletitle = this.createStyle(wb, true, false, true);
//标题风格 不加粗(单独设置)
HSSFCellStyle styletitle1 = this.createStyle(wb, true, false, false);
//文本风格
HSSFCellStyle textStyle = wb.createCellStyle();
//设置列为文本
sheet.setDefaultColumnStyle(0, textStyle);
sheet.setDefaultColumnStyle(1, textStyle);
sheet.setDefaultColumnStyle(2, textStyle);
sheet.setDefaultColumnStyle(3, textStyle);
sheet.setDefaultColumnStyle(4, textStyle);
//设置列宽度(像素)
for (int i = 0; i < excelHeaderWidth.length; i++) {
sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
}
//设置标题行
HSSFRow row1 = sheet.createRow(0);
HSSFCell cell = row1.createCell(0); //第一个单元格
cell.setCellValue("指标编码"); //设定值
cell.setCellStyle(style); //内容居中
cell = row1.createCell(1); //第二个单元格
cell.setCellValue("指标名称");
cell.setCellStyle(style);
cell = row1.createCell(2); //第三个单元格
cell.setCellValue("部门名称");
cell.setCellStyle(style);
cell = row1.createCell(3); //第四个单元格
cell.setCellValue("风险点");
cell.setCellStyle(style);
cell = row1.createCell(4); //第五个单元格
cell.setCellValue("指标值");
cell.setCellStyle(style);
//添加excel数据
//tbcrr07List 为业务数据
for (int i = 0; i < tbcrr07List.size(); i++) {
//添加列元素
HSSFRow row = sheet.createRow(i+1);
row.createCell(0).setCellValue("//取list中集合对象属性");
row.createCell(1).setCellValue("");
row.createCell(2).setCellValue("");
row.createCell(3).setCellValue("");
row.createCell(4).setCellValue("");
}
//创建输出流及输出文件
outputStream = new FileOutputStream(filePath);
wb.write(outputStream);
outputStream.flush();
outputStream.close();
return AjaxResult.success(file.getName(), "UTF-8");
} catch (RuntimeException e) {
logger.error("导出Excel失败。", e);
throw e;
} finally {
//关闭流
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
Excel样式设计
//设置excel样式
private HSSFCellStyle createStyle(HSSFWorkbook wb, boolean isTitle, boolean isRight, boolean isBold) {
//整体风格(单独设置)
HSSFCellStyle style = wb.createCellStyle();//风格设置
//文本字体
HSSFFont font = wb.createFont();//字体设置
font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());//颜色
//判断是不是标题
if (isTitle) {
font.setFontHeightInPoints((short) 12); //设置字体大小
} else {
font.setFontHeightInPoints((short) 10); //设置字体大小
}
//字体是否加粗
if (isBold) {
font.setBold(true);
} else {
font.setBold(false);
}
style.setFont(font);
//是否靠右显示
if (isRight) {
style.setAlignment(HorizontalAlignment.RIGHT);
} else {
style.setAlignment(HorizontalAlignment.CENTER);
}
return style;
}