ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 엑셀 생성 + 저장위치 설정 다운로드(리스트)
    Web (Spring ) 2016. 11. 23. 19:09

    개요


    리스트 엑셀 출력


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    /**
         * 엑셀 다운로드
         * @param adminBuildEnergyVo
         * @return
         * @throws Throwable 
         */
        @RequestMapping(value="/admin/stats/excelCreate.do")
        @ResponseBody
        public void adminBuildEenrgyExcel(@ModelAttribute("loginStatsVo") LoginStatsVo loginStatsVo, HttpServletRequest request, HttpServletResponse    response) throws Throwable{
            try{
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                
                String rdmStr = CommonUtils.getRandomString();
                Calendar todayCal = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyMd");
                int today = Integer.parseInt(sdf.format(todayCal.getTime()));
                String homedir = "D:/eGovFrameDev-3.5.1-32bit/workspace/greencity/src/main/webapp/excel/"+today+"/"//엑셀 생성
                
                // Workbook 생성
                Workbook xlsWb = new HSSFWorkbook(); // Excel 2007 이전 버전
                
                    Workbook xlsxWb = new XSSFWorkbook(); // Excel 2007 이상
                     
                    // *** Sheet-------------------------------------------------
                    // Sheet 생성
                    Sheet sheet = xlsxWb.createSheet("로그인 통계"); //시트이름
             
                    // 컬럼 너비 설정
                    sheet.setColumnWidth(03000);
                    sheet.setColumnWidth(13000);
                    sheet.setColumnWidth(23000);
                    sheet.setColumnWidth(33000);
                    sheet.setColumnWidth(43000);
                    // ----------------------------------------------------------
                     
                    // *** Style--------------------------------------------------
                    // Cell 스타일 생성
                    CellStyle cellStyle = xlsxWb.createCellStyle();
                     
                    // 줄 바꿈
                    cellStyle.setWrapText(true);
                     
                    // Cell 색깔, 무늬 채우기
                    cellStyle.setFillForegroundColor(HSSFColor.LIME.index);
                    cellStyle.setFillPattern(CellStyle.ALIGN_CENTER);
                     
                    Row row = null;
                    Cell cell = null;
                    //----------------------------------------------------------
                    
                    // 첫 번째 줄
                    row = sheet.createRow(0);
                     
                    // 첫 번째 줄에 Cell 설정하기-------------
                    cell = row.createCell(0);
                    cell.setCellValue("번호");
                    cell.setCellStyle(cellStyle); // 셀 스타일 적용
                     
                    cell = row.createCell(1);
                    cell.setCellValue("ID");
                    cell.setCellStyle(cellStyle); // 셀 스타일 적용
                     
                    cell = row.createCell(2);
                    cell.setCellValue("IP");
                    cell.setCellStyle(cellStyle); // 셀 스타일 적용
                    
                    cell = row.createCell(3);
                    cell.setCellValue("접속시간");
                    cell.setCellStyle(cellStyle); // 셀 스타일 적용
                    
                    cell = row.createCell(4);
                    cell.setCellValue("접속종료시간");
                    cell.setCellStyle(cellStyle); // 셀 스타일 적용
                    //---------------------------------
                     
                    // 두 번째 줄
                    row = sheet.createRow(1);                                
                    
                    List<LoginStatsVo> list = statsService.selectExcelDown(loginStatsVo); //로그인 통계 목록 조회
                    
                    System.out.println("size:"+list.size());
                    
                    //객체 json 변환
    //                JSONArray jsonArray = new JSONArray();
    //                jsonArray.add(loginStatsVo);
    //                out.println(jsonArray);
                    
                    for(int i=0; i < list.size(); i++){
                        
                        System.out.println("list"+list.get(i).toString());
                        
                        row = sheet.createRow(i+1);
                     
                        // 두 번째 줄에 Cell 설정하기-------------
                        cell = row.createCell(0);
                        cell.setCellValue(list.get(i ).getLoginstatsno());
                         
                        cell = row.createCell(1);
                        cell.setCellValue(list.get(i ).getUserid());
                         
                        cell = row.createCell(2);
                        cell.setCellValue(list.get(i ).getUserip());
                        
                        cell = row.createCell(3);
                        cell.setCellValue(list.get(i ).getConnecttime());
                        
                        cell = row.createCell(4);
                        cell.setCellValue(list.get(i ).getLogouttime());
                    
                    }
                    //---------------------------------
                    
                    // excel 파일 저장
                    
                    try {
                        
                        File file = new File(homedir+rdmStr+"/");
                        if(file.exists() == false){
                            file.mkdirs();
                        }
                        
                        File xlsFile = new File(homedir+rdmStr+"_로그인 통계.xls");
                        System.out.println("2222222222"+xlsFile.toString());
                        FileOutputStream fileOut = new FileOutputStream(xlsFile);
                        xlsxWb.write(fileOut);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                                     
                    
    //            out.println(jsonArray);
    //            String zipName = homedir+rdmStr+".zip";
    //            Zip.compress(homedir+rdmStr+"/");
                out.println(homedir+rdmStr+"_로그인 통계.xls");
                
                }catch(Exception e){
                    System.err.println(e.toString());
                }
                
    //        return "y";
        }
    cs


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    @RequestMapping(value="/admin/stats/excelDownLoad.do")
        public void adminBuildEenrgyExcel2(HttpServletRequest request, HttpServletResponse    response){
            try{
    //            String root = "D:/eGovFrameDev-3.5.1-32bit/workspace/greencity/src/main/webapp/";
                String root = "D:/eGovFrameDev-3.5.1-32bit/workspace/greencity/src/main/webapp/";
                
    //            String rootPath = root + "/";
    //            byte fileByte[] = FileUtils.readFileToByteArray(new File("D:/eGovFrameDev-3.5.1-32bit/workspace/test2/src/main/webapp"+saveFileName));
                System.out.println("path : "+request.getParameter("path"));
                byte fileByte[] = FileUtils.readFileToByteArray(new File(request.getParameter("path")));
        
                response.setContentType("application/octet-stream");
                response.setContentLength(fileByte.length);
     
                response.setHeader("Content-Disposition""attachment; fileName=\"" + URLEncoder.encode(request.getParameter("path").substring(request.getParameter("path").lastIndexOf("/")),"UTF-8").replace("+""%20")+"\";");
                response.setHeader("Content-Transfer-Encoding""binary");
                response.getOutputStream().write(fileByte);
                
                response.getOutputStream().flush();
                response.getOutputStream().close();
            }catch(Exception e){
                System.out.println(e.toString());
            }
    cs


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    function fn_execlView() {
             
    //             $(".loginstatsno").attr("name", "loginstatsno"); //name값 삽입
     
                console.log($("form[name=frm]").serialize());
                
                 $.ajax({
                        url : "/admin/stats/excelCreate.do",
                        type : "POST",
                        data : $("form[name=frm]").serialize(),
                        dataType : "text",
        
                        success : function(response) {
    //                         $(".loginstatsno").attr("name", ""); //name 삭제
                            location.href = "/admin/stats/excelDownLoad.do?path="+response;
                        },
        
                        error : function(request, status, error) {
                            alert("서버 통신에 실패하였습니다.");
                        }
        
                    });
                
            }
    cs


    'Web (Spring )' 카테고리의 다른 글

    jQuery ui theme  (0) 2016.11.29
    엑셀 생성 + 저장위치 설정 다운로드(알집+개별저장)  (0) 2016.11.23
    jQuery 동적 tr 생성 및 삭제  (0) 2016.11.18
    ajax 파일 전송  (0) 2016.11.17
    jQuery DatePicker 기간조회  (0) 2016.11.16
Designed by Tistory.