在客户端浏览器中点击下载生成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 ...
随机推荐
- kettle--window开发环境和linux运行环境的迁移
首先要做的是将kettle在linux下搭建好. 一.搭建linux的kettle环境 1.1解压 (my_python_env)[root@hadoop26 ~]# .zip -d /usr/loc ...
- LOADRUNNER之汉字编码转换及\X00问题
我们在使用loadrunner做性能测试的时候经常会出现一些URL编码问题,如当参数中存在中文的时候 "Name=user", "Value=孟林", ENDI ...
- MongoDB初试备份及恢复
MongoDB作为文档数据库,有 1.登录MongoDB官网,地址:https://www.mongodb.com/download-center#community , 根据自己操作系统下载相应版 ...
- 淘宝 code 使用
淘宝 code上 svn 使用,基本流程: 新建项目 mkdir 创建 branches 文件夹(新建项目的时候,只有 trunk) copy 来创建新分支 checkout 主干和(或)分支到本地 ...
- selenium常用获取元素点
//通过id WebElement element = driver.findElement(By.id("coolestWidgetEvah")); //通过className ...
- scala的hello world出现的问题
build出现: Error:scalac: Error: org.jetbrains.jps.incremental.scala.remote.ServerExceptionError compil ...
- asp.net后台正则表达式验证手机号码邮箱
//如果文本中可以为空的为NO,文本中内容不为空 if (input.nullable == "no" || !isnull(input.value)) { //文本中值的类型 s ...
- ask 调用时间标签
织梦时间调用标签大全(2012-08-03 12:50:13) 转载▼ 分类: 织梦 织梦首页时间标签1,11-20 样式([field:pubdate function='strftime(& ...
- 【Spring学习笔记-MVC-12】Spring MVC视图解析器之ResourceBundleViewResolver
场景 当我们设计程序界面的时候,中国人希望界面是中文,而美国人希望界面是英文. 我们当然希望后台代码不需改变,系统能够通过配置文件配置,来自己觉得是显示中文界面还是英文界面. 这是,Spring mv ...
- appium定位方法
1.id定位 driver.find_element_by_id("这里是resource-id") 2.name定位 (新版本的appium 1.7 已经没有这个定位方法了) d ...