ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 엑셀 생성 + 저장위치 설정 다운로드(알집+개별저장)
    Web (Spring ) 2016. 11. 23. 19:17

    개요


    개별 파일 알집 속에 저장




    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
    145
    146
    147
    148
          /**
         * 엑셀 다운로드
         * @param adminBuildEnergyVo
         * @return
         * @throws Throwable 
         */
        @RequestMapping(value="/admin/authenticate/buildEnergy/excelCreate.do")
        @ResponseBody
        public void adminBuildEenrgyExcel(HttpServletRequest request, HttpServletResponse    response) throws Throwable{
            try{
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                
                AdminBuildEnergyVo adminBuildEnergyVo = new AdminBuildEnergyVo();
                
                String rdmStr = CommonUtils.getRandomString();
                Calendar todayCal = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyMd");
                int today = Integer.parseInt(sdf.format(todayCal.getTime()));
                String homedir = ""+today+"/"//엑셀 생성
     
                // Workbook 생성
    //            Workbook xlsWb = new HSSFWorkbook(); // Excel 2007 이전 버전
                
                String[] adminbuildenergynos = request.getParameterValues("adminbuildenergyno");
                System.out.println("length : "+adminbuildenergynos.length);
                
                /*엑셀 전체저장할 시에*/
                for(int i=0; i<adminbuildenergynos.length; i++){
                    adminBuildEnergyVo.setAdminbuildenergyno(Integer.parseInt(adminbuildenergynos[i]));
                    System.out.println("length:"+adminbuildenergynos.length); //파라미터 벨류 length
                    System.out.println("values"+adminBuildEnergyVo.getAdminbuildenergyno());
                    adminBuildEnergyVo = adminBuildEnergyService.getAdminBuildEnergy(adminBuildEnergyVo); //서비스 다시 주입
                    
                    Workbook xlsxWb = new XSSFWorkbook(); // Excel 2007 이상
                     
                    // *** Sheet-------------------------------------------------
                    // Sheet 생성
                    Sheet sheet = xlsxWb.createSheet(""); //시트이름
             
                    // 컬럼 너비 설정
                    sheet.setColumnWidth(03000);
                    sheet.setColumnWidth(13000);
                           // ----------------------------------------------------------
                     
                    // *** 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("용도");
                    cell.setCellStyle(cellStyle); // 셀 스타일 적용
                     
                   
                    //---------------------------------
                     
                    // 두 번째 줄
                    row = sheet.createRow(1);
                     
                    // 두 번째 줄에 Cell 설정하기-------------
                    cell = row.createCell(0);
                    cell.setCellValue(adminBuildEnergyVo.getCertificationno());
                     
                    cell = row.createCell(1);
                    cell.setCellValue(adminBuildEnergyVo.getUsage());
                     
                    
                    //---------------------------------
                    
                    // excel 파일 저장
                    try {
                        File file = new File(homedir+rdmStr+"/");
                        if(file.exists() == false){
                            file.mkdirs();
                        }
                        
                        File xlsFile = new File(homedir+rdmStr+"/"+.xls");
                        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+"/", zipName);
                out.println(zipName);
                
                }catch(Exception e){
                    System.err.println(e.toString());
                }
                
    //        return "y";
        }
        
        
        
        @RequestMapping(value="/admin/authenticate/buildEnergy/excelDownLoad.do")
    //    @ResponseBody
        public void adminBuildEenrgyExcel2(HttpServletRequest request, HttpServletResponse    response){
            try{
    //            String root = "D:/eGovFrameDev-3.5.1-32bit/workspace/greencity/src/main/webapp/";
                String root = "";
                
    //            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




    create 후에 download ajax로 바로다운로드



    ajax

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


    input button


    1
    <input type="button" class="table-btn-3" onclick="fn_execlView('${adminBuildEnergyList[0].adminbuildenergyno}');" value="엑셀저장" style="width: 100px; height: 30px"><br/><br/><br/>
    cs


    for문 안에 hidden


    1
    2
    3
    <c:forEach items="${adminBuildEnergyList}" var="data" varStatus="status">
        <input type="hidden" class="adminbuildenergyno" id="adminbuildenergyno" value="${data.adminbuildenergyno }">
    </c:forEach>
    cs


Designed by Tistory.