导出excel
2015-08-07 17:25
344 查看
<div id="rds_list_toolbar" style="padding:5px;"> <a id="slowLogs_excel_btn" href="javaScript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit'">导出Excel</a> </div>
点击之后:
$("#slowLogs_excel_btn").click(function(){ //获取查询加载数据的总数 var total=dataGridTable.datagrid('getData').total; console.info(total); var url=webContext+'/rds/rds_exportSlowLogsList.action?rds.dbinstanceid='+dbinstanceid +'&engine='+engine+'&start_time='+$('#start_time').datebox('getValue') +'&end_time='+$('#end_time').datebox('getValue')+'&dBName='+$('#dBName').combobox('getValue') +'&total='+total; $('#slowLogs_excel_btn').attr("href", url); /* Ajax('/rds/rds_exportSlowLogsList.action?rds.dbinstanceid='+dbinstanceid+'&engine='+engine,{ start_time:$('#start_time').datebox('getValue'), end_time:$('#end_time').datebox('getValue'), dBName:$('#dBName').combobox('getValue') },function(data){ if(data.success){ //Alert('提示','删除成功!'); }else{ Alert('提示','导出excel失败:'+data.msg); } }); */ });
java代码(action):
package com.aliyun.util.excel; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.sinopec.common.excel.exp.ExcelExportColAttr.CellValType; import com.sinopec.common.exception.BaseRuntimeException; import com.sinopec.common.util.PlatForm; import com.sinopec.system.SysConstants; import com.sinopec.system.workflow.service.imp.WkflwManagerImp; @SuppressWarnings("unused") public abstract class ExcelExport { private static Logger log = Logger.getLogger(WkflwManagerImp.class.getName()); /** * 声明一个工作薄由子类完成其对象的创建 */ protected XSSFWorkbook xworkbook = null; /** * 具体的导出excel数据的业务实现类 */ protected ExportDateTemplate eeb = null; protected ExportDateTemplate[] eebes = null; /** * 请求响应对象 */ private HttpServletResponse response; public ExcelExport(ExportDateTemplate eeb, HttpServletResponse response) { this.eeb = eeb; this.response = response; } public ExcelExport(ExportDateTemplate[] eebes, HttpServletResponse response) { this.eebes = eebes; this.response = response; } public abstract void createXSSFWorkbook()throws Exception; /** * 调用此方法,导出excel数据 * @author hanxg * @date 2012-11-27 */ public void export(){ OutputStream outs = null; try { outs = response.getOutputStream(); this.setResponse(); // outs = new FileOutputStream(new File("d:\\testexport.xls")); createXSSFWorkbook(); if (xworkbook == null) throw new BaseRuntimeException("创建工作薄失败"); xworkbook.write(outs); outs.flush(); } catch (Exception e) { throw new BaseRuntimeException("创建WritableWorkbook发生异常", e); }finally { try { if(outs!=null){ outs.close(); } } catch (Exception e) { throw new BaseRuntimeException("导出excel关闭流时发生异常", e); } } } private void setResponse() { String newName = null; try { if(eeb!=null) newName = eeb.getFileName(); else newName =eebes[0].getFileName(); newName=new String(newName.getBytes("GBK"), "iso8859-1"); // //weblogic // if(SysConstants.APPSERVER.equalsIgnoreCase("weblogic")){ // newName=new String(newName.getBytes(), "iso8859-1"); // } // else{//tomcat // if(eeb!=null) // newName = new String(newName.getBytes(), "iso8859-1"); // else // newName = new String(newName.getBytes(), "iso8859-1"); // } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("application/octet-stream;"); response.setHeader("Content-Disposition", "attachment;filename=" + newName); } public static void main(String[] args) throws FileNotFoundException{ // InputStream input=Thread.currentThread().getContextClassLoader().getSystemResourceAsStream("com/sinopec/common/excel/imp/testbean.xlsx"); // FileOutputStream ost=new FileOutputStream(new File("d:testexport.xlsx")); // TestTemplate tt=new TestTemplate(); // ExcelExport ee=new ExcelNoTemExport(tt,null); // ExcelExport eet=new ExcelTemExport(tt,null); //有模板 // ee.export(); // eet.export(); } }
View Code
相关文章推荐
- MongoDB Shell
- java equals()用法错误解析
- node msgpack5 数据传输 简单实现
- VS2008 fstream 编译出错的解决方法
- Rational Rose 2007(v7.0)下载地址、安装及激活详解教程
- IOS学习之委托和block
- poj2828(线段树)
- 使用jmx 监控zookeeper
- Java经典位运算实例
- Ambari Install Hadoop ecosystem for 9 steps
- SearchLookUpEdit 和 GridLookUpEdit
- RedHat 4下无resize2fs命令
- 自写网站阶段之:终结篇
- HDU 5363 Key Set(快速幂取模)
- Spring整合Quartz
- 亲历基本OSGI实例,进入另番思维领域(转)----包括打包发布为可执行文件
- JSONModel解析数据成Model
- cmake 命令行
- LWIP互联网资料汇总
- Android TextView滑动