生成excel的样式,里面的数据已经写好,使用apache,poi来写的。

1.首先是controller

/**

*下载服务结构体Excel

*

*@return

*/

@RequestMapping(value="/Downloadexcel.do")

@ResponseBody

public JsonRpcResponse structtureFileDownload(HttpServletRequest request,HttpServletResponse response){

  JsonRpcResponse jsonRpcResponse = new JsonRpcResponse();

  try{

    //获取服务实例编号

    String serviceId = request.getParameter("serviceId");

    if(StringUtils.isNotBlank(serviceId)){

      //调用service层下载excel方法

      Boolean flag = serviceDyService.DownloadService(serviceId,response);

      if(flag){

        jsonRpcResponse.setSuccess(true);

        jsonRpcResponse.setMessage("实例结构体文件下载成功");

      }else{

        jsonRpcResponse.setSuccess(false);

        jsonRpcResponse.setMessage("服务实例不存在,请输入正确的服务实例编号");

      }

    }else{

      jsonRpcResponse.setSuccess(false);

      jsonRpcResponse.setMessage("请输入需要下载的服务实例编号");

    }

  }catch(Exception e){

    jsonRpcResponse.setSuccess(false);

    jsonRpcResponse.setMessage("实例结构体文件下载失败!");

    e.printStackTrace();

  }

  return jsonRpcResponse;

}

2.然后是service

/**

*下载对应的服务结构体excel文档

*

*@return

**/

@SuppressWarnings({"unchecked","rawtypes"})

public boolean DownService(String serviceId,HttpServletResponse response){

  //通过数据元编码查询所有数据

  String sql = "select * from .... where service_id = ' "+serviceId+" ' ";

  List<Map<String,Object>> listAllData = jdbcTemplate.queryForList(sql);

  for(int z = 0 ; z < listAllData.size() ; z ++ ){

    Object[] oo = new Object[23];

    oo[0] = listAllData.get(z).get("SERVICE_ID");

    oo[1] = listAllData.get(z).get("SERVICE_NAME");

    oo[2] = listAllData.get(z).get("AA");

    oo[3] = listAllData.get(z).get("SOURCE_ID");

    oo[4] = listAllData.get(z).get("DATA_CHINESE_NAME");

    oo[5] = listAllData.get(z).get("DATA_ENGLISH_NAME");

    oo[6] = listAllData.get(z).get("BUSINESS_DATA_TYPE");

    oo[7] = listAllData.get(z).get("BUSINESS_DATA_LENGTH");

    oo[8] = listAllData.get(z).get("BUSINESS_DATA_ACCURACY");

    oo[9] = listAllData.get(z).get("BUSINESS_UNIT");

    oo[10] = listAllData.get(z).get("BUSINESS_RULE");

    oo[11] = listAllData.get(z).get("ENGLISH_ABBREVIATION");

    oo[12] = listAllData.get(z).get("TCHNICAL_DATA_TYPE_DOMAIN");

    oo[13] = listAllData.get(z).get("TCHNICAL_DATA_TYPE");

    oo[14] = listAllData.get(z).get("TCHNICAL_DATA_LENGTH");

    oo[15] = listAllData.get(z).get("TCHNICAL_DATA_ACCURACY");

    oo[16] = listAllData.get(z).get("BB");

    oo[17] = listAllData.get(z).get("CC");

    oo[18] = listAllData.get(z).get("FLAG");

    oo[19] = listAllData.get(z).get("GROUP_EN_NAME");

    oo[20] = listAllData.get(z).get("PARENT_GROUP_CODE");

    oo[21] = listAllData.get(z).get("SEQID");

    oo[22] = listAllData.get(z).get("SEQ_ID");

    dataList.add(oo);

  }

  //查询一共有多少条数据元编码

  int countElementEncoding = jdbcTemplate.queryForObject("select count(...) from ... where ... = ...");

  String title = "交易数据";

  String[] rowsName = new String[]{"服务编号","服务名称","字段类型","数据元编码","中文名称","英文名称","数据类型","数据长度","数据精度","单位","业务规则","英文缩写","数据元类型域","数据存储类型","数据长度","数据精度","数据格式","说明","是否重复","重复组结构体名","父结构体名"," "," "};

  ExportExcel ex = new ExportExcel(countElementEncoding,title,rowsName,dataList,reponse);

  try{

    ex.export();

  }catch (Exception e1){

    e1.printStackTrace();

  }

  if(listAllData.isEmpty()){

    return false;

  }

  return true;

}

