页面设置参考上文。SpringMVC 下载文本文档的设置

此文是关于xls文档下载的,这个文档使用Apache的POI生成,需要的jar包可以从下面地址下载:

http://pan.baidu.com/s/1i3IJttF

下面是controller代码,比上一篇文本的少很多:

    @RequestMapping("/downloadAnnotatorListXls")
    public ModelAndView downloadAnnotatorListXls(HttpServletRequest request,HttpServletResponse response){
        String fileName="annotatorList.xls";

        response.reset();// 不加这一句的话会出现下载错误
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);   // 设定输出文件头
        response.setContentType("application/vnd.ms-excel");   // 定义输出类型 

        try {

            HSSFWorkbook xls=service.getAnnotatorListInXls();
            OutputStream ouputStream = response.getOutputStream();
            xls.write(ouputStream); 
            ouputStream.flush();
            ouputStream.close();   

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);

            request.setAttribute("error", e.getClass());
            request.setAttribute("reason", e.getMessage());
            StackTraceElement[] arr=e.getStackTrace();
            request.setAttribute("stackTraceElements", arr);

            return new ModelAndView("pages/error/index.jsp");
        }

        return null;
    }

具体xls生成请参考:

public HSSFWorkbook getAnnotatorListInXls() throws Exception{
        StringBuilder sb=new StringBuilder();
        sb.append("    select");
        sb.append("        t1.id,");
        sb.append("        t1.ownerId,");
        sb.append("        t1.ownerName,");
        sb.append("        t1.annotatorId,");
        sb.append("        t1.name,");
        sb.append("        t1.fullName,");
        sb.append("        t1.language,");
        sb.append("        format(t1.planHour,2) as planHour");
        sb.append("    from");
        sb.append("        ownership t1 ");
        sb.append("    order by");
        sb.append("        id");
        String sql=sb.toString();

        List<?> ls=this.getJdbcTemplate().query(sql, new NameValueRowMapper());

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");

        // 表头颜色
        HSSFCellStyle blueStyle = wb.createCellStyle();
        blueStyle.setBorderTop(HSSFCellStyle.BORDER_THICK);
        blueStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        blueStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        blueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

        // 表头部分"ID,OwnerId,OwnerName,AnnotatorId,Name,FullName,Language,PlanHour\r\n"
        HSSFRow row = sheet.createRow(0);//建立新行

        HSSFCell cell =row.createCell((short)0);
        cell.setCellValue("ID");
        cell.setCellStyle(blueStyle);

        cell =row.createCell((short)1);
        cell.setCellValue("OwnerId");
        cell.setCellStyle(blueStyle);

        cell =row.createCell((short)2);
        cell.setCellValue("OwnerName");
        cell.setCellStyle(blueStyle);

        cell =row.createCell((short)3);
        cell.setCellValue("AnnotatorId");
        cell.setCellStyle(blueStyle);

        cell =row.createCell((short)4);
        cell.setCellValue("Name");
        cell.setCellStyle(blueStyle);

        cell =row.createCell((short)5);
        cell.setCellValue("FullName");
        cell.setCellStyle(blueStyle);

        cell =row.createCell((short)6);
        cell.setCellValue("Language");
        cell.setCellStyle(blueStyle);

        cell =row.createCell((short)7);
        cell.setCellValue("PlanHour");
        cell.setCellStyle(blueStyle);

        short rowNum=1;
        short columnNum=0;
        for(Object obj:ls){
            row = sheet.createRow(rowNum);//建立新行
            columnNum=0;

            List<NameValue> lsTemp=(List<NameValue>)obj;

            for(NameValue nv:lsTemp){
                cell =row.createCell(columnNum);
                cell.setCellValue(nv.getValue());
                columnNum++;
            }

            rowNum++;
        }

        return wb;
    }

