java 实现excel 导出功能
实现功能:java导出excel表
1、jsp代码
- <form id="zhanwForm" action="<%=path%>/conferences.do?" target="_self" method="get" >
- <input type="hidden" name="method" value="outData"/>
- <input type="hidden" name="compassId" value='1'/>
- <input type="hidden" name="fromWhere" value="AAAA"/>
- </from>
2、js代码
- $(".daochu1").click(function(){
- var bank = new Array();
- $.each($(".guangGaoLeiXing").find("input:checked"), function () {
- bank.push($(this).val());
- });
- if(bank == false){
- alert("请选择要统计类型");
- }else{
- $("#zhanwForm").submit();
- }
- })
- $(".lyBtn").click(function(){
- $(".mask").show();
- $(".tishiDiv").show();
- $.ajax({
- url : '<%=path %>/conferences.do?method=getUserLy&fromWhere=csco',
- type : 'GET',
- dataType : 'json',
- success : function(data) {
- $(".mask").hide();
- $(".tishiDiv").hide();
- window.open(data.url);
- }
- })
- });
3、action处理代码
- //导出数据
- //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
- @RequestMapping(params = "method=outData",method = RequestMethod.GET)
- public void outCompassTongJiData(Integer compassId,String fromWhere,ModelMap model,HttpServletRequest request,HttpServletResponse response)
- {
- String urlPath = request.getScheme()+"://"+request.getServerName()+request.getContextPath();
- XSSFWorkbook rwb1 = new XSSFWorkbook();
- while(true)
- {
- if(rwb1.getNumberOfSheets() != 0){
- rwb1.removeSheetAt(0);
- }
- else{
- break;
- }
- }
- try {
- int bankLong = 0;
- int bank1Long = 0;
- String [] bank = request.getParameterValues("bank");//广告类型选择
- String [] bank1 = request.getParameterValues("bank1");//身份选择
- String startTime = request.getParameter("startTime");
- String endTime = request.getParameter("endTime");
- String[] shenfenList = {"医生","医学生","其他"};
- String[] zhichengArray = {"主任医师","副主任医师","主治医师","住院医师(有执业证)","住院医师(未考执业证)","助理医师(有执业证)","助理医师(未考执业证)","其他"};
- String typeString1 ="";
- String typeString2 ="";
- if(bank!=null){
- bankLong = bank.length;
- for(int i=0;i<bankLong;i++){
- if(bank[i].equals("1")||bank[i].equals("3")||bank[i].equals("8")){
- typeString1 = typeString1+bank[i]+",";
- }else {
- typeString2 = typeString2+bank[i]+",";
- }
- }
- }
- if(typeString1 != null && typeString1.length() != 0){
- typeString1 = typeString1.substring(0, typeString1.length()-1);
- }
- if(typeString2 != null && typeString2.length() != 0){
- typeString2 = typeString2.substring(0, typeString2.length()-1);
- }
- if(bank1!=null){
- bank1Long = bank1.length;
- }
- XSSFSheet ws1 = rwb1.createSheet("统计数据");
- ws1.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
- ws1.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
- ws1.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
- ws1.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
- ws1.addMergedRegion(new CellRangeAddress(0, 0, 4, 5));
- int count = 0;//行
- int rolNUm = 0;//列
- XSSFRow row1 = ws1.createRow(0);
- row1.createCell(rolNUm).setCellValue("类型");
- rolNUm++;
- row1.createCell(rolNUm).setCellValue("ID");
- rolNUm++;
- row1.createCell(rolNUm).setCellValue("标题");
- rolNUm++;
- row1.createCell(rolNUm).setCellValue("地址");
- rolNUm++;
- row1.createCell(rolNUm).setCellValue("点击量");//1/2
- count++;
- XSSFRow row2 = ws1.createRow(count);
- row2.createCell(rolNUm).setCellValue("总点击次数");
- rolNUm++;
- row2.createCell(rolNUm).setCellValue("总点击用户数");
- //六种统计类型:首页广告、上方广告、session相关性广告、展商活动、动态(现场秀)、消息站
- //得到需要统计的广告类型的集合
- List<CompassTongji> compassTongJis = conferencesService.getCompassTongJiByType(fromWhere,typeString1,typeString2,startTime,endTime);// i,group by dataId 查询不重复的data
- //如果不为空,遍历
- if(compassTongJis != null && compassTongJis.size() != 0){
- for(CompassTongji compassTongJi :compassTongJis){
- int dataId = compassTongJi.getDataId();//广告ID
- int type = compassTongJi.getType();
- String title = compassTongJi.getDataTitle();//广告标题
- String url1 = compassTongJi.getDataUrl()==null?"":compassTongJi.getDataUrl();//广告地址
- String url = "";
- if(type - 8 == 0){
- if(url1.indexOf("method=")!= -1){
- String[] urlStrings = url1.split("&");
- url = urlStrings[0]+"&"+urlStrings[1];
- }else{
- int index = url1.indexOf("?");
- if(index != -1){
- url = url1.substring(0, index);
- }else{
- url = url1;
- }
- }
- }else{
- int index = url1.indexOf("?");
- if(index != -1){
- url = url1.substring(0, index);
- }else{
- url = url1;
- }
- }
- //总点击量
- int totalClickNumber = conferencesService.getTotalClickNumber(dataId,url,startTime,endTime,type,fromWhere);
- //总点击人数
- int totalClickPeopleNumber = conferencesService.getTotalClickPeopleNumber(dataId,url,startTime,endTime,type,fromWhere);
- count++;
- XSSFRow row3 = ws1.createRow(count);
- rolNUm=0;
- row3.createCell(rolNUm).setCellValue(type == 1?"首页广告":type ==2?"上方广告":type==3?"session相关性广告":type==4?"展商活动":type==5?"动态(现场秀)":type==6?"消息站":type == 7?"弹窗":type == 8?"推送":"首页模块");
- rolNUm++;
- row3.createCell(rolNUm).setCellValue(dataId);
- rolNUm++;
- row3.createCell(rolNUm).setCellValue(title);
- rolNUm++;
- row3.createCell(rolNUm).setCellValue(url1);
- rolNUm++;
- row3.createCell(rolNUm).setCellValue(totalClickNumber);
- rolNUm++;
- row3.createCell(rolNUm).setCellValue(totalClickPeopleNumber);
- for(int j=0;j<bank1Long;j++){
- int tongJiZiDuan = Integer.valueOf(bank1[j]);
- if (tongJiZiDuan == 1) {
- //领域统计 userLy
- //先查询所有领域列表
- //遍历领域列表(compassfield) 根据 领域id 查询 用户表(field_id 领域ID field 领域 ) 得到 领域统计信息
- List<CompassField> compassList = conferencesService.getCompassList();
- int compassListLength = compassList.size();
- ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, compassListLength+rolNUm));
- row1.createCell(rolNUm+1).setCellValue("领域");
- if(compassList != null && compassList.size()!=0){
- for (CompassField compassField : compassList) {
- int compassFieldId = compassField.getCompassFieldId();
- int countLy = conferencesService.getUserLyInfo(dataId,url,compassFieldId,startTime,endTime,type,fromWhere);
- rolNUm++;
- row2.createCell(rolNUm).setCellValue(compassField.getFieldName());
- row3.createCell(rolNUm).setCellValue(countLy);
- }
- }
- }
- if(tongJiZiDuan == 2){//科室统计 userKs
- List<CompassKs> compassKsListList = conferencesService.getCompassKsList();
- int userKsListLength = compassKsListList.size();
- ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, userKsListLength+rolNUm));
- row1.createCell(rolNUm+1).setCellValue("科室");
- if(compassKsListList != null && compassKsListList.size()!=0){
- for (CompassKs compassKs : compassKsListList) {
- int compassKsId = compassKs.getCompassKsId();
- int countKs = conferencesService.getUserKsInfo(dataId,url,compassKsId,startTime,endTime,type,fromWhere);
- rolNUm++;
- row2.createCell(rolNUm).setCellValue(compassKs.getKsName());
- row3.createCell(rolNUm).setCellValue(countKs);
- }
- }
- }
- if(tongJiZiDuan == 3){//职称统计 zhicheng
- int userZhichengListLeng = zhichengArray.length;
- ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, userZhichengListLeng+rolNUm));
- row1.createCell(rolNUm+1).setCellValue("职称");
- for (int k=0;k<userZhichengListLeng;k++) {
- String zhicheng = zhichengArray[k];
- int userZhichengList = conferencesService.getUserZhichengInfo(dataId,url,startTime,endTime,zhicheng,type,fromWhere);
- rolNUm++;
- row2.createCell(rolNUm).setCellValue(zhicheng);
- row3.createCell(rolNUm).setCellValue(userZhichengList);
- }
- }
- if(tongJiZiDuan == 4){//身份统计
- int userShenfenListLength = shenfenList.length;
- ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, userShenfenListLength + rolNUm));
- row1.createCell(rolNUm+1).setCellValue("身份");
- for (int k = 0;k<userShenfenListLength;k++) {
- String shenfen = shenfenList[k];
- int countShenFen = conferencesService.getClickUserShenFenInfo( dataId,url,startTime,endTime,shenfen,type,fromWhere);
- rolNUm++;
- row2.createCell(rolNUm).setCellValue(shenfen);
- row3.createCell(rolNUm).setCellValue(countShenFen);
- }
- }
- if(tongJiZiDuan == 5){//省市(市)统计
- List<Hospital> provinceList = conferencesService.getProvinceList();
- if(provinceList != null && provinceList.size()!=0){
- for (Hospital hospital : provinceList) {
- Integer provinceId = hospital.getHospitalId();
- String provinceName = hospital.getProvince();
- if(provinceName!=null && provinceName.length() != 0){
- List<Hospital> cityList = conferencesService.getCityInfoByProvinceId(provinceId);
- if(cityList != null && cityList.size() != 0){
- int cityListLength = cityList.size();
- ws1.addMergedRegion(new CellRangeAddress(0, 0, rolNUm+1, rolNUm+cityListLength));
- row1.createCell(rolNUm+1).setCellValue(provinceName);
- for (Hospital hospital2 : cityList) {
- //得到市的名称
- String cityName = hospital2.getCity();
- Integer cityId = hospital2.getHospitalId();
- if(cityName!=null && cityName.length() != 0){
- int cityNum = conferencesService.getCityInfoByProvinceNameAndCityName(dataId,url,startTime, endTime, cityId,type,fromWhere);
- rolNUm++;
- row2.createCell(rolNUm).setCellValue(cityName);
- row3.createCell(rolNUm).setCellValue(cityNum);
- }
- }
- }
- }//省不为空
- }
- }
- }//省市统计结束
- }
- }
- }//最外if结束
- String filePath = request.getSession().getServletContext().getRealPath("files/execl");
- File conFile = new File(filePath); //目录结构
- if(!conFile.exists())
- {
- conFile.mkdir();
- }
- String fileName = "compassTongJi.xlsx";
- File file = new File(filePath+"/"+fileName);
- if(file.exists())
- {
- file.delete();
- }
- else{
- file.createNewFile();
- }
- FileOutputStream fout = new FileOutputStream(file);
- rwb1.write(fout);
- fout.close();
- //JSONObject jsonObject = new JSONObject();
- //jsonObject.accumulate("url",request.getContextPath()+"/files/execl/"+fileName);
- //writeToJson(response, jsonObject.toString());
- String urlString ="<script type='text/javascript'>window.parent.open('"+urlPath+"/files/execl/"+fileName+"')</script>";
- PrintWriter out = response.getWriter();
- response.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=UTF-8");
- out.write(urlString);
- out.flush();
- out.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
梦想是一个说出来就矫情的东西,它是生长在暗地里的一颗种子,只有破土而出,拔节而长,终有一日开出花来,才能正大光明的让所有人都知道。在此之前,除了坚持,别无选择。
java 实现excel 导出功能的更多相关文章
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...
- 用SpringMvc实现Excel导出功能
以前只知道用poi导出Excel,最近用了SpringMvc的Excel导出功能,结合jxl和poi实现,的确比只用Poi好,两种实现方式如下: 一.结合jxl实现: 1.引入jxl的所需jar包: ...
- excel导出功能优化
先说说优化前,怎么做EXCEL导出功能的: 1. 先定义一个VO类,类中的字段按照EXCEL的顺序定义,并且该类只能用于EXCEL导出使用,不能随便修改. 2. 将查询到的结果集循环写入到这个VO类中 ...
- 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能
我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...
- excel导出功能原型
本篇博客是记录自己实现的excel导出功能原型,下面我将简单介绍本原型: 这是我自制的窗体,有一个ListView和一个Button(导出)控件. 这是我在网上找到了使用exel需要引用的库. usi ...
- java利用EasyPoi实现Excel导出功能
easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言( ...
- JAVA实现Excel导出数据(以写好的Excel模版导出)
工作中经常会有将后台数据以Excel导出的功能. 简单的方法有将response的contentType设置为application/vnd.ms-excel: 或在JSP页面直接设置成: <% ...
- 使用NPOI实现简单的Excel导出功能
[1]NPOI是啥? NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作. POI是一个开源的Java读写Excel. ...
- java-excel导出
java excel导出分为两种2003年的格式和2007年的格式. 2003年的xls一个sheet限制65536. 2007年的xlsx限制为1048576. jxl导入2003 gradle j ...
随机推荐
- Cocos Creator 使用protobufjs
Win7 + Creator 2.0.0 + protobufjs 6.8.8 1.下载安装protobufjs npm install -g protobufjs 可以看到protobufjs安装在 ...
- Unity---判断某个点是否在摄像机的视景范围内
using UnityEngine; [RequireComponent(typeof(Camera))] public class VisualDetectionCamera : MonoBehav ...
- POJ-3294 Life Forms n个字符串中出现超过n/2次的最长子串(按字典序依次输出)
按照以前两个字符串找两者的最长公共子串的思路类似,可以把所有串拼接到一起,这里为了避免讨论LCP跨越多个串需需要特别处理的问题用不同的字符把所有串隔开(因为char只有128位,和可能不够用,更推荐设 ...
- localStorage(本地存储器)、sessionStorage(会话存储)
设置:localStorage.setItem("token", JSON.parse(res).data.token); 获取:that.token = localSto ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)C. Morse Code
题意:给你n个01字符,每次问你前缀的所有本质不同的子串,由摩斯密码组成的方案数和. 题解:离线处理,把字符建sam,通过topo序来dp计算每个节点表示的子串方案数的和.统计答案时,把n个字符挨个匹 ...
- python自动化测试入门篇-jemter
接口测试基础-jemter 接口文档地址:http://doc.nnzhp.cn 使用jmeter实现简单的http request的接口测试 一.get获取学生信息接口 第一步:新建一个Thread ...
- 946. Validate Stack Sequences验证栈序列
网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate ...
- Oracle 11g streams部署
环境 源服务器 目标服务器 系统版本 CentOS Linux release 7.3.1611 (Core) CentOS Linux release 7.3.1611 (Core) 主机名 s ...
- koa和express对比
不同: 1.启动方式不同 koa采用了new Koa()的方式,而express采用传统的函数形式 2.中间件形式二者不一样,这是由二者处理中间件的逻辑差异导致的,实际上这也是二者最根本的差别 3.k ...
- 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 ...