poi 의 기초 사용 방법


POI 추천 사용자 매 뉴 얼: http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook
 HSSFCellStyle (POI API Documentation)
 
poi 사용법
 
 /**
     *         excel  
     * @param head      (        )
     * @param colName          , content     
     * @param content            
     * @return String           
     */
    public String inputContentToExcel(String head,String[] colName,List<List> content) throws IOException {
        String path = getExportDir();
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        path += "/" + head + ".xls";
        File currentFile = new File(path);
        if(!currentFile.exists()){
            file.mkdir();
        }
        int colCount = colName.length;
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(head);
        //         
        HSSFRow row1 = sheet.createRow(0);
        HSSFCell cell1 = row1.createCell((short)0);
        cell1.setCellValue(head);
        //           
        HSSFRow row2 = sheet.createRow(1);
        for(short col=0;col<colCount;col++){
            HSSFCell cell = row2.createCell(col);
            String val = colName[col];
            cell.setCellValue(Validator.isNULL(val) ? "" : val);
        }
        //         
        int rowInd = 2;
        for (List list : content){
            HSSFRow row = sheet.createRow(rowInd);
            for(short j=0;j<colCount;j++){
                HSSFCell cell = row.createCell(j);
                String val = list.get(j).toString();
                cell.setCellValue(val);
            }
            rowInd++;
        }

        //     
        sheet.addMergedRegion(new Region(0,(short)0,0,(short)(colCount-1)));
        //     
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);    //  
        font.setFontHeightInPoints((short)18);            //      
        font.setFontName("  ");                         //    
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);     //    
        style.setFont(font);
        cell1.setCellStyle(style);
        //          
        for(short k=0;k<colCount;k++){
            sheet.autoSizeColumn(k);
        }

        //      
        FileOutputStream stream = new FileOutputStream(path);
        wb.write(stream);
        stream.close();
        return head + ".xls";
    }

 
 
경로 가 져 오 는 방법
 
public String getExportDir(){
        String dir = "";
        dir = configService.getFileRealPath() + "export/";
        //        ,      
        Calendar calendar = Calendar.getInstance();
        dir += calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH)+1) + "/" + calendar.get(Calendar.DATE) ;
        System.out.println("        :"+dir);
        return dir;
    }

 
효과 도 첨부 파일

좋은 웹페이지 즐겨찾기