easyui导出Excel、Word java

问题描述: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';  
                    }  
                }  
            }  
            for (var i = 0; i < columns[index].length; ++i) {  
                if (!columns[index][i].hidden) {  
                    tableString += '\n';  
                }  
            }  
            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',
			title:'Export',
		    width:300,    
		    height:170,
		    modal:true   	
	};
	mpgdialog(param2);
} 


function mpgdialog(param){	
		$('#dialog2').dialog(param);
		$('#dialog2').window('center');
	}
 
  



第五:export.jsp代码为:

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











     
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");
	 }


}




你可能感兴趣的:(技术)

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