excel的导出
pom依赖
<!-- 导出excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-contrib -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-contrib</artifactId>
<version>3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
1.先设置表头格式
/**
* 设置报表头样式
* @param workbook
* @return
*/
public static CellStyle headStyle(SXSSFWorkbook workbook){
// cell样式
CellStyle cellStyle = workbook.createCellStyle();
// 设置单元格背景色,设置单元格背景色以下两句必须同时设置
// 设置填充样式
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 设置填充色
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
// 设置单元格上、下、左、右的边框线
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 创建一个字体对象
Font font = workbook.createFont();
// 设置字体的宽度
font.setBoldweight((short) 10);
// 设置字体的高度
font.setFontHeightInPoints((short) 10);
// 粗体显示
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置cellStyle的字体
cellStyle.setFont(font);
// 设置自动换行
cellStyle.setWrapText(true);
// 设置单元格字体显示居中(左右方向)
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置单元格字体显示居中(上下方向)
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return cellStyle;
}
/**
* 设置报表体样式
* @param wb
* @return
*/
public static CellStyle contentStyle(SXSSFWorkbook wb){
CellStyle style1 = wb.createCellStyle();// cell样式
// 设置单元格上、下、左、右的边框线
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setWrapText(true);// 设置自动换行
style1.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 设置单元格字体显示居中(左右方向)
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置单元格字体显示居中(上下方向)
return style1;
}
/**
* 设置报表标题样式
* @param workbook
* @return
*/
public static CellStyle titleStyle(SXSSFWorkbook workbook){
CellStyle cellStyle = workbook.createCellStyle();// cell样式
// 设置单元格背景色,设置单元格背景色以下两句必须同时设置
// if(color != HSSFColor.WHITE.index){
// cellStyle.setFillForegroundColor(color);// 设置填充色
// }
// 设置单元格上、下、左、右的边框线
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
// //创建一个字体对象
// Font font = workbook.createFont();//
// font.setBoldweight(fontSize);// 设置字体的宽度
// font.setFontHeightInPoints(fontSize);// 设置字体的高度
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
// cellStyle.setFont(font);// 设置cellStyle的字体
// 设置自动换行
cellStyle.setWrapText(true);
// 设置单元格字体显示居中(左右方向)
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置单元格字体显示居中(上下方向)
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return cellStyle;
}
/**
*设置表头
* @param sheet
*/
public static void initTitleEX(SXSSFSheet sheet, CellStyle header,String title[]) {
//设置单元格样式
SXSSFRow row0 = sheet.createRow(0);
// 字段名所在表格的宽度
row0.setHeight((short) 800);
for(int j = 0;j<title.length; j++) {
SXSSFCell cell = row0.createCell(j);
//设置每一列的字段名
XSSFRichTextString text = new XSSFRichTextString(title[j]);
cell.setCellValue(text);
cell.setCellStyle(header);
sheet.setColumnWidth(j,10000);
}
}
private void export(List<Alarm> list) {
log.info("开始导出");
try {
//创建一个workbook对应一个Excel文件
//将后面的1000条缓存到内存中,其余的写入磁盘的临时文件中,写入成功后会将临时文件删除
SXSSFWorkbook workbook = new SXSSFWorkbook(1000);
// 设置数据压缩
workbook.setCompressTempFiles(true);
SXSSFSheet sheet = workbook.createSheet();
// 设置报表头样式
CellStyle header = ExcelFormatUtil.headStyle(workbook);
// 报表体样式
CellStyle content = ExcelFormatUtil.contentStyle(workbook);
// 设置表头样式
CellStyle titleStyle = ExcelFormatUtil.titleStyle(workbook);
//创建表头
ExcelFormatUtil.initTitleEX(sheet, header, Title);
//设置表格内容格式
log.info("遍历数据组装单元格内容");
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Alarm alarm = list.get(i);
//设置第二行的高度
SXSSFRow row = sheet.createRow(i + 1);
//设置表格高度
row.setHeight(((short) 500));
//在这里使用反射
Field[] declaredFields = alarm.getClass().getDeclaredFields();
for(int j=0;j<declaredFields.length;){
//用来访问类中的私有变量
declaredFields[j].setAccessible(true);
//获取类中的值
Object value = declaredFields[j].get(alarm);
SXSSFCell cell = row.createCell(j++);
cell.setCellValue(value.toString());
cell.setCellStyle(content);
cell.setCellStyle(titleStyle);
}
}
log.info("结束组装");
}
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = dateFormat.format(date);
//创建一个文件
FileOutputStream outFile = new FileOutputStream("/Users/lele/Downloads/"+"监控表格"+time+".xlsx");
// 将工作簿写入输出流
//这里关闭流的顺序,最后用完,最先关闭,后进先出,
workbook.write(outFile);
outFile.close();
// 处理掉临时文件
workbook.dispose();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
在开发的时候遇到的一些问题
1.我在进行遍历数据到单元格的时候,先是使用的是逐个遍历的形式,但是在后面感觉,当我需要导出的列过于多时,这种方法太笨重了。于是,使用了反射的方法来达到目的。但是反射的效率有有点低,因为反射的时候需要对参数进行封装和解封的操作了,需要检查可见性,校验参数等,但是现在还没想到更好的方法。
2.关于使用SXSSFWorkBook的原因
需要导出的文件可能会很大,为了防止内存的溢出,就只能用磁盘空间换取内存。
一些字体和导出的格式是借鉴于这位老哥的https://www.jb51.net/article/161337.htm