后台代码:
/**
* 导出EXCLE表格
*
* @param request
* 请求
* @param response
* 响应
*/
@RequestMapping("/exportTables.do")
@ResponseBody
public void exportTables(HttpServletRequest request, HttpServletResponse response) {
/**
* 1、获取数据
*/
List<Lease> list = leaseservice.getAllLease();
/**
* 2、标题
*/
String[] title = { "资产编码", "物资编码", "设备名称", "使用年限", "单位编号", "入账日期", "使用日期", "累计折旧", "租赁类别", "所在地点", "出厂编号",
"责任人", "主机编号", "技术参数", "供应商编号", "单位", "规格型号", "设备状态", "部门编号", "入矿日期", "资产原值", "净值", "设备种类", "安全标号",
"备注" };
/**
* 表名字
*/
String fileName = "设备租赁信息表" + System.currentTimeMillis() + ".xls";
/**
* sheet名字
*/
String sheetName = "设备租赁信息表";
String[][] content = new String[list.size()][];
for (int i = 0; i < list.size(); i++) {
content[i] = new String[title.length];
Lease leasee = list.get(i);
content[i][0] = leasee.getAsset_number();
content[i][1] = leasee.getMaterial_coding() + "";
content[i][2] = leasee.getDevice_name() + "";
content[i][3] = leasee.getFix_years() + "";
content[i][4] = leasee.getCompany_number();
content[i][5] = leasee.getDate_entry();
content[i][6] = leasee.getUse_date();
content[i][7] = leasee.getAccumulated_depreciaition() + "";
content[i][8] = leasee.getRental_type();
content[i][9] = leasee.getLocation();
content[i][10] = leasee.getFactory_number();
content[i][11] = leasee.getPerson_liable();
content[i][12] = leasee.getHost_number();
content[i][13] = leasee.getTechnical_parameter();
content[i][14] = leasee.getSupplier_number();
content[i][15] = leasee.getUnit();
content[i][16] = leasee.getSpecification_model();
content[i][17] = leasee.getEquipment_state();
content[i][18] = leasee.getDepartment_numb();
content[i][19] = leasee.getEntrydate_ore();
content[i][20] = leasee.getOriginal_assets();
content[i][21] = leasee.getNet_worth() + "";
content[i][22] = leasee.getDevice_type();
content[i][23] = leasee.getSecurity_labe();
content[i][24] = leasee.getRemarks();
}
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 以IO流的形式响应到页面
* @param response
* @param fileName
*/
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
前台代码:<a class="layui-btn layui-btn-normal exportExcle_btn">导出</a>