遇到一个需求,就是把数据库里对应jqgrid显示的列的数据全部导出到EXCEL,查了查,没搞,只有夺奶和皮二皮的,没有JAVA的,没办法,人家官网上夺奶的是全自动的,那就自己写个半自动的呗。动手:
1.页面:
<td><input type="button" value="导出EXCEL" id="exportToExcel"
style="margin-right: 5px" /></td>
2.JS:
// 导出EXCEL
$("#exportToExcel").click(function() {
// 得到jqgrid显示的列属性 getColProperties(param),param-jqgrid的id
var cols = getColProperties("list");
// window.location.href = "test?cols=" + cols;
jQuery("#list").excelExport("exportPcToExcel?cols=" + cols);
});
================说明:为了找回夺奶的平衡,我改了一点点jqGrid的源码,如下:
第一处:
excelExport:function(a){a=b.extend({exptype:"remote",url:null,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+"?"+e}})}})})(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);
第二处:
在JQGRID里找个空地儿塞上去(这是我JS调用是用的):
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}
3.ACTION代码:
/**
* XXXXXXXXEXCEL
*
* @return
* @throws Exception
*/
@Action(value = "exportPcToExcel")
@SkipValidation()
public String exportPcToExcel() throws Exception {
// 获取数据
List<WpPc> wppcs = materialDepositService.findAll();
CommonUtil util = CommonUtil.getInstance();
for (WpPc wpPc : wppcs) {
WpPcBean wpPcBean = new WpPcBean();
wpPcBean.setPdh(wpPc.getPdh());
wpPcBean.setKfName(util.findOrgNameById(wpPc.getKfid()));
wpPcBean.setPdrxm(util.findUserNameById(wpPc.getPdr()));
wpPcBean.setPcdSj(wpPc.getPdsj());
wpPcBean.setZt(wpPc.getZt());
records.add(wpPcBean);
}
ExcelUtil exUtil = new ExcelUtil();
// 设置导出列名
String properties = ServletActionContext.getRequest().getParameter(
"cols");
String[] title = { "XXXX", "XXXX", "XXX", "XXXX", "XX" };
exUtil.setProperties(properties);// 设置需要导出的列
exUtil.exportExcel("XXXXXX", title, records);
return null;
}
4.最后,就是自己写的util了,
package com.zdsoft.common.util.excel;
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;
import com.zdsoft.common.util.StringUtil;
/**
* @author wuyj
*/
public class ExcelUtil<T> {
private 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列表,不然会报找不到属性,
* eg:页面-->WpPcBean的List,这里-->WpPcBean的List
* @return
* @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 (!StringUtil.isBlank(value)) {
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;
}
}
}