jxls导出excel
2015-12-14 00:00
381 查看
JxlsUtils.java:
调用上面的方法进行生成excel:
js导出Excel:
zbxxExportTemplate.xls模板文件内容如下:
/** * */ package com.linewell.common; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Map; import javax.servlet.ServletContext; import net.sf.jxls.transformer.XLSTransformer; import org.apache.poi.ss.usermodel.Workbook; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; /** * jxls 生成excel并的导出的工具类 * @author cjianquan * 2015-1-29 */ public class JxlsUtils { public static void exportExcel(String templateFile,Map<String,Object> beans,String destFileName){ XLSTransformer xlsTransformer = new XLSTransformer(); ServletContext sc = (ServletContext) ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT); String path = sc.getRealPath("/"); templateFile = path+"\\"+templateFile; destFileName = path+"\\"+destFileName; try{ xlsTransformer.transformXLS(templateFile, beans,destFileName); }catch(Exception e){ e.printStackTrace(); throw new RuntimeException("导出excel错误"); } } }
调用上面的方法进行生成excel:
/** * 导出所选记录(装备) * @param request * @param response * @author cjianquan * 2015-1-29 */ @RequestMapping(params = "exportZbxx") public void exportZbxx (HttpServletRequest request,HttpServletResponse response){ String[] ids = request.getParameterValues("ids[]"); String conIds = ""; for(String s:ids){ conIds+="'"+s+"',"; } conIds = conIds.substring(0,conIds.length()-1); StringBuilder querySql = new StringBuilder("select ZHUANGBEI_ZBMC,"); querySql.append("ZHUANGBEI_ZBBH,(select zblx_mc from TBL_BASE_ZBLX a where a.zblx_unid=t.ZHUANGBEI_ZBLX) ZHUANGBEI_ZBLX,") .append("ZHUANGBEI_SSDWMC,") .append("ZHUANGBEI_ZBSJ,") .append("(select typename from t_s_type where typegroupid =(select id from t_s_typegroup where typegroupcode='xxcg_jczb_zwzt') and typecode=t.ZHUANGBEI_ZBZT) ZHUANGBEI_ZWZT,") .append("ZHUANGBEI_SCCJ,") .append("(select typename from t_s_type where typegroupid =(select id from t_s_typegroup where typegroupcode='xxcg_jczb_zlzk') and typecode=t.ZHUANGBEI_ZBZT) ZHUANGBEI_ZLZK,") .append("(select typename from t_s_type where typegroupid =(select id from t_s_typegroup where typegroupcode='xxlb_zbgl_zbzt') and typecode=t.ZHUANGBEI_ZBZT) ZHUANGBEI_ZBZT") .append(" from TBL_BASE_ZHUANGBEI t where ZHUANGBEI_UNID in ("+conIds+")"); List<Map<String,Object>> zbxxList = this.systemService.findForJdbc(querySql.toString()); Map<String,Object> exportBeans = new HashMap<String, Object>(); String templateFile = "export/xlsTemplates/zbxxExportTemplate.xls"; String destFileName = "export/output/zbxx.xls"; exportBeans.put("resultList", zbxxList); JxlsUtils.exportExcel(templateFile, exportBeans, destFileName); }
js导出Excel:
//导出所选记录 //add by cjianquan on 2015-1-29 function exportXls(){ var ids = []; var rows = $('#zbxxList').datagrid('getSelections'); if(rows.length<=0){ alert("请选择要导出的记录!"); return false; } for(var i=0; i<rows.length; i++){ ids.push(rows[i].id); } //ids.join(','); $.ajax({ type:'POST', async:false, data:{"ids":ids}, url:'./zbxxController.do?exportZbxx', success:function(data){ window.location.href='export/output/zbxx.xls'; }, error:function(XMLHttpRequest,textStatus,errorThrown){ alert("导出Excel失败!"); } }); }
zbxxExportTemplate.xls模板文件内容如下:
相关文章推荐
- excel 计算单元格的算式
- Eclipse3.4以上使用dropins的插件安装方式
- WebDriver多浏览器测试
- anroid Bar 进度条示例
- ejb的小例子(未能成功运行)
- 其他事件TRIGGER 4-8
- androidpn消息下推(1)
- 使用draggable插件实现对象的拖曳操作8-1
- DB2 SQL Error: SQLCODE=-418, SQLSTATE=42610, SQLERRMC=null, DRIVER=4.9.78
- spring 结合 proxool连接池 启动tomcat报错:Attempt to refer to a unregistered pool by its alias
- JAVA中的内省使用
- android文件访问实例
- 使用$.map()工具函数变更数组中的元素9-6
- 使用tabs插件展示选项卡的基本功能8-7
- fusionchart使用说明
- 文本框中事件应用(含邮箱地址验证)4-9
- 08面向对象高级特性一
- org.hibernate.MappingException: Unknown entity
- androidpn消息下推2
- 获取当前位置并利用百度地图显示