SpringMVC 下载XLS文档的设置的更多相关文章

  1. springmvc下载一个文档下载接口里的文档

    A提供了一个文件下载的接口,在调用的时候可以直接在前端用a标签来调用 <a href="http://" target="_blank">下载< ...

  2. IIS下不能下载文件的docx文档,XLSX文档的设置方法(转)

    IIS下不能下载文件的docx文档,XLSX文档的设置方法 Office 2007的的界面风格默认格式中都是.DOCX,XLSX,PPTX等等后缀,连结中包含此类文件时,界面风格默认什么打不开的其实只 ...

  3. Java导出freemarker实现下载word文档格式功能

    首先呢,先说一下制作freemarker模板步骤, 1. 在WPS上写出所要的下载的word格式当做模板 2. 把模板内不固定的内容(例:从数据库读取的信息)写成123或者好代替的文字标注 3. 把固 ...

  4. 配置允许匿名用户登录访问vsftpd服务,进行文档的上传下载、文档的新建删除等操作

    centos7环境下 临时关闭防火墙 #systemctl stop firewalld 临时关闭selinux #setenforce 0 安装ftp服务 #yum install vsftpd - ...

  5. NPOI(2.1.3)向excel中插入图片,xls文档图片插入成功,xlsx文档图片插入失败

    众所周知,NPOI对xls和xlsx两个版本的excel文档的操作并没有一个统一的支持, 程序若想兼容这两个版本的操作,必须根据excel版本分别去调用HSSF和XSSF这两套操作库, 之前一直不明白 ...

  6. 手动下载 Xcode 文档

    下载Xcode文档的方法有两个: 1. 自动下载:到在Xcode的Preserences中Downloads页面的Documentation,点击对应文档的下载. 2. 手动下载:到这个页面:http ...

  7. 安利一个免费下载VIP文档神器

    今天安利给大伙一个非非非常好用的可以免费下载VIP文档的下载神器------冰点文库下载器,用过的人都说好.操作简单,小巧轻便,完全免费.支持百度.豆丁.畅享.mbalib.hp009.max.boo ...

  8. C#(MVC) Word 替换,填充表格,导出并下载PDF文档

    近期做一个关于C# 操作 Word 模板 文档的功能模块,查阅资料,最终完美完成任务,记录下来,以便后面还会用到.

  9. 前端调用后台接口下载word文档的两种方法

    1传统的ajax虽然能提交到后台,但是返回的数据被解析成json,html,text等字符串,无法响应浏览器下载.就算使用bob模拟下载,数据量大时也不方便 废话不多说:上代码(此处是Layui监听提 ...

随机推荐

  1. Create Windows Server 2008 cluster from the command line

    How to create a Windows Server 2008 cluster from the command line? Creating a cluster in Server 2008 ...

  2. 快速激活最新JetBrains公司系列产品包括最新的phpstorm10

    快速激活最新JetBrains公司系列产品包括最新的phpstorm10 IntelliJ IDEA开源社区 提供了如下通用激活方法: 注册时选择License server 然后输入框填写:http ...

  3. LNMP下安装phpmyadmin的一个小错误解决办法

    环境:ubuntu16.04 + nginx1.10.0 + php7.04 + mysql5.6 安装phpmyadmin之后tail nginx 的错误日志,提示以下: 2016/06/30 15 ...

  4. RQNOJ123_多人背包_C++_Pascal

    题目:http://www.rqnoj.cn/problem/123 不得不说,RQNOJ 的机子跑得好慢呀,5*107 的数据范围本地跑 0.2s,服务器上愣是把我卡掉了,最后只好写了一份 Pasc ...

  5. 2018多校第三场 hdu6331 M :Walking Plan

    题目链接 hdu6331 自我吐槽,这场多校大失败,开局签到因输入输出格式写错,wa了3发.队友C题wa了1个小时,还硬说自己写的没错,结果我随便造了个小数据,他都没跑对.然后跑对了后又进入了无限的卡 ...

  6. 积木大赛(NOIP2013)(纯贪心+模拟)

    好吧,这道题也是..醉了. 其实题目编程挺水的,但是贪心过程不好想. 原题传送门 这道题对于任何一个点a[i]如果a[i]<a[i-1]的话,那么假设a[i-1]的高度为X,a[i]的高度为y, ...

  7. Mina框架的学习笔记——Android客户端的实现

    Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...

  8. 控制台注入DLL代码

    // zhuru.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <Windows.h> #define GameC ...

  9. sharepoint 2013 和 office web apps server 2013集成

    环境: 三台服务器  系统:window 2008 R2server01: 192.168.10.162(office web app)server02: 192.168.10.163(AD)serv ...

  10. 【NOI2016】优秀的拆分

    题目描述 如果一个字符串可以被拆分为 $AABB$ 的形式,其中 $A$ 和 $B$ 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 aabaabaa,如果令 $A = \m ...