3.然后是ExportExcel

public class ExportExcel{

  private int countElementEncoding;//数据元编码条数

  private String title;//显示导出表的标题

  private String[] rowName;//导出表的列名

  private List<Object[]> dataList = new ArrayList<Object[]>();

  private HttpServletResponse response;

  public ExportExcel(Integer countElementEncoding,String title,String[] rowName,List<Object[]> dataList,HttpServletResponse response){

    super();

    this.countElementEncoding = countElementEncoding;

    this.title = title;

    this.rowName = rowName;

    this.dataList = dataList;

    this.response = response;

  }

  //导出数据

  public void export() throws Exception{

    try{

      HSSFWorkbook workbook = new HSSFWorkbook();//创建工作薄对象

      HSSFSheet sheet = workbook.createSheet(title);

      //产生表格标题行,第一行

      HSSFRow rowm1 = sheet.createRow(0);

      HSSFCell cell1 = rowm1.createCell(0);

      HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);

      HSSFCellStyle style = this.getStyle(workbook);

      //创建文本筛选框

      org.apache.poi.ss.util.CellRangeAddress c1 = org.apache.poi.ss.util.CellRangeAddress.valueOf("A2:U2");

      sheet.setAutoFilter(c1);

      //定义所需要的列数

      int columnNum = rowName.length;

      HSSFRow rowRowName1 = sheet.createRow(0);

      HSSFRow rowRowName2 = sheet.createRow(1);

      rowRowName1.setHeight((short)(15*15));//设置第一行高度

      rowRowName2.setHeight((short)(20*25));//设置第二行高度

      //将列头设置到sheet单元格中

      for(int n = 0 ; n < columnNum; n ++){

        //设置第一行的值

        HSSFCell cellRowName1 = rowRowName1.createCell(n);//创建列头对应个数的单元格

        cellRowName1.setCellType(HSSFCell.CELL_TYPE_STRING);//创建列头单元格的数据类型

        HSSFRichTextString text1 = new HSSFRichTextString(rowName[n]);

        cellRowName1.setCellValue(text1);//设置列头单元格的值

        cellRowName1.setCellStyle(columnTopStyle);//设置列头单元格样式

        //设置第二行的值

        HSSFCell cellRowName2 = rowRowName2.createCell(n);//创建列头对应个数的单元格

        cellRowName2.setCellType(HSSFCell.CELL_TYPE_STRING);//设置列头单元格的数据类型

        HSSFRichTextString text2 = new HSSFRichTextString(rowName[n]);

        cellRowName2.setCellValue(text2);// 设置列头单元格的值

        cellRowName2.setCellStyle(columnTopStyle);//设置列头单元格的样式

      }

      //合并业务属性

      HSSFRow rowywsx = sheet.getRow(0);

      HSSFCell cellywsx = rowywsx.getCell(6);

      cellywsx.setCellValue("业务属性");

      //合并技术属性

      HSSFRow rowjssx = sheet.getRow(0);

      HSSFCell celljssx = rowjssx.getCell(11);

      cellywsx.setCellValue("业务属性");

      //合并重复组

      HSSFRow rowcfz = sheet.getRow(0);

      HSSFCell cellcfz = rowcfz.getCell(18);

      cellcfz.setCellValue("重复组");

      //将前三列合并

      sheet.addMergeRegtion(new CellRangeAddress(2,countElementEncoding+1,0,0));

      sheet.addMergeRegtion(new CellRangeAddress(2,countElementEncoding+1,1,1));

      sheet.addMergeRegtion(new CellRangeAddress(2,countElementEncoding+1,2,2));

      

      //将第一行与第二行合并

      sheet.addMergeRegtion(new CellRangeAddress(0,1,0,0));

      sheet.addMergeRegtion(new CellRangeAddress(0,1,1,1));

      sheet.addMergeRegtion(new CellRangeAddress(0,1,2,2));

      sheet.addMergeRegtion(new CellRangeAddress(0,1,3,3));

      sheet.addMergeRegtion(new CellRangeAddress(0,1,4,4));

      sheet.addMergeRegtion(new CellRangeAddress(0,1,5,5));

      

      //合并业务属性

      sheet.addMergeRegion(new CellRangeAddress(0,0,6,10));

      //合并技术属性

      sheet.addMergeRegion(new CellRangeAddress(0,0,11,16));

      //合并说明

      sheet.addMergeRegion(new CellRangeAddress(0,1,17,17));

      //合并重复组

      sheet.addMergeRegion(new CellRangeAddress(0,0,18,20));

      

      //将查询出的数据设置到sheet对应的单元格中

      for(int i = 0 ; i < dataList.size() ; i ++){

        Object[] obj = dataList.get(i);//遍历每个对象

        HSSFRow row = sheet.createRow(i+2);//创建所需要的行数

        row.setHeight((short)(25*35));//设置第三行开始的单元格高度

        for(int j = 0 ; j < obj.length ; j ++ ){

          HSSFCell cell = null;//设置单元格的数据类型

          cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);

          if(!" ".equals(obj[j])&& obj[j] ! = null){

            cell.setCellValue(obj[j].toString());//设置单元格的值

          }

          cell.setCellStyle();//设置单元格样式

        }

      }

      

