본문 바로가기

IT/ETC

Apache poi(excel)

반응형
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
@RequestMapping(value = "excelDownload")
    public void excelDownload(HttpServletResponse response) throws Exception{
        //정보를 담는 객체 
        List<BoardVO> list = boardService.selectAll();
        
        //Starting basic, Workbook for creating excel
        //Sheet with workbook
        //row and cell
        //workbook, sheet, row, cell
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("게시판");
        Row row = null;
        Cell cell = null;
        int rowNo = 0;
        
        //optional, but recommended to style your columns
        //if there are a lot of columns, different styles are required,
        //better to create a method to maintain usability of ur code
        //for me, i just needed two different type so decided to hardcode em
        
        //font for font styling
        Font font = wb.createFont();
        //cellstyle for styling your cell
        CellStyle headStyle = wb.createCellStyle();
        headStyle.setBorderTop(BorderStyle.THIN);
        headStyle.setBorderBottom(BorderStyle.THIN);
        headStyle.setBorderLeft(BorderStyle.THIN);
        headStyle.setBorderRight(BorderStyle.THIN);
        //if you want to change your background, need foreground && fillpattern!!
        headStyle.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //apply the font here
        headStyle.setFont(font);
        
        //this is for body of a table
        CellStyle bodyStyle = wb.createCellStyle();
        bodyStyle.setBorderTop(BorderStyle.THIN);
        bodyStyle.setBorderBottom(BorderStyle.THIN);
        bodyStyle.setBorderLeft(BorderStyle.THIN);
        bodyStyle.setBorderRight(BorderStyle.THIN);
        //bodyStyle.setWrapText(true);
        
        //top column 
        String[] headerString = {"게시번호""제목","작성자","작성 날짜","시작일","종료일","조회수"};
        row = sheet.createRow(rowNo++);
        for(int i = 0; i < headerString.length; i++){
            cell = row.createCell(i);
            cell.setCellStyle(headStyle);
            cell.setCellValue(headerString[i]);
            
            
        }
        
        //tried this with an array, but cant seem to find a solution 
        //prints out strings.. 
        String[] bodyString = {".getBoardid()"".getBtitle()"".getBusername()"".getBdate()"".getBfrom()"".getBto()"".getBhit()"};
        for(BoardVO vo : list){
            row = sheet.createRow(rowNo++);
//            for(int i = 0; i < bodyString.length; i++){
//                cell = row.createCell(i);
//                cell.setCellStyle(bodyStyle);
//                String temp = "vo" + bodyString[i];
//                
//                cell.setCellValue(temp);
//            }
            cell = row.createCell(0);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(vo.getBoardid());
            cell = row.createCell(1);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(vo.getBtitle());
            cell = row.createCell(2);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(vo.getBusername());
            cell = row.createCell(3);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(vo.getBdate());
            cell = row.createCell(4);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(vo.getBfrom());
            cell = row.createCell(5);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(vo.getBto());
            cell = row.createCell(6);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(vo.getBhit());
        }
        
        //set the content type and header to respond
        response.setContentType("ms-vnd/excel");
        response.setHeader("Content-Disposition""attachment;filename=test.xls");
        
        //if you want to size your columns according to text, do it after
        //data has all been inputed or it will have no effect!
        for(int i = 0; i < headerString.length; i++){
            sheet.autoSizeColumn(i);
        }
        
        //write to a workbook and close
        wb.close();
    }
}
    
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 
반응형

'IT > ETC' 카테고리의 다른 글

FTP vs SFTP  (0) 2020.01.29
AWS(1/29/20 notes) 접속  (0) 2020.01.29
01/20/20 notes(mac java dw, java download and setup)  (0) 2020.01.20
Algorithms(week3, problemset)  (0) 2020.01.06
Arrays(week 2, problem set)  (0) 2019.12.30