poi 导出Excel

后台 dbiName selectType 是两个查询参数

@SuppressWarnings("unchecked")
    public String export() {
        SessionUser sessionUser = (SessionUser) this.getSessionUser(Constant.Session_AdminInfo);
        try {
            List<DStorageInfo> list = storageInfoService.export(whereInfo);
            String dbiName=whereInfo.getQuerys().get("dbiName");
            String selectType=whereInfo.getQuerys().get("selectType");
            if(selectType.equals("2")){
                selectType="零库存";
            }else if(selectType.equals("1")){
                selectType="非零库存";
            }else{
                selectType="全部";
            }
            
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("错误信息");
            sheet.setColumnWidth(0,6000);
            sheet.setColumnWidth(1,6000);
            sheet.setColumnWidth(3,8000);
            HSSFCellStyle style = wb.createCellStyle();
            
            
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell=row.createCell(0);
            cell.setCellValue("搜索名称");
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cell.setCellStyle(style);
            
            cell=row.createCell(1);
            cell.setCellValue("搜索类型");
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cell.setCellStyle(style);
            
            row=sheet.createRow(1);
            cell=row.createCell(0);
            cell.setCellValue(dbiName);
            cell.setCellStyle(style);
            
            
            cell=row.createCell(1);
            cell.setCellValue(selectType);
            cell.setCellStyle(style);
            
            // 第三步创建行row:添加表头0行
            row = sheet.createRow(3);
            
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中

            // 第四步创建单元格
            cell = row.createCell(0); // 第一个单元格
            cell.setCellValue("批次编码"); // 设定值
            cell.setCellStyle(style); // 内容居中

            cell = row.createCell(1); // 第二个单元格
            cell.setCellValue("教材名称");
            cell.setCellStyle(style);
            
            cell = row.createCell(2); // 第三个单元格
            cell.setCellValue("库存数量");
            cell.setCellStyle(style);

            cell = row.createCell(3); // 第四个单元格
            cell.setCellValue("所属机构");
            cell.setCellStyle(style);
            
            
            
            for (int i = 0; i < list.size(); i++) {  
                DStorageInfo storageInfo = list.get(i);  
                //创建行  
                row = sheet.createRow(i+4);  
                //创建单元格并且添加数据  
                
                row.createCell(0).setCellValue(storageInfo.getSbiId());  
                row.createCell(1).setCellValue(storageInfo.getDbiName());  
                row.createCell(2).setCellValue(storageInfo.getSiAmount());  
                row.createCell(3).setCellValue(storageInfo.getMoiName());
                
            }  
              
            //第六步将生成excel文件保存到指定路径下  
            try {  
                FileOutputStream fout = new FileOutputStream("C:\Users\Administrator\Desktop\xxxx.xls");  
                wb.write(fout);  
                fout.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
              
            System.out.println("Excel文件生成成功...");  
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return Constant.Struts_JsonForword_DataGrid;
    }






js

function dataGridToolbar(){
    return [{
        iconCls : 'icon-ok',
        text : '导出Excel',
        handler : function() {
            
            $.messager.confirm('确认对话框', '确认导出吗?', function(r){
                if (r){
                    
                    var dbiName=$('#d_sbiId').combogrid('textbox').val();
                    var selectType=$('#s_showAll').combobox('getValue');
                    
                    $.ajax({
                        dataType : "json",
                        type : "GET",
                        traditional : true,
                        url : '/fireschool/adminJson/workStorageInfo_export',
                        data : {
                            'whereInfo.querys.dbiName':dbiName,
                            'whereInfo.querys.selectType':selectType
                        },

                        success : function(data) {
                            if(data.success){
                                $.messager.alert('系统信息', data.returnMsg, 'info');
                            }else{
                                $.messager.alert('系统信息', data.returnMsg, 'error');
                            }
                        }
                        
                    });
                    
                }
            });
        }
    }]
}
原文地址:https://www.cnblogs.com/m97i/p/8003871.html