实现功能:java导出excel表

1、jsp代码

  1. <form id="zhanwForm" action="<%=path%>/conferences.do?" target="_self" method="get" >
  2. <input type="hidden" name="method" value="outData"/>
  3. <input type="hidden" name="compassId" value='1'/>
  4. <input type="hidden" name="fromWhere" value="AAAA"/>
  5.  
  6. </from>

2、js代码

  1. $(".daochu1").click(function(){
  2. var bank = new Array();
  3. $.each($(".guangGaoLeiXing").find("input:checked"), function () {
  4. bank.push($(this).val());
  5. });
  6. if(bank == false){
  7. alert("请选择要统计类型");
  8. }else{
  9. $("#zhanwForm").submit();
  10. }
  11. })
  12.  
  13. $(".lyBtn").click(function(){
  14. $(".mask").show();
  15. $(".tishiDiv").show();
  16. $.ajax({
  17. url : '<%=path %>/conferences.do?method=getUserLy&fromWhere=csco',
  18. type : 'GET',
  19. dataType : 'json',
  20. success : function(data) {
  21. $(".mask").hide();
  22. $(".tishiDiv").hide();
  23. window.open(data.url);
  24. }
  25. })
  26. });

3、action处理代码

  1. //导出数据
  2. //SELECT SUBSTRING(create_time,1,10) s,COUNT(*) FROM data_read WHERE data_id IN (SELECT data_id FROM DATA WHERE model_id=2 OR model_id=27) GROUP BY s
  3. @RequestMapping(params = "method=outData",method = RequestMethod.GET)
  4. public void outCompassTongJiData(Integer compassId,String fromWhere,ModelMap model,HttpServletRequest request,HttpServletResponse response)
  5. {
  6. String urlPath = request.getScheme()+"://"+request.getServerName()+request.getContextPath();
  7. XSSFWorkbook rwb1 = new XSSFWorkbook();
  8.  
  9. while(true)
  10. {
  11. if(rwb1.getNumberOfSheets() != 0){
  12. rwb1.removeSheetAt(0);
  13. }
  14. else{
  15. break;
  16. }
  17. }
  18. try {
  19. int bankLong = 0;
  20. int bank1Long = 0;
  21. String [] bank = request.getParameterValues("bank");//广告类型选择
  22. String [] bank1 = request.getParameterValues("bank1");//身份选择
  23. String startTime = request.getParameter("startTime");
  24. String endTime = request.getParameter("endTime");
  25. String[] shenfenList = {"医生","医学生","其他"};
  26. String[] zhichengArray = {"主任医师","副主任医师","主治医师","住院医师(有执业证)","住院医师(未考执业证)","助理医师(有执业证)","助理医师(未考执业证)","其他"};
  27. String typeString1 ="";
  28. String typeString2 ="";
  29. if(bank!=null){
  30. bankLong = bank.length;
  31. for(int i=0;i<bankLong;i++){
  32. if(bank[i].equals("1")||bank[i].equals("3")||bank[i].equals("8")){
  33. typeString1 = typeString1+bank[i]+",";
  34. }else {
  35. typeString2 = typeString2+bank[i]+",";
  36. }
  37. }
  38. }
  39. if(typeString1 != null && typeString1.length() != 0){
  40. typeString1 = typeString1.substring(0, typeString1.length()-1);
  41. }
  42. if(typeString2 != null && typeString2.length() != 0){
  43. typeString2 = typeString2.substring(0, typeString2.length()-1);
  44. }
  45. if(bank1!=null){
  46. bank1Long = bank1.length;
  47. }
  48.  
  49. XSSFSheet ws1 = rwb1.createSheet("统计数据");
  50. ws1.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
  51. ws1.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
  52. ws1.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
  53. ws1.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
  54. ws1.addMergedRegion(new CellRangeAddress(0, 0, 4, 5));
  55. int count = 0;//行
  56. int rolNUm = 0;//列
  57.  
  58. XSSFRow row1 = ws1.createRow(0);
  59.  
  60. row1.createCell(rolNUm).setCellValue("类型");
  61. rolNUm++;
  62. row1.createCell(rolNUm).setCellValue("ID");
  63. rolNUm++;
  64. row1.createCell(rolNUm).setCellValue("标题");
  65. rolNUm++;
  66. row1.createCell(rolNUm).setCellValue("地址");
  67. rolNUm++;
  68. row1.createCell(rolNUm).setCellValue("点击量");//1/2
  69.  
  70. count++;
  71. XSSFRow row2 = ws1.createRow(count);
  72. row2.createCell(rolNUm).setCellValue("总点击次数");
  73. rolNUm++;
  74. row2.createCell(rolNUm).setCellValue("总点击用户数");
  75. //六种统计类型:首页广告、上方广告、session相关性广告、展商活动、动态(现场秀)、消息站
  76.  
  77. //得到需要统计的广告类型的集合
  78. List<CompassTongji> compassTongJis = conferencesService.getCompassTongJiByType(fromWhere,typeString1,typeString2,startTime,endTime);// i,group by dataId 查询不重复的data
  79. //如果不为空,遍历
  80. if(compassTongJis != null && compassTongJis.size() != 0){
  81. for(CompassTongji compassTongJi :compassTongJis){
  82. int dataId = compassTongJi.getDataId();//广告ID
  83. int type = compassTongJi.getType();
  84. String title = compassTongJi.getDataTitle();//广告标题
  85. String url1 = compassTongJi.getDataUrl()==null?"":compassTongJi.getDataUrl();//广告地址
  86. String url = "";
  87. if(type - 8 == 0){
  88. if(url1.indexOf("method=")!= -1){
  89. String[] urlStrings = url1.split("&");
  90. url = urlStrings[0]+"&"+urlStrings[1];
  91. }else{
  92. int index = url1.indexOf("?");
  93. if(index != -1){
  94. url = url1.substring(0, index);
  95. }else{
  96. url = url1;
  97. }
  98. }
  99. }else{
  100. int index = url1.indexOf("?");
  101. if(index != -1){
  102. url = url1.substring(0, index);
  103. }else{
  104. url = url1;
  105. }
  106.  
  107. }
  108.  
  109. //总点击量
  110. int totalClickNumber = conferencesService.getTotalClickNumber(dataId,url,startTime,endTime,type,fromWhere);
  111. //总点击人数
  112. int totalClickPeopleNumber = conferencesService.getTotalClickPeopleNumber(dataId,url,startTime,endTime,type,fromWhere);
  113. count++;
  114. XSSFRow row3 = ws1.createRow(count);
  115. rolNUm=0;
  116. row3.createCell(rolNUm).setCellValue(type == 1?"首页广告":type ==2?"上方广告":type==3?"session相关性广告":type==4?"展商活动":type==5?"动态(现场秀)":type==6?"消息站":type == 7?"弹窗":type == 8?"推送":"首页模块");
  117. rolNUm++;
  118. row3.createCell(rolNUm).setCellValue(dataId);
  119. rolNUm++;
  120. row3.createCell(rolNUm).setCellValue(title);
  121. rolNUm++;
  122. row3.createCell(rolNUm).setCellValue(url1);
  123. rolNUm++;
  124. row3.createCell(rolNUm).setCellValue(totalClickNumber);
  125. rolNUm++;
  126. row3.createCell(rolNUm).setCellValue(totalClickPeopleNumber);
  127. for(int j=0;j<bank1Long;j++){
  128. int tongJiZiDuan = Integer.valueOf(bank1[j]);
  129. if (tongJiZiDuan == 1) {
  130. //领域统计 userLy
  131. //先查询所有领域列表
  132. //遍历领域列表(compassfield) 根据 领域id 查询 用户表(field_id 领域ID field 领域 ) 得到 领域统计信息
  133. List<CompassField> compassList = conferencesService.getCompassList();
  134. int compassListLength = compassList.size();
  135.  
  136. ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, compassListLength+rolNUm));
  137. row1.createCell(rolNUm+1).setCellValue("领域");
  138. if(compassList != null && compassList.size()!=0){
  139. for (CompassField compassField : compassList) {
  140. int compassFieldId = compassField.getCompassFieldId();
  141. int countLy = conferencesService.getUserLyInfo(dataId,url,compassFieldId,startTime,endTime,type,fromWhere);
  142. rolNUm++;
  143. row2.createCell(rolNUm).setCellValue(compassField.getFieldName());
  144. row3.createCell(rolNUm).setCellValue(countLy);
  145. }
  146. }
  147. }
  148. if(tongJiZiDuan == 2){//科室统计 userKs
  149. List<CompassKs> compassKsListList = conferencesService.getCompassKsList();
  150. int userKsListLength = compassKsListList.size();
  151. ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, userKsListLength+rolNUm));
  152. row1.createCell(rolNUm+1).setCellValue("科室");
  153. if(compassKsListList != null && compassKsListList.size()!=0){
  154. for (CompassKs compassKs : compassKsListList) {
  155. int compassKsId = compassKs.getCompassKsId();
  156. int countKs = conferencesService.getUserKsInfo(dataId,url,compassKsId,startTime,endTime,type,fromWhere);
  157. rolNUm++;
  158. row2.createCell(rolNUm).setCellValue(compassKs.getKsName());
  159. row3.createCell(rolNUm).setCellValue(countKs);
  160. }
  161. }
  162. }
  163.  
  164. if(tongJiZiDuan == 3){//职称统计 zhicheng
  165. int userZhichengListLeng = zhichengArray.length;
  166. ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, userZhichengListLeng+rolNUm));
  167. row1.createCell(rolNUm+1).setCellValue("职称");
  168. for (int k=0;k<userZhichengListLeng;k++) {
  169. String zhicheng = zhichengArray[k];
  170. int userZhichengList = conferencesService.getUserZhichengInfo(dataId,url,startTime,endTime,zhicheng,type,fromWhere);
  171. rolNUm++;
  172. row2.createCell(rolNUm).setCellValue(zhicheng);
  173. row3.createCell(rolNUm).setCellValue(userZhichengList);
  174. }
  175.  
  176. }
  177.  
  178. if(tongJiZiDuan == 4){//身份统计
  179. int userShenfenListLength = shenfenList.length;
  180. ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, userShenfenListLength + rolNUm));
  181. row1.createCell(rolNUm+1).setCellValue("身份");
  182.  
  183. for (int k = 0;k<userShenfenListLength;k++) {
  184. String shenfen = shenfenList[k];
  185. int countShenFen = conferencesService.getClickUserShenFenInfo( dataId,url,startTime,endTime,shenfen,type,fromWhere);
  186. rolNUm++;
  187. row2.createCell(rolNUm).setCellValue(shenfen);
  188. row3.createCell(rolNUm).setCellValue(countShenFen);
  189. }
  190. }
  191. if(tongJiZiDuan == 5){//省市(市)统计
  192. List<Hospital> provinceList = conferencesService.getProvinceList();
  193.  
  194. if(provinceList != null && provinceList.size()!=0){
  195. for (Hospital hospital : provinceList) {
  196. Integer provinceId = hospital.getHospitalId();
  197. String provinceName = hospital.getProvince();
  198.  
  199. if(provinceName!=null && provinceName.length() != 0){
  200. List<Hospital> cityList = conferencesService.getCityInfoByProvinceId(provinceId);
  201.  
  202. if(cityList != null && cityList.size() != 0){
  203. int cityListLength = cityList.size();
  204. ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, rolNUm+cityListLength));
  205. row1.createCell(rolNUm+1).setCellValue(provinceName);
  206. for (Hospital hospital2 : cityList) {
  207. //得到市的名称
  208. String cityName = hospital2.getCity();
  209. Integer cityId = hospital2.getHospitalId();
  210. if(cityName!=null && cityName.length() != 0){
  211. int cityNum = conferencesService.getCityInfoByProvinceNameAndCityName(dataId,url,startTime, endTime, cityId,type,fromWhere);
  212. rolNUm++;
  213. row2.createCell(rolNUm).setCellValue(cityName);
  214. row3.createCell(rolNUm).setCellValue(cityNum);
  215. }
  216. }
  217. }
  218. }//省不为空
  219. }
  220. }
  221. }//省市统计结束
  222. }
  223. }
  224. }//最外if结束
  225.  
  226. String filePath = request.getSession().getServletContext().getRealPath("files/execl");
  227. File conFile = new File(filePath); //目录结构
  228. if(!conFile.exists())
  229. {
  230. conFile.mkdir();
  231. }
  232. String fileName = "compassTongJi.xlsx";
  233. File file = new File(filePath+"/"+fileName);
  234. if(file.exists())
  235. {
  236. file.delete();
  237. }
  238. else{
  239. file.createNewFile();
  240. }
  241. FileOutputStream fout = new FileOutputStream(file);
  242. rwb1.write(fout);
  243. fout.close();
  244. //JSONObject jsonObject = new JSONObject();
  245. //jsonObject.accumulate("url",request.getContextPath()+"/files/execl/"+fileName);
  246. //writeToJson(response, jsonObject.toString());
  247. String urlString ="<script type='text/javascript'>window.parent.open('"+urlPath+"/files/execl/"+fileName+"')</script>";
  248. PrintWriter out = response.getWriter();
  249. response.setCharacterEncoding("utf-8");
  250. response.setContentType("text/html;charset=UTF-8");
  251. out.write(urlString);
  252. out.flush();
  253. out.close();
  254. } catch (Exception e) {
  255. // TODO: handle exception
  256. e.printStackTrace();
  257. }
  258. }

