java/Struts2 jqgrid 导出excel

11 篇文章 0 订阅

jsp:

	<script>
	$(function() {
	       // 导出EXCEL
$("#exportToExcel").click(function() {
var cols = getColProperties("gridTable");
jQuery("#gridTable").excelExport("../../shujuweihu/shujuweihu_export.action?cols=" + cols);
});
	
	});
	
	function getColProperties(id){var b = jQuery("#"+id)[0];var params = b.p.colModel;var cols = params[0].name;for ( var i = 1; i < params.length; i++) {cols += "," + params[i].name;}return cols}
	
	
	</script>
<table id="gridTable"><tbody></tbody></table>
			    <div id="gridPager"></div>
			 <input id="exportToExcel" value="导出EXCEL" type="button" />

Action:

public String export() throws Exception
	{
	   System.out.println("测试导出!");
	   List<ShuJuWeiHu> list = new ArrayList<ShuJuWeiHu>();
	   ShuJuWeiHu s=new ShuJuWeiHu();
	   s.setKey_id(1);
	   s.setItem_name("test1");
	   list.add(s);
	   ExcelUtil exUtil = new ExcelUtil();
      
		// 设置导出列名
		String properties = ServletActionContext.getRequest().getParameter("cols");
		String[] title = { "key_id", "item_name",};
		//System.out.println(properties.substring(properties.indexOf(",")+1,properties.length()));
		//rn自动列需要此步骤 properties=properties.substring(properties.indexOf(",")+1,properties.length());
		exUtil.setProperties(properties);// 设置需要导出的列

		exUtil.exportExcel("jqgrid测试导出", title, list);

		return null;
 	
	}


ExcelUtil:

 

import org.apache.log4j.Logger;
import java.lang.reflect.Field;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;



public class ExcelUtil<T> {

	private static Logger log=Logger.getLogger(ExcelUtil.class); 
	// jqgrid上显示出来的属性
	private String properties;

	private String[] cols;

	public String[] getCols() {
		return cols;
	}

	public void setCols(String[] cols) {
		this.cols = cols;
	}

	public void setProperties(String properties) {
		cols = properties.split(",");
		this.properties = properties;
	}

	/**
	 * 导出EXCEL
	 * 
	 * @param fileName
	 *            文件名/sheet名
	 * @param title
	 *            导出的EXCEL列名
	 * @param dataList
	 *            数据列表,注:一定要传与页面显示的相同的bean列表,不然会报找不到属性,
	 * @throws Exception
	 */
	public boolean exportExcel(String fileName, String[] title, List<T> dataList)
			throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		// response.setContentType("application/vnd.ms-excel");
		// response.setHeader("Content-disposition", "attachment;filename="
		// + fileName + ".xls");
		response.setContentType("aplication/vnd.ms-excel");
		response
				.addHeader("Content-Disposition", "inline; filename="
						+ new String(fileName.getBytes("GB2312"), "ISO8859_1")
						+ ".xls");

		try {
			// 创建Excel工作薄
			WritableWorkbook wwb = Workbook.createWorkbook(response
					.getOutputStream());

			// 添加第一个工作表并设置第一个Sheet的名字
			WritableSheet sheet = wwb.createSheet(fileName, 0);

			Label label;
			Field f;

			// 添加标题(title)
			if (title != null) {
				for (int i = 0; i < title.length; i++) {
					label = new Label(i, 0, title[i]);
					sheet.addCell(label);
				}
			}

			// 下面是填充数据
			if (dataList != null && dataList.size() > 0) {
				for (int i = 0; i < dataList.size(); i++) {

					for (int j = 0; j < cols.length; j++) {
						f = dataList.get(i).getClass()
								.getDeclaredField(cols[j]);
						f.setAccessible(true);
						String value = f.get(dataList.get(i)) + "";
						if (!value.equals("") && value!=null) {
							label = new Label(j, i + 1, value);
						} else {
							label = new Label(j, i + 1, "");
						}
						sheet.addCell(label);
					}
				}
			}

			wwb.write();
			// 关闭
			wwb.close();
			return true;
		} catch (Exception e) {
			log.debug("------------生成Excel异常------------");
			e.printStackTrace();
			return false;
		}
	}
}

都完事后 最重要的一步

修改:jquery.jqGrid.min.js

修改前:

excelExport:function(a){a=b.extend({exptype:"remote",url:null,oper:"oper",tag:"excel",exportOptions:{}},a||{});return this.each(function(){if(this.grid){var d;if(a.exptype=="remote"){d=b.extend({},this.p.postData);d[a.oper]=a.tag;d=jQuery.param(d);d=a.url.indexOf("?")!=-1?a.url+"&"+d:a.url+"?"+d;window.location=d}}})}})})(jQuery);

修改后:

excelExport:function(a){a=b.extend({exptype:"remote",url:a,oper:"oper",tag:"excel",exportOptions:{}},a||{});return this.each(function(){$t=this;if(this.grid)if(a.exptype=="remote"){var e=b.extend({},this.p.postData);e[a.oper]=a.tag;e=jQuery.param(e);window.location=a.url}})}})})(jQuery);


如果不改此处 JS会报错误!

 

好了到此结束 最重要的部分我已和大家分享  祝大家好运!

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值