一、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
*/
@RequestMappingvalue = "/export.do",method = RequestMethod.GET)
@ResponseBody
public void exportHttpServletRequest 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.encodefileName, "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.setFontHeightInPointsshort) 11);
font.setFontName"宋体");
// 2.生成样式对象
CellStyle style = workbook.createCellStyle);
style.setFontfont); // 调用字体样式对象
style.setWrapTexttrue);//自动换行
//创建第一行
HSSFRow row = sheet.createRow0);
//定义一个单元格,相当于在第一行插入了三个单元格值分别是
// "供应商", "房间号", "平台","订单号","应收帐","是否到账","到账时间
HSSFCell cell = null;
row.setHeightInPoints20);//目的是想把行高设置成20px
//插入第一行数据
for int i = 0; i < headers.length; i++) {
cell = row.createCelli);
cell.setCellValueheaders[i]);
cell.setCellStylestyle);
}
int cou = 0;
//追加数据
HSSFRow nextrow = null;
if list.size)>=1){
for int i = 1; i <=count; i++) {
cou++;
nextrow = sheet.createRowi);
HSSFCell cell2 = nextrow.createCell0);
// 3.单元格应用样式
cell2.setCellStylestyle);
cell2.setCellValuelist.geti - 1).getSupplierName));
cell2 = nextrow.createCell1);
cell2.setCellStylestyle);
cell2.setCellValuelist.geti - 1).getRoomNumber));
cell2 = nextrow.createCell2);
cell2.setCellStylestyle);
cell2.setCellValuelist.geti - 1).getPlatformName));
cell2 = nextrow.createCell3);
cell2.setCellStylestyle);
cell2.setCellValuelist.geti - 1).getCodeNumber));
/*cell2 = nextrow.createCell4);
cell2.setCellStylestyle);
cell2.setCellValuelist.geti - 1).getPassengerName));*/
cell2 = nextrow.createCell4);
cell2.setCellStylestyle);
if list.geti - 1).getCurrency) == 1) {
cell2.setCellValue"¥" + list.geti - 1).getAccountsreceivable));
} else if list.geti - 1).getCurrency) == 2) {
cell2.setCellValue"₱" + list.geti - 1).getAccountsreceivable));
}
cell2 = nextrow.createCell5);
cell2.setCellStylestyle);
if list.geti - 1).getIsdao) == 1) {
cell2.setCellValue"否");
} else if list.geti - 1).getIsdao) == 2) {
cell2.setCellValue"是");
}
cell2 = nextrow.createCell6);
cell2.setCellStylestyle);
String tsStr = "";
Timestamp ts = list.geti - 1).getPaymentdate);
if ts != null) {
try {
tsStr = ts.toString);
} catch Exception e) {
e.printStackTrace);
}
cell2.setCellValuetsStr);
}
sheet.setColumnWidthi, 25 * 256);
}
}
try {
OutputStream out = response.getOutputStream);
workbook.writeout);
out.close);
workbook.close);
} catch IOException e) {
// TODO Auto-generated catch block
e.printStackTrace);
}
}