梦想是一个说出来就矫情的东西,它是生长在暗地里的一颗种子,只有破土而出,拔节而长,终有一日开出花来,才能正大光明的让所有人都知道。在此之前,除了坚持,别无选择。

java 实现excel 导出功能的更多相关文章

  1. Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc

    Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...

  2. 用SpringMvc实现Excel导出功能

    以前只知道用poi导出Excel,最近用了SpringMvc的Excel导出功能,结合jxl和poi实现,的确比只用Poi好,两种实现方式如下: 一.结合jxl实现: 1.引入jxl的所需jar包: ...

  3. excel导出功能优化

    先说说优化前,怎么做EXCEL导出功能的: 1. 先定义一个VO类,类中的字段按照EXCEL的顺序定义,并且该类只能用于EXCEL导出使用,不能随便修改. 2. 将查询到的结果集循环写入到这个VO类中 ...

  4. 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能

    我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...

  5. excel导出功能原型

    本篇博客是记录自己实现的excel导出功能原型,下面我将简单介绍本原型: 这是我自制的窗体,有一个ListView和一个Button(导出)控件. 这是我在网上找到了使用exel需要引用的库. usi ...

  6. java利用EasyPoi实现Excel导出功能

    easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言( ...

  7. JAVA实现Excel导出数据(以写好的Excel模版导出)

    工作中经常会有将后台数据以Excel导出的功能. 简单的方法有将response的contentType设置为application/vnd.ms-excel: 或在JSP页面直接设置成: <% ...

  8. 使用NPOI实现简单的Excel导出功能

    [1]NPOI是啥? NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作. POI是一个开源的Java读写Excel. ...

  9. java-excel导出

    java excel导出分为两种2003年的格式和2007年的格式. 2003年的xls一个sheet限制65536. 2007年的xlsx限制为1048576. jxl导入2003 gradle j ...

随机推荐

  1. Cocos Creator 使用protobufjs

    Win7 + Creator 2.0.0 + protobufjs 6.8.8 1.下载安装protobufjs npm install -g protobufjs 可以看到protobufjs安装在 ...

  2. Unity---判断某个点是否在摄像机的视景范围内

    using UnityEngine; [RequireComponent(typeof(Camera))] public class VisualDetectionCamera : MonoBehav ...

  3. POJ-3294 Life Forms n个字符串中出现超过n/2次的最长子串(按字典序依次输出)

    按照以前两个字符串找两者的最长公共子串的思路类似,可以把所有串拼接到一起,这里为了避免讨论LCP跨越多个串需需要特别处理的问题用不同的字符把所有串隔开(因为char只有128位,和可能不够用,更推荐设 ...

  4. localStorage(本地存储器)、sessionStorage(会话存储)

      设置:localStorage.setItem("token", JSON.parse(res).data.token);   获取:that.token = localSto ...

  5. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)C. Morse Code

    题意:给你n个01字符,每次问你前缀的所有本质不同的子串,由摩斯密码组成的方案数和. 题解:离线处理,把字符建sam,通过topo序来dp计算每个节点表示的子串方案数的和.统计答案时,把n个字符挨个匹 ...

  6. python自动化测试入门篇-jemter

    接口测试基础-jemter 接口文档地址:http://doc.nnzhp.cn 使用jmeter实现简单的http request的接口测试 一.get获取学生信息接口 第一步:新建一个Thread ...

  7. 946. Validate Stack Sequences验证栈序列

    网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate ...

  8. Oracle 11g streams部署

    环境   源服务器 目标服务器 系统版本 CentOS Linux release 7.3.1611 (Core) CentOS Linux release 7.3.1611 (Core) 主机名 s ...

  9. koa和express对比

    不同: 1.启动方式不同 koa采用了new Koa()的方式,而express采用传统的函数形式 2.中间件形式二者不一样,这是由二者处理中间件的逻辑差异导致的,实际上这也是二者最根本的差别 3.k ...

  10. Wireshark 过滤 基本语法

    转载 1.过滤IP,如来源IP或者目标IP等于某个IP   例子: ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107 或者 ip.addr eq 1 ...