本文非原创,原创不知道在哪里了,要是侵权了,可以私信我。只做自身知识点记录和自身总结。有需要的朋友可以参考,不足之处欢迎下方留言~
这个功能接口需求如下:前端展示一个数据集合,在每条数据后面有一个导出功能,导出的时候传给后台一个id,后台接收到参数,就与数据库进行匹配,查到的数据放进集合,将数据生产EXCEL文件流,在返给前端。由于前后端分离开发,前段代码就不贴出来了。
public void generateSheet(List<FamilyInfoBean> dataList, HttpServletResponse response)throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar(1900,0,-1);
Date date = calendar.getTime();
//创建工作薄
HSSFWorkbook wb = new HSSFWorkbook();
//创建工作表
HSSFSheet sheet = wb.createSheet();
//创建样式和字体
HSSFCellStyle curStyle = wb.createCellStyle();
HSSFFont curFont = wb.createFont();
//创建行列
HSSFRow nRow = sheet.createRow(0);
HSSFCell nCell = nRow.createCell(0);
//设置列的样式(具体实现在后面......)
nCell.setCellStyle(this.mainTitleStyle(curStyle, curFont));
//控制行号列号
int rowNo = 0;
int colNo = 0;
//列标题
String[] title;
title = new String[]{"id","姓名","地址"};
//设置标题到第一行的列中
nRow = sheet.createRow(rowNo++);
for (int i = 0; i < title.length; i++) {
nCell = nRow.createCell(i);
nCell.setCellValue(title[i]);
nCell.setCellStyle(this.textStyle(curStyle, curFont));
}
//创建样式和字体(为什么又new,因为下面是正文部分了)
curStyle = wb.createCellStyle();
for (FamilyInfoBean dto :dataList){
colNo = 0;//控制列号
//每遍历一次创建一行
nRow = sheet.createRow(rowNo++);
//id(先查询出,再将值设置到列中)
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getCommunity_name());
nCell.setCellStyle(this.textStyle( curStyle, curFont));
//姓名
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getHouse_number_plate());
nCell.setCellStyle(this.textStyle( curStyle, curFont));
//地址
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getPerson_address());
nCell.setCellStyle(this.textStyle( curStyle, curFont));
//到这里,excel就已经生成了,然后就需要通过流来写出去
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//将excel写入流
wb.write(byteArrayOutputStream);
//设置文件标题
String dateTime = DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
String outFile =dateTime + ".xls";
//设置返回的文件类型
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//对文件编码
outFile = response.encodeURL(new String(outFile.getBytes("gb2312"), "iso8859-1"));
//使用Servlet实现文件下载的时候,避免浏览器自动打开文件
response.addHeader("Content-Disposition", "attachment;filename=" + outFile);
//设置文件大小
response.setContentLength(byteArrayOutputStream.size());
//创建Cookie并添加到response中
Cookie cookie = new Cookie("fileDownload", "true");
cookie.setPath("/");
response.addCookie(cookie);
//将流写进response输出流中
ServletOutputStream outputstream = response.getOutputStream();
byteArrayOutputStream.writeTo(outputstream);
byteArrayOutputStream.close();
outputstream.flush();
//下面这个注释的代码,是测试是否能保存到本地,是否乱码,格式是否正确,如果没问题,前端可以直接请求接口
/*FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\excel\\1.xls"));
wb.write(fileOutputStream);
wb.close();
fileOutputStream.close();*/
}
}
private HSSFCellStyle mainTitleStyle( HSSFCellStyle curStyle, HSSFFont curFont) {
curStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
curFont.setFontName("宋体"); //字体
curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 加粗
curFont.setFontHeightInPoints((short) 16);//字体大小
curStyle.setFont(curFont); // 绑定关系
return curStyle;
}
private HSSFCellStyle textStyle(HSSFCellStyle curStyle, HSSFFont curFont) {
curStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左对齐
curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
curStyle.setWrapText(true); // 自动换行
curStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 实线
curStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 实线
curStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 实线
curStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 实线
curFont.setFontName("Times New Roman");//字体
curFont.setFontHeightInPoints((short) 10);//字体大小
curStyle.setFont(curFont); // 绑定关系
return curStyle;
}