java 实现excel 导出功能

时间:2022-03-17 22:16:29

实现功能: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();
}
}

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