JS代码:
<!-- 导出Exc表格 -->
<script type="text/javascript">
function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
//如果jsondata不是对象,那么json.parse将分析对象中的json字符串。
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData)
: JSONData;
var CSV = '';
//在第一行拼接标题
CSV += ReportTitle + '\r\n\n';
//产生数据标头
if (ShowLabel) {
var row = "";
//此循环将从数组的第一个索引中提取标签
for ( var index in arrData[0]) {
//现在将每个值转换为字符串和逗号分隔
row += index + ',';
}
row = row.slice(0, -1);
//添加带换行符的标签行
CSV += row + '\r\n';
}
//第一个循环是提取每一行
for (var i = 0; i < arrData.length; i++) {
var row = "";
//第二个循环将提取每个列并将其转换为以逗号分隔的字符串
for ( var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
//每行后添加换行符
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
//Generate a file name
//var fileName = "我的学生_";
//this will remove the blank-spaces from the title and replace it with an underscore
//fileName += ReportTitle.replace(/ /g, "_");
//Initialize file format you want csv or xls
//var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
var uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURI(CSV);
// Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = ReportTitle + ".csv";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
$("#btnExport").click(function() {
var rows = $("#tab").datagrid("getSelections");
if (rows.length == 0) {
$.messager.alert("系统信息", "请选择导出的数据");
return;
}
$("#win-daochu").window("open");
});
$("#qrdaochu").click(function() {
var rows = $("#tab").datagrid("getSelections");
var ches = $("#daochu-checkbox :checkbox");//要导出显示的列
var datarows = [];//空数组
var row = {};//空json对象
for (var i = 0; i < rows.length; i++) {
for (var k = 0; k < ches.length; k++) {
var na = ches[k].name;//name:字段名
var isOk = ches[k].checked;
if(isOk && na == "studentId"){row.ID = rows[i].studentId;}
if(isOk && na == "name"){row.姓名 = rows[i].name;}
if(isOk && na == "age"){row.年龄 = rows[i].age;}
if(isOk && na == "phone"){row.手机号 = rows[i].phone;}
if(isOk && na == "education"){row.学历 = rows[i].education;}
if(isOk && na == "state"){row.个人状态 = rows[i].state;}
if(isOk && na == "sources"){row.来源渠道 = rows[i].sources;}
if(isOk && na == "sourceSite"){row.来源网址 = rows[i].sourceSite;}
if(isOk && na == "sourcekeyword"){row.来源关键词 = rows[i].sourcekeyword;}
if(isOk && na == "qq"){row.学员QQ = rows[i].qq;}
if(isOk && na == "weChat"){row.微信 = rows[i].weChat;}
}
datarows[i] = row;
row = {};
}
var data = JSON.stringify(datarows);//转换数据为json
var title = $("#daochu-title").textbox("getValue");//获取标题
JSONToCSVConvertor(data, title , true);
});
</script>
HTML代码:
<%-- 导出EXCel窗口 --%>
<div class="easyui-window"
data-options="iconCls:'icon-save',closed:true,width:600,height:380"
id="win-daochu" title="导出Excel">
<table cellpadding="10" id="tab-daochu">
<tr>
<td>
<label for="content">填写表名:</label>
<input class="easyui-textbox" type="text" id="daochu-title" style="width:200px">
</td>
</tr>
<tr>
<td>
<h3>选列:选择要显示的列</h3>
<table id="daochu-checkbox" style="border:1px solid #000000;" cellpadding="15">
<tr>
<td><input type="checkbox" name="studentId" value="ID" checked="checked"/>ID</td>
<td><input type="checkbox" name="name" value="姓名" checked="checked"/>姓名</td>
<td><input type="checkbox" name="age" value="年龄" checked="checked"/>年龄</td>
<td><input type="checkbox" name="phone" value="手机号" checked="checked" />手机号</td>
</tr>
<tr>
<td><input type="checkbox" name="education" value="学历" checked="checked"/>学历</td>
<td><input type="checkbox" name="state" value="个人状态" checked="checked"/>个人状态</td>
<td><input type="checkbox" name="sources" value="来源渠道" checked="checked"/>来源渠道</td>
<td><input type="checkbox" name="sourceSite" value="来源网址" checked="checked"/>来源网址</td>
</tr>
<tr>
<td><input type="checkbox" name="sourcekeyword" value="来源关键词" checked="checked"/>来源关键词</td>
<td><input type="checkbox" name="qq" value="学员QQ" checked="checked"/>学员QQ</td>
<td><input type="checkbox" name="weChat" value="微信" checked="checked"/>微信</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<a class="easyui-linkbutton" id="qrdaochu" data-options="iconCls:'icon-print'">确认导出</a>
</td>
</tr>
</table>