在table工具栏加导出按钮:
<table id="hwestable" class="easyui-datagrid"></table>
<div id="tb" style="padding:5px;height:auto">
<div style="text-align: right;">
<a href="#" class="easyui-linkbutton" id="exportExcel" iconCls="icon-export" plain="true">导出excel</a>
</div>
</div>
触发:
$(function(){
$("#exportExcel").click(function () {
Export("导出指标",$("#allindextable"));
});
});
Export方法:
function Export(strXlsName, exportGrid) {
var f = $('<form action="${ctx}/huawu/exportXls" method="post" id="fm1"></form>');
var i = $('<input type="hidden" id="txtContent" name="txtContent" />');
var l = $('<input type="hidden" id="txtName" name="txtName" />');
i.val(ChangeToTable(exportGrid));
i.appendTo(f);
l.val(strXlsName);
l.appendTo(f);
f.appendTo(document.body).submit();
document.body.removeChild(f);
}
exportXls方法:
/**
* 将报表导出为excel
* @param request
* @param response
*/
@SuppressWarnings("all")
@RequestMapping(value = "exportXls")
public void exportXls(HttpServletRequest request,
HttpServletResponse response) {
//step.1 设置,获取配置信息
String codedFileName = request.getParameter("txtName");
if(StringUtils.isEmpty(codedFileName)){
codedFileName="导出.xls";
}else{
codedFileName+=".xls";
}
String sheetName="导出信息";
String txtContent=request.getParameter("txtContent");
response.setContentType("Application/ms-excel");
PrintWriter out = null;
try {
//step.4 根据浏览器进行转码,使其支持中文文件名
if (isIE(request)) {
codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF-8");
response.setHeader("content-disposition", "attachment;filename="
+ codedFileName);
/*response.setHeader("content-disposition", "attachment;FileFormat="
+ "-4143");*/
} else {
codedFileName = new String(codedFileName.getBytes("UTF-8"),
"ISO-8859-1");
response.setHeader("content-disposition", "attachment;filename="
+ codedFileName);
}
/*response.setHeader("content-disposition", "attachment;filename="
+ codedFileName);*/
/*response.setHeader("content-disposition", "attachment;FileFormat="
+ "-4143");*/
out = response.getWriter();
out.write("<html>\n<head>\n");
out.write("<style type=\"text/css\">\n.pb{font-size:13px;border-collapse:collapse;} "+
"\n.pb th{font-weight:bold;text-align:center;border:0.5pt solid windowtext;padding:2px;} " +
"\n.pb td{border:0.5pt solid windowtext;padding:2px;}\n</style>\n</head>\n");
out.write("<body>\n" + txtContent + "\n</body>\n</html>");
out.flush();
} catch (UnsupportedEncodingException e1) {
} catch (Exception e) {
} finally {
if(out!=null){
out.close();
}
}
}
protected boolean isIE(HttpServletRequest request) {
return (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 || request
.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0) ? true
: false;
}
export.js:
function ChangeToTable(printDatagrid) {
var tableString = '<table cellspacing="0" class="pb">';
var frozenColumns = printDatagrid.datagrid("options").frozenColumns; // 得到frozenColumns对象
var columns = printDatagrid.datagrid("options").columns; // 得到columns对象
var nameList = new Array();
// 载入title
if (typeof columns != 'undefined' && columns != '') {
$(columns).each(function (index) {
tableString += '\n<tr>';
if (typeof frozenColumns != 'undefined' && typeof frozenColumns[index] != 'undefined') {
for (var i = 0; i < frozenColumns[index].length; ++i) {
if (!frozenColumns[index][i].hidden) {
tableString += '\n<th width="' + frozenColumns[index][i].width + '"';
if (typeof frozenColumns[index][i].rowspan != 'undefined' && frozenColumns[index][i].rowspan > 1) {
tableString += ' rowspan="' + frozenColumns[index][i].rowspan + '"';
}
if (typeof frozenColumns[index][i].colspan != 'undefined' && frozenColumns[index][i].colspan > 1) {
tableString += ' colspan="' + frozenColumns[index][i].colspan + '"';
}
if (typeof frozenColumns[index][i].field != 'undefined' && frozenColumns[index][i].field != '') {
nameList.push(frozenColumns[index][i]);
}
tableString += '>' + frozenColumns[0][i].title + '</th>';
}
}
}
for (var i = 0; i < columns[index].length; ++i) {
if (!columns[index][i].hidden) {
tableString += '\n<th width="' + columns[index][i].width + '"';
if (typeof columns[index][i].rowspan != 'undefined' && columns[index][i].rowspan > 1) {
tableString += ' rowspan="' + columns[index][i].rowspan + '"';
}
if (typeof columns[index][i].colspan != 'undefined' && columns[index][i].colspan > 1) {
tableString += ' colspan="' + columns[index][i].colspan + '"';
}
if (typeof columns[index][i].field != 'undefined' && columns[index][i].field != '') {
nameList.push(columns[index][i]);
}
tableString += '>' + columns[index][i].title + '</th>';
}
}
tableString += '\n</tr>';
});
}
// 载入内容
var rows = printDatagrid.datagrid("getRows"); // 这段代码是获取当前页的所有行
for (var i = 0; i < rows.length; ++i) {
tableString += '\n<tr>';
for (var j = 0; j < nameList.length; ++j) {
var e = nameList[j].field.lastIndexOf('_0');
tableString += '\n<td';
if (nameList[j].align != 'undefined' && nameList[j].align != '') {
tableString += ' style="text-align:' + nameList[j].align + ';"';
}
tableString += '>';
if (e + 2 == nameList[j].field.length) {
tableString += rows[i][nameList[j].field.substring(0, e)];
}
else
tableString += rows[i][nameList[j].field];
tableString += '</td>';
}
tableString += '\n</tr>';
}
tableString += '\n</table>';
return tableString;
}
来源:oschina
链接:https://my.oschina.net/u/2285090/blog/542293