easyui浏览器下载xls文件

一、html页面

引入easyui

<script type="text/javascript" src="s/easyui/jquery.min.js"></script>
<script type="text/javascript" src="js/easyui/jquery.easyui.min.js"></script>
<div>
<a id="consumesOutExcel" class="easyui-linkbutton" style="" data-options="iconCls:'icon-redo'">导出当前数据</a>
</div>

二、js
$(function() {
//导出excel表
$("#consumesOutExcel").on('click',function(){
$.messager.progress({
title : '处理中',
msg : '请稍后',
});
$.messager.progress('close');
location.href="xls/export.do";

});
});
三、后台control
/**
* 用于导出excel的查询结果
* @param
* @return
*/
@RequestMapping(value = "/export.do",method = RequestMethod.GET)
@ResponseBody
public void export(HttpServletRequest request, HttpServletResponse response)throws UnsupportedEncodingException {

ModelAndView mv=null;
//查询用户表的全部数据

List<StayRegisterPo> list = null;
Integer count = 0;
list = this.stayRegisterService.fuzzyselectAll();
//查询用户表有多少行记录
count = stayRegisterService.countAll();

//二、 数据转成excel
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
String fileName = "财务报表.xls";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);

//创建excel表的表头
String[] headers = {"供应商", "房间号", "平台", "订单号", "应收帐", "是否到账", "到账时间"};
//创建Excel工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个工作表sheet
HSSFSheet sheet = workbook.createSheet();

//字体
// 1.生成字体对象
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
font.setFontName("宋体");

// 2.生成样式对象
CellStyle style = workbook.createCellStyle();
style.setFont(font); // 调用字体样式对象
style.setWrapText(true);//自动换行


//创建第一行
HSSFRow row = sheet.createRow(0);
//定义一个单元格,相当于在第一行插入了三个单元格值分别是
// "供应商", "房间号", "平台","订单号","应收帐","是否到账","到账时间
HSSFCell cell = null;
row.setHeightInPoints(20);//目的是想把行高设置成20px

//插入第一行数据
for (int i = 0; i < headers.length; i++) {
cell = row.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(style);
}
int cou = 0;
//追加数据
HSSFRow nextrow = null;
if (list.size()>=1){
for (int i = 1; i <=count; i++) {
cou++;
nextrow = sheet.createRow(i);
HSSFCell cell2 = nextrow.createCell(0);
// 3.单元格应用样式
cell2.setCellStyle(style);
cell2.setCellValue(list.get(i - 1).getSupplierName());
cell2 = nextrow.createCell(1);
cell2.setCellStyle(style);
cell2.setCellValue(list.get(i - 1).getRoomNumber());
cell2 = nextrow.createCell(2);
cell2.setCellStyle(style);
cell2.setCellValue(list.get(i - 1).getPlatformName());
cell2 = nextrow.createCell(3);
cell2.setCellStyle(style);
cell2.setCellValue(list.get(i - 1).getCodeNumber());
/*cell2 = nextrow.createCell(4);
cell2.setCellStyle(style);
cell2.setCellValue(list.get(i - 1).getPassengerName());*/
cell2 = nextrow.createCell(4);
cell2.setCellStyle(style);
if (list.get(i - 1).getCurrency() == 1) {
cell2.setCellValue("¥" + list.get(i - 1).getAccountsreceivable());
} else if (list.get(i - 1).getCurrency() == 2) {
cell2.setCellValue("₱" + list.get(i - 1).getAccountsreceivable());
}
cell2 = nextrow.createCell(5);
cell2.setCellStyle(style);
if (list.get(i - 1).getIsdao() == 1) {
cell2.setCellValue("否");
} else if (list.get(i - 1).getIsdao() == 2) {
cell2.setCellValue("是");
}
cell2 = nextrow.createCell(6);
cell2.setCellStyle(style);
String tsStr = "";
Timestamp ts = list.get(i - 1).getPaymentdate();
if (ts != null) {
try {
tsStr = ts.toString();
} catch (Exception e) {
e.printStackTrace();
}
cell2.setCellValue(tsStr);
}
sheet.setColumnWidth(i, 25 * 256);

}
}

try {
OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
原文地址:https://www.cnblogs.com/liuyuanchen/p/11772034.html