使用POI实现Excel下载功能

如果没有使用maven的话,就用poi-3.10.1.jar和poi-ooxml-schemas-3.10.1.jar 两个jar包就行

1.首先,在想要在提交的Form上绑定对应的controller
例如:
其中CT是controller的名称,exportedKSKHExcel是controller中方法名称
2.创建一个按钮,给按钮绑定点击事件

绑定点击事件:
(function(){ (function(){ (“#imp”).click(function(){
$(“#form1”).submit();
return;
});
});
3.此时,点击按钮,会将表单中的数据提交给../C/exportedKSKHExcel。
4.在controller上绑定路径
@RequestMapping(“/exportedKSKHExcel”)
5.方法头
public void exportedKSKHExcel(HttpServletRequest request, HttpServletResponse response,ProblemDetailParam problemDetialParam) throws UnsupportedEncodingException
传HttpServletResponse 是为了能够让request去设置返回excel文件以及excel的编码格式
传HttpServletRequest 是为了能够获取session中的参数
传ProblemDetailParam 是为了能够使用ProblemDetailParam 的参数
throws UnsupportedEncodingException 是为了防止导出的文件或内容按照其他编码格式进行
6.方法体,设计表格样式部分
//此处为了获取session中的user
User user = (User) request.getSession().getAttribute(“SESSION_USER”);
HSSFWorkbook workbook= new HSSFWorkbook();
HSSFSheet sheet= workbook.createSheet();
// 设置表格默认列宽度为18个字节
sheet.setDefaultColumnWidth((short) 18);
// 生成一个样式
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);
7.设计表格表头部分
String[] headers = { “表头1”,”表头2”,”表头3”};
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++){
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
8.传入excel中的数据部分
(1)此处有多种方式,如果是针对单独一个表进行查询,且查询数据不需要进行其他操作,可直接采用List

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值