java easyui表格导出exceL,easyui导出Excel、Word java

该博客详细介绍了如何使用EasyUI的DataGrid组件结合Java后台实现Excel和Word文件的导出。首先,在前端页面上设置JavaScript来获取DataGrid的数据并转换为HTML格式。然后,通过定义按钮触发导出操作,调用export.js中的expt函数,该函数遍历DataGrid的列和行信息生成表格字符串。在export.jsp页面中,接收这个字符串并根据用户选择的导出类型(Excel或Word)进行处理,最后由后台Java代码完成文件的创建和下载。后台代码主要负责设置响应头,读取前端传递的表格字符串,并根据类型生成相应文件格式的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:easyui datagrid导出Excel,使用java后台适用于多个页面

解决方案:第一:在含有datagrid的界面exportPage.jsp中引用js(主要用于获取datagrid数据以及转换为html格式)

代码为:

第二:在exportPage.jsp的javascritp编辑中设置变量var grid='';var exportString='';并且在

之间添加一个div,主要用于弹出对话框,选择导出格式:代码:

第三:将exportPage.jsp的datagrid初始化时赋值于grid,

代码为:

grid=$('#dg').datagrid({

url:.....,

toolbar: [{

text:'Export',

iconCls: 'icon-excel',//自定义的css样式图片

handler: function(){

expt(grid);

}

},'-']

});

第四:export.js的代码为:

function expt(grid){

var tableString = '

var frozenColumns = grid.datagrid("options").frozenColumns; // 得到frozenColumns对象

var columns = grid.datagrid("options").columns; // 得到columns对象

var nameList = new Array();

// 载入title

if (typeof columns != 'undefined' && columns != '') {

$(columns).each(function (index) {

tableString += '\n

';

if (typeof frozenColumns != 'undefined' && typeof frozenColumns[index] != 'undefined') {

for (var i = 0; i < frozenColumns[index].length; ++i) {

if (!frozenColumns[index][i].hidden) {

tableString += '\n

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 + '

';

}

}

}

for (var i = 0; i < columns[index].length; ++i) {

if (!columns[index][i].hidden) {

tableString += '\n

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 + '

';

}

}

tableString += '\n

';

});

}

// 载入内容

var rows = grid.datagrid("getRows"); // 这段代码是获取当前页的所有行

for (var i = 0; i < rows.length; ++i) {

tableString += '\n

';

for (var j = 0; j < nameList.length; ++j) {

var e = nameList[j].field.lastIndexOf('_0');

tableString += '\n

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 += '

';

}

tableString += '\n

';

}

tableString += '\n

';

$('#hlf').val(tableString);

exportString=tableString;

var url="../export.jsp";

var param2={

doSize:false,

shadow:false,

content:'',

title:'Export',

width:300,

height:170,

modal:true

};

mpgdialog(param2);

}

function mpgdialog(param){

$('#dialog2').dialog(param);

$('#dialog2').window('center');

}

第五:export.jsp代码为:

pageEncoding="gb2312"%>

//var currTabId=parent.currTabId;

//var parentObj=parent.$("#"+currTabId)[0].contentWindow;

window.onload=function(){

document.getElementById("hlf").value=parent.exportString;

};

function btnExcel(){

document.getElementById("type").value="excel";

document.getElementById("formAction").submit();

}

function btnWord(){

document.getElementById("type").value="word";

document.getElementById("formAction").submit();

}

Please select the type of export for export ......

第五:后台代码为:

package com.ieslab.eim.login.action;

import java.io.IOException;

import java.io.PrintWriter;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

public class Export extends Action{

public ActionForward execute(ActionMapping actionMapping,

ActionForm actionForm,

HttpServletRequest request,

HttpServletResponse response) {

response.reset();

try {

request.setCharacterEncoding("UTF-8");

} catch (UnsupportedEncodingException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

String hf=request.getParameter("hfs");

String type=request.getParameter("type");

String exportname="grid";

try {

if(type.equals("excel"))

{

exportname+=".xls";

response.setHeader("Content-disposition", "attachment; filename="+java.net.URLEncoder.encode(exportname, "UTF-8")+"");

response.setContentType("application/msexcel;charset=utf-8");

}

else if(type.equals("word"))

{

exportname+=".doc";

response.setHeader("Content-disposition", "attachment; filename="+java.net.URLEncoder.encode(exportname, "UTF-8")+"");

response.setContentType("application/ms-word;charset=UTF-8");

}

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

PrintWriter out;

try {

out = response.getWriter();

out.println(hf);

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return actionMapping.findForward("failure");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值