      if(workbook ! = null){

        try{

          response.setContentType("application/vnd.ms-excel;charset=uft-8");

          response.setHeader("Content-Disposition","attachment;filename=""+new String("服务定义与结构体.xls".getBytes("gb2312"),"ISO8850-1"));

          OutputStream out = response.getOutputStream();

          workbook.write(out);

          out.close();

        } catch (IOException e ){

          e.printStackTrace();

        }

      }

    }catch(Exception e){

    e.printStackTrace();

    

    }

  }

  //设置列头单元格样式

  public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook){

  }

  //设置列数据单元格样式

  public HSSFCellStyle getStyle(HSSFWorkbook workbook){

  }

}

在客户端浏览器中点击下载生成excel的更多相关文章

  1. 解决默写浏览器中点击input输入框时,placeholder的值不消失的方法

    html中,placeholder作为input的一个属性,起到了在输入框中占位并提示的作用. 但是有一些浏览器,如chrome,当鼠标点击输入框时,placeholder的值不消失,只有输入数据才消 ...

  2. 在Chrome浏览器中点击链接,打开IE浏览器,跳转到指定页面并传递参数

    需求: 在Chrome浏览器中点击链接,打开IE浏览器,跳转到指定页面并传递参数 过程: 一些应用软件可以通过点击URL链接启动并执行操作(例如迅雷),这是如何做到的呢? 主要是通过修改注册表,注册U ...

  3. django xadmin中logout页面在chrome浏览器中点击关闭页面无效

    问题现象 django xadmin中logout页面在chrome浏览器中点击关闭页面无效,无法关闭相应的页面 问题原因 高版本的chrome等浏览器不支持在window.colse()的写法 问题 ...

  4. 解决IE浏览器中点击按钮上传无效的问题

    前几天写了上传功能,点击按钮上传,在谷歌中是没有任何问题的: 但是在IE浏览器中点击没有任何效果 源代码如下:  后来发现在Firefox.IE浏览器中button标签内部可以含有其他标签,但是不能对 ...

  5. 用PHP实现浏览器点击下载各种格式文档的方法详解【txt apk等等】

    [[注:其他文件想设置成下载文件,和下面介绍的方法一致]] 由于现在的浏览器已经可以识别txt文档格式,如果只给txt文档做一个文字链接的话,点击后只是打开一个新窗口显示txt文件的内容,并不能实现点 ...

  6. php中点击下载按钮后待下载文件被清空

    在php中设置了文件下载,下载按钮使用表单的方式来提交 <form method="post" class="form-inline" role=&quo ...

  7. 有关PHP中点击下载文件的小功能

    最近需要在项目里加一个可以点击导出树状目录的目录结构图, 我在网上查了之后,发现基本千篇一律, 都是使用下面这种header函数, 直接去readfile() 这个文件 header('Content ...

  8. 浏览器中点击链接,跳转qq添加好友的实现方式

    做android三年了,都不知道到底干了啥,现在好好研究应该来得及,哈哈哈,希望看到文章的人共勉,哈哈哈(新手写文章,大佬轻喷,呜呜呜~) 好了,这篇只是记录下,项目中遇到的坑(MMP测试),哈哈哈, ...

  9. 【bug】使用element-ui遇到在IE浏览器中点击enter会回到登录页

    1.点击el-input框,会回到登录页(IE浏览器) 外层是el-table/el-form/el-input 添加可以解决 <el-form onSubmit="return fa ...

随机推荐

  1. 理解git

    为了真正了解git,我们从底部.底层开始,了解git核心,知其然并知其所以然. 为什么要进行版本控制呢? 因为编写文件不可能一次到位,文件总是有不同的状态需要保存下来,方便以后出错回滚. git 是目 ...

  2. PHP经典乱码“口”字与解决办法

    这几天看了看 Ajax 的基础知识,在练习一个简单的 请求和响应时,PHP 返回来的数据 在 IE 中开头总显示 一个 “锘” 字!上网 Baidu 了一下,发现这是由于 系统 处理 UTF-8 的方 ...

  3. zstack(一)运行及开发环境搭建及说明(转载)

    本篇介绍zstack的部署环境,以及二次开发环境 运行环境 讲真,ZStack的安装做的还是不错的,提供多种安装模式,如离线安装.在线安装.一键安装.分布式安装等.安装的过程其实都很简单,当然这也是z ...

  4. CountDownLatch、CyclicBarrier、Semaphore 区别

    CountDownLatch.CyclicBarrier.Semaphore 区别: CountDownLatch和CyclicBarrier都能够实现线程之间的等待,只不过它们侧重点不同: Coun ...

  5. telinit:Did not receive a reply.Possible causes include:the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired

    问题: Enabling /etc/fstab swaps: [ok]telinit:Did not receive a reply.Possible causes include:the remot ...

  6. 洛谷 3784(bzoj 4913) [SDOI2017]遗忘的集合——多项式求ln+MTT

    题目:https://www.luogu.org/problemnew/show/P3784 https://www.lydsy.com/JudgeOnline/problem.php?id=4913 ...

  7. bzoj 3572 [Hnoi2014]世界树——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3572 关于虚树:https://www.cnblogs.com/zzqsblog/p/556 ...

  8. Package rdkafka was not found in the pkg-config search path.

    问题 在使用confluent-kafka-go 时遇到如下问题: $ go build t.go # pkg-config --cflags rdkafka Package rdkafka was ...

  9. x86 openwrt虚拟路由代理上网

    一.代理服务器设置 1.下载代理软件CCProxy 6.8 Build 2.设置如下 二.x86 路由设置 1.在/etc目录下编辑profile http_proxy= https_proxy= f ...

  10. 【比特币】SPV是如何工作的

    SPV是如何工作的 SPV, Bloom 过滤器和检查点 这是一篇技术文章,获取比特币的工作知识. 一个完整的节点,比如比特币核心,知道以下几点: 每一个当前正在围绕网络广播事务处理 每一个曾经被送到 ...