在客户端浏览器中点击下载生成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){
}
}
在客户端浏览器中点击下载生成excel的更多相关文章
- 解决默写浏览器中点击input输入框时,placeholder的值不消失的方法
html中,placeholder作为input的一个属性,起到了在输入框中占位并提示的作用. 但是有一些浏览器,如chrome,当鼠标点击输入框时,placeholder的值不消失,只有输入数据才消 ...
- 在Chrome浏览器中点击链接,打开IE浏览器,跳转到指定页面并传递参数
需求: 在Chrome浏览器中点击链接,打开IE浏览器,跳转到指定页面并传递参数 过程: 一些应用软件可以通过点击URL链接启动并执行操作(例如迅雷),这是如何做到的呢? 主要是通过修改注册表,注册U ...
- django xadmin中logout页面在chrome浏览器中点击关闭页面无效
问题现象 django xadmin中logout页面在chrome浏览器中点击关闭页面无效,无法关闭相应的页面 问题原因 高版本的chrome等浏览器不支持在window.colse()的写法 问题 ...
- 解决IE浏览器中点击按钮上传无效的问题
前几天写了上传功能,点击按钮上传,在谷歌中是没有任何问题的: 但是在IE浏览器中点击没有任何效果 源代码如下: 后来发现在Firefox.IE浏览器中button标签内部可以含有其他标签,但是不能对 ...
- 用PHP实现浏览器点击下载各种格式文档的方法详解【txt apk等等】
[[注:其他文件想设置成下载文件,和下面介绍的方法一致]] 由于现在的浏览器已经可以识别txt文档格式,如果只给txt文档做一个文字链接的话,点击后只是打开一个新窗口显示txt文件的内容,并不能实现点 ...
- php中点击下载按钮后待下载文件被清空
在php中设置了文件下载,下载按钮使用表单的方式来提交 <form method="post" class="form-inline" role=&quo ...
- 有关PHP中点击下载文件的小功能
最近需要在项目里加一个可以点击导出树状目录的目录结构图, 我在网上查了之后,发现基本千篇一律, 都是使用下面这种header函数, 直接去readfile() 这个文件 header('Content ...
- 浏览器中点击链接,跳转qq添加好友的实现方式
做android三年了,都不知道到底干了啥,现在好好研究应该来得及,哈哈哈,希望看到文章的人共勉,哈哈哈(新手写文章,大佬轻喷,呜呜呜~) 好了,这篇只是记录下,项目中遇到的坑(MMP测试),哈哈哈, ...
- 【bug】使用element-ui遇到在IE浏览器中点击enter会回到登录页
1.点击el-input框,会回到登录页(IE浏览器) 外层是el-table/el-form/el-input 添加可以解决 <el-form onSubmit="return fa ...
随机推荐
- JMeter连接数据库(查询出的数据作为参数)
针对Mysql jdbc:mysql://ip:3306/数据库名?useUnicode=true&characterEncoding=utf8&allowMultiQueries=t ...
- spring 自带框架及可替换框架
spring 自带框架 可替换框架 (可替换框架)是否推荐使用 spring security shiro 推荐使用 spring aop aspectj 集成aspectj使用 Shiro 对比 S ...
- 无人驾驶之激光雷达&摄像头(主要from 速腾CEO 邱纯鑫分享)
无人驾驶之激光雷达&摄像头 (from 速腾CEO 邱纯鑫公开课分享) 根据听的一些讲座和看的书籍,个人感觉:目前现在的自动驾驶,根本问题还是在于感知(路况,周边物体,交通标识等等),控制的方 ...
- hello world之Makefile
hello world之Makefile
- <dedecms>织梦内页调用会员信息
1.织梦CMS v5.7调用文章所属会员信息标签 打开官方默认模板article_artcile.htm,我们可以提取出如下代码: {dede:memberinfos} 会员头像:<a h ...
- 关闭IE 对剪切板访问的提示
在internet 选项-“安全”选项卡-自定义级别. 在“脚本”下面找到“允许对剪切板进行编程访问”,选择“启用”即可. -END
- C# 日期格式化的中的(/)正斜杠的问题(与操作系统设置有关)
Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd" )); //这行代码, 如果你在系统日期格式默认的情况下输出 2013/0 ...
- linux $* $@ 特定位置参数
举例说:脚本名称叫test.sh 入参三个: 1 2 3运行test.sh 1 2 3后$*为"1 2 3"(一起被引号包住)$@为"1" "2&qu ...
- samba 挂载windows共享文件夹
先转载一片文章 centOS下yum安装配置samba 地址 http://blog.csdn.net/linglongwunv/article/details/5212875 遇到问题1 # ...
- bzoj 3768: spoj 4660 Binary palindrome二进制回文串
Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<= ...