ExtJS实现Excel导出

1. 使用POI组件实现excel导出功能java

//获取问题列表
app

List<Suggestion> targetStockList = suggestionService.getSuggestionList(map);
 
        //建立一个新的Excel
        HSSFWorkbook workBook = new HSSFWorkbook();
        //建立sheet页
        HSSFSheet sheet = workBook.createSheet();
        //sheet页名称
        workBook.setSheetName(0, "targetStockList");
        //建立header页
        HSSFHeader header = sheet.getHeader();
        //设置标题居中
        header.setCenter("标题");
        
        //设置第一行为Header
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell0 = row.createCell(Short.valueOf("0"));
        HSSFCell cell1 = row.createCell(Short.valueOf("1"));
        HSSFCell cell2 = row.createCell(Short.valueOf("2"));
 
        
        // 设置字符集
        cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
        
        cell0.setCellValue("问题标题");
        cell1.setCellValue("问题描述");
        cell2.setCellValue("反馈时间");
    
        
        if(targetStockList != null && !targetStockList.isEmpty()) {
            for(int i = 0; i < targetStockList.size(); i++) {
                Suggestion targetStock = targetStockList.get(i);
                row = sheet.createRow(i + 1);
                cell0 = row.createCell(Short.valueOf("0"));
                cell1 = row.createCell(Short.valueOf("1"));
                cell2 = row.createCell(Short.valueOf("2"));
                
                // 设置字符集
                cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
                cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
                cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
                
                cell0.setCellValue(targetStock.getType());
                cell1.setCellValue(targetStock.getContent());
                cell2.setCellValue(targetStock.getPublishTime());
     
 
                
                sheet.setColumnWidth((short) 0, (short) 4000);
                sheet.setColumnWidth((short) 1, (short) 4000);
                sheet.setColumnWidth((short) 2, (short) 4000);
            }
        }
        
        //经过Response把数据以Excel格式保存
        response.reset();
        response.setContentType("application/msexcel;charset=UTF-8");
        try {
            response.addHeader("Content-Disposition", "attachment;filename=\""
                    + new String(("用户意见信息表" + ".xls").getBytes("GBK"),
                            "ISO8859_1") + "\"");
            OutputStream out = response.getOutputStream();
            workBook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

2.ExtJS调用此函数,实现Excel的导出异步

咱们知道在界面上经过一个按钮实现Excel的导出,ExtJS的按钮通常都是经过Ext.Ajax.request的异步请求实现数据提交的。可是在这里咱们不能使用异步调用,咱们须要在直接提示给用户是否保存或打开此文档。如何实现呢?函数

       其实很简单,咱们若是了解了ExtJS就是Javascript开发的,咱们就可以很容易实现此功能了。咱们在按钮的触发事件中写入此段代码就能实现Excel的函数调用了。excel

    // 初始化 Excel导出 的按钮
code

    var exportExcel = Ext.get('exportExcel');
    exportExcel.on('click', exportButtonClick);
    
    function exportButtonClick (){
         window.location.href = Ext.CONTEXT + '/admin/suggestion.do?method=exportTargetList';
};


相关文章
相关标签/搜索