您的位置:首页 > 其它

导出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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: