ssm中怎么导出excel报表,求实例。可加Q527484903

附上demo。感激不尽。

 <script type="text/javascript">
function exportExcel(){
    Ext.MessageBox.confirm("提示","您是否要导出Excel表格吗?",function(btnId){
        if(btnId == 'yes'){ 
            Ext.MessageBox.wait("正在导出Excel表格,请稍等...", "提示");                        Ext.Ajax.request({
                url:'accessrecord_exportExcel',
                method:'post',                          
                success:function(response,options){                                     Ext.Msg.alert('提示','成功导出Excel表格!');
                },
                failure:function(response,options){
                    Ext.Msg.alert('提示','发生错误,未能导出Excel表格!');
                }
            });
        }
    });
}
</script>

需要导入 jxl.jar 这个jar包

 /**
     * 导出excel
     * request和response :无需解释,fileName:文件名,retList:数据list里面存的是map,titleList:标题就是首列文字啊什么的,columnList:根据这个list去map中取值也就是说这个是map里面KEY的一个集合
     */
    public static void print(HttpServletRequest request,
            HttpServletResponse response, String fileName,
            List<Map<String, Object>> retList, List titleList, List columnList)
            throws Exception {
        final String userAgent = request.getHeader("USER-AGENT");
        if (userAgent.contains("Mozilla")) {// google,火狐浏览器
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        } else {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        }
        response.addHeader("Content-Disposition", "attachment;filename="
                + fileName);
        response.setContentType("application/octet-stream");
        OutputStream os = response.getOutputStream();
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        // 创建新的一页
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);

        // 获取内容
        for (int i = 0; i < titleList.size(); i++) {
            Label label = new Label(i, 0, (String) titleList.get(i));
            sheet.addCell(label);
        }

        for (int i = 0; i < retList.size(); i++) {
            Map map = retList.get(i);
            for (int j = 0; j < columnList.size(); j++) {
                Object cellText = map.get(columnList.get(j));
                if (cellText == null) {
                    cellText = "";
                }
                Label label = new Label(j, i + 1, cellText.toString());
                sheet.addCell(label);
            }
        }
        workbook.write();
        workbook.close();
        os.close();
    }

这是我在项目里面找的一个UTIL ,随便写了点注释,有看不懂的可以联系我 Q593921602

http://blog.csdn.net/tffboke/article/details/51785500

给你个例子

@SuppressWarnings({ "deprecation", "unchecked" })
@RequestMapping("export-newStudentList")
@ResponseBody
protected String exportNewCommer(EmployeeQuery query,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
response.reset();
// 获得国际化语言
RequestContext requestContext = new RequestContext(request);
String CourseCompany = requestContext.getMessage("manage-student");
response.setContentType("APPLICATION/vnd.ms-excel;charset=UTF-8");
// 注意,如果去掉下面一行代码中的attachment; 那么也会使IE自动打开文件。
response.setHeader(
"Content-Disposition",
"attachment; filename="
+ java.net.URLEncoder.encode(
DateUtil.getExportDate() + ".xls", "UTF-8"));
OutputStream os = response.getOutputStream();// new
query.setNeedFilte(true);
query.setJoinEndDate(DateUtil.toStringWidthoutHHMMSS(DateUtil
.addDayDate(new Date(), -7)));
EmployeeOrganizationRefQuery equery = new EmployeeOrganizationRefQuery();
equery.setEmployeeCode(CurrentUserUtil.getCurrentUserName());
List eomlist = employeeOrganizationRefService
.getEmployeeOrganizationRefList(equery);
// EmployeeOrganizationRefModel
// eom=employeeOrganizationRefService.getByEmployeeCode(CurrentUserUtil.getCurrentUserName());
if (eomlist.size() == 1) {
query.setStoreNo(eomlist.get(0).getUnitId());
}
// if (eom!=null) {
// query.setStoreNo(eom.getUnitId());
// }
List list = employeeService.getfreshList(query);
// 产生Excel表头
HSSFSheet sheet = workbook.createSheet(DateUtil.getExportDate()
+ CourseCompany);
sheet.setDefaultColumnWidth((short) 17);
// 创建属于上面Sheet的Row,参数0可以是0~65535之间的任何一个,
HSSFRow header = sheet.createRow(0); // 第0行
HSSFCellStyle style = workbook.createCellStyle();
// 设置样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        String employeeCode = requestContext.getMessage("employeeCode");
        String employeeName = requestContext.getMessage("employeeName");
        String division = requestContext.getMessage("division");
        String section = requestContext.getMessage("section");
        String joinDate = requestContext.getMessage("joinDate");
        // 表头名称数组
        String[] headerArr = new String[] { employeeCode, employeeName,
                division, section, joinDate };
        // 产生标题列
        HSSFCell cell;
        for (int i = 0; i < headerArr.length; i++) {
            cell = header.createCell((short) i);
            cell.setCellStyle(style);
            cell.setCellValue(headerArr[i]);
        }
        // 迭代数据
        if (list != null && list.size() > 0) {
            int rowNum = 1;
            for (EmployeeModel history : list) {
                HSSFRow row = sheet.createRow(rowNum++);
                row.createCell((short) 0).setCellValue(
                        history.getEmployeeCode());
                row.createCell((short) 1).setCellValue(history.getName());
                row.createCell((short) 2).setCellValue(
                        history.getDivisiName());
                row.createCell((short) 3).setCellValue(
                        history.getSectionName());
                if (history.getJoinDate() != null) {
                    row.createCell((short) 4).setCellValue(
                            DateUtil.toString(history.getJoinDate()));
                } else {
                    row.createCell((short) 4).setCellValue("");
                }

            }
        }

        workbook.write(os);
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
        return "false";
    }
    return "true";
}

http://blog.csdn.net/fiangasdre/article/details/51741580 看看我这篇文章 希望对你有帮助


var idTmr; function getExplorer() { var explorer = window.navigator.userAgent ; //ie if (explorer.indexOf("MSIE") >= 0) { return 'ie'; } //firefox else if (explorer.indexOf("Firefox") >= 0) { return 'Firefox'; } //Chrome else if(explorer.indexOf("Chrome") >= 0){ return 'Chrome'; } //Opera else if(explorer.indexOf("Opera") >= 0){ return 'Opera'; } //Safari else if(explorer.indexOf("Safari") >= 0){ return 'Safari'; } } function method1(tableid) {//整个表格拷贝到EXCEL中 if(getExplorer()=='ie') { var curTbl = document.getElementById(tableid); var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel var oWB = oXL.Workbooks.Add(); //获取workbook对象 var xlsheet = oWB.Worksheets(1); //激活当前sheet var sel = document.body.createTextRange(); sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中 sel.select(); //全选TextRange中内容 sel.execCommand("Copy"); //复制TextRange中内容 xlsheet.Paste(); //粘贴到活动的EXCEL中 oXL.Visible = true; //设置excel可见属性 try { var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls"); } catch (e) { print("Nested catch caught " + e); } finally { oWB.SaveAs(fname); oWB.Close(savechanges = false); //xls.visible = false; oXL.Quit(); oXL = null; //结束excel进程,退出完成 //window.setInterval("Cleanup();",1); idTmr = window.setInterval("Cleanup();", 1); } } else { tableToExcel(tableid) } } function Cleanup() { window.clearInterval(idTmr); CollectGarbage(); } var tableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,', template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>', base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) } return function(table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} window.location.href = uri + base64(format(template, ctx)) } })()


标识内容创建时间
1excel导出012015-07-22
2excel导出022015-07-22


onclick="javascript:method1('targetTable')" />

就是这个链接自己看看吧 http://download.csdn.net/download/zsbiubiu/8923033