在客户端浏览器中点击下载生成excel

生成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){

  }

}

猜你喜欢

转载自www.cnblogs.com/zhanzhuang/p/9066588.html