query easyui中datagrid数据导出excel

js方法:

functionbtn_import_excel(){

varrtnObject_datagrid;

varsql_param={};

if(jQuery('#swjg').val()!=""&&jQuery('#swjg').val()!=null){

sql_param.jgdm=jQuery('#swjg').val();

}

sql_param.pageNumber=jQuery('#pageNumber').val();

sql_param.pageSize=jQuery('#pageSize').val();

alert("fxydtjService.querySwryRw");

varservice_datagrid=newCommonService("fxydtjService.querySwryRw",sql_param,"json");

service_datagrid.doService();

if(service_datagrid.getCode()=="2000"){

rtnObject_datagrid=service_datagrid.getResponse();

}else{

rtnObject_datagrid={total:0,rows:[]};

}

if(rtnObject_datagrid.rows.length==0){

alert("查询结果区域没有数据可导出!");

}else{

vardata=rtnObject_datagrid;

varcolumns=$('#bfpryxxlb').datagrid("options").columns;

$('#tableColumnsId').val(JSON.stringify(columns));

$('#tableDataId').val(JSON.stringify(data));

$('#exportFormFf').submit();

}

}

jsp:

<!--要导出的数据-->

<formid="exportFormFf"action="./exporttaskhistory.jsp"method="post">

<inputtype="hidden"name="tableData"id="tableDataId"value=""/>

<inputtype="hidden"name="tableColumns"id="tableColumnsId"value=""/>

</form>

jsp中的java代码:

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%>

<%@pageimport="java.util.List"%>

<%@pageimport="java.util.Map"%>

<%@pageimport="net.sf.json.JSONObject"%>

<%@pageimport="net.sf.json.JSONSerializer"%>

<%

response.reset();

response.setContentType("application/vnd.ms-excel;charset=GBK");

response.setHeader("Content-disposition","attachment;filename=taskhistory.xls");

%>

<html>

<head>

<%

StringtableData=request.getParameter("tableData");

Stringcolumns=request.getParameter("tableColumns");

Objectlist1=JSONSerializer.toJSON(tableData);

Listcolumns1=(List)JSONSerializer.toJSON(columns);

MapdataMap1=(Map)list1;

Listdata=(List)dataMap1.get("rows");

ListcolumnList=(List)columns1.get(0);

String[]columnsArr=newString[columnList.size()];

for(inti=0;i<columnList.size();i++){

Mapmap=(Map)columnList.get(i);

columnsArr[i]=String.valueOf(map.get("field"));

}

%>

<title>EXCEL</title>

</head>

<body>

<formname="fm"method="post">

<tablecellpadding="5"cellspacing="1"align="center"style="width:100%;font-size:9pt;style-align:center;background-color:#ffffff;border:1pxsolid#cccccc;">

<tr>

<%for(inti=0;i<columnList.size();i++){

Mapmap=(Map)columnList.get(i);

%>

<tdclass=formtitlealign="center"nowrapstyle="width:13%;font-size:9pt;background:#a480b2;color:#ffffff;height:30px;text-align:center;border:1pxsolid#cccccc;"><%=map.get("title")%></td>

<%}%>

</tr>

<%for(intj=0;j<data.size();j++){

Mapmap=(Map)data.get(j);

%>

<tr>

<%

for(intk=0;k<columnsArr.length;k++){

Stringfield=columnsArr[k];

%>

<tdclass=formtitle2align="left"nowrapstyle="width:13%;font-size:9pt;background:#ffffff;color:#000000;height:30px;text-align:left;border:1pxsolid#cccccc;"><%=map.get(field)==null?"":""+map.get(field)%></td>

<%}%>

</tr>

<%}%>

</table>

</form>

</body>

</html>

相关推荐