Extjs 导出excel

1前台响应

onExcelBtnClick: function(button, e, eOpts) {
        var gridpanel = this.getRunGrid();
        var cm=gridpanel.columns;
        var data=[];
        var store = gridpanel.getStore();
        store.each(function(record){
            data.push(record.data);
        },this);
        var headerIndex=[];
        Ext.Array.forEach( cm, function(item){
            if (item.xtype == "gridcolumn") {
                headerIndex.push(item.dataIndex);
            }
        });
        var headText=[];
        Ext.Array.forEach( cm, function(item){
            if (item.xtype == "gridcolumn") {
                headText.push(item.text);
            }
        });
        Ext.Ajax.request({
            disableCaching: true ,
            url: ‘../../sys/MyExportExcel!exportExcel.action‘,
            method: ‘POST‘,
            params: {
                excleData:Ext.JSON.encode(data),
                excleHeader:Ext.JSON.encode(headerIndex),
                excleText:Ext.JSON.encode(headText)
            }
        });
    },

2 后台解析数据

package com.kl.action.sys;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.kl.action.ParentAction;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/sys")
@Action(value = "MyExportExcel")
public class ExportExcel extends ActionSupport  implements 
 ServletRequestAware, ServletResponseAware {
    private String excleData;
    private String excleHeader;
    private String excleText;
    
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    
    
    public String getExcleData() {
        return excleData;
    }
    public void setExcleData(String excleData) {
        this.excleData = excleData;
    }
    public String getExcleHeader() {
        return excleHeader;
    }
    public void setExcleHeader(String excleHeader) {
        this.excleHeader = excleHeader;
    }
    public String getExcleText() {
        return excleText;
    }
    public void setExcleText(String excleText) {
        this.excleText = excleText;
    }
    public void exportExcel(){
        JSONArray excle = JSONArray.fromObject(this.excleData);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        // 解析表头
        JSONArray dataHeader = JSONArray.fromObject(this.excleText);
        HSSFRow headrow = sheet.createRow(0);
        for (int col = 0; col < dataHeader.size(); col++) {
            String mycell = dataHeader.getString(col);
            HSSFCell cell = headrow.createCell(col);
            // 定义单元格为字符串类型
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            // 在单元格中输入一些内容
            cell.setCellValue(mycell);
        }
        int r=0;
        for(Object o : excle) {
            JSONObject excleObj = (JSONObject)o;
            HSSFRow row=sheet.createRow((short)r+1);
            
            //解析列
            JSONArray dataIndex = JSONArray.fromObject(this.excleHeader);
            
            for(int col=0;col<dataIndex.size();col++){
                String mycell=dataIndex.getString(col);
                mycell=excleObj.getString(mycell);
                HSSFCell cell = row.createCell(col);
                // 定义单元格为字符串类型
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                // 在单元格中输入一些内容
                cell.setCellValue(mycell);
                
            }
            r++;
        };
        //通过Response把数据以Excel格式保存
        response.reset();
        response.setContentType("application/msexcel;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try {
            response.addHeader("Content-Disposition", "attachment;filename=\""
                    + new String(("test" + ".xls").getBytes("UTF-8"),
                            "ISO8859_1") + "\"");
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    
}


示例依赖apache  poi组件

请参考 官方案例和参考文档

http://extjs.com/forum/showthread.php?t=32400&highlight=excel

http://yeakyang.blog.163.com/blog/static/6413081200961611437835/


运行结果:





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。