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();
}
}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM