# 의존성 주입
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!-- 아파치 POI 엑셀 업로드 -->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<!-- 파일입출력을 위한 commonsio -->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
<!-- 아파치 POI 엑셀 업로드 끝 -->
|
cs |
1. workbook 을 생성한다.
2. workbook 내에 sheet를 생성한다.
3. sheet 내에 row를 생성한다.
4. 하나의 row에 여러개의 cell을 생성한다. (= 하나의 행에 여러 열을 생성한다)
5. 3과 4의 과정을 계속해서 반복한다.
# 컨트롤러 파일
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
package kr.or.ddit.poi.controller;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/poi")
@Controller
public class PoiController {
private static final Logger logger =
LoggerFactory.getLogger(PoiController.class);
@GetMapping("/page")
public String poipage() {
return "poi/page";
}
@RequestMapping(value = "/page", method = RequestMethod.POST)
public void poiTest(Model model, HttpServletResponse response,
HttpServletRequest request) throws Exception{
//jsp에서 넘긴 값을 받아서 날짜 입력받음
String day1 = request.getParameter("day1"); // 2021-05-01
String day2 = request.getParameter("day2"); // 2021-05-30
/*
실제로는 DB 데이터를 추출하는 경우가 많기 때문에 날짜데이터를 예시로 든다면.
웹에서 사용자가 day1 과 day2 를 넘겨주고. 컨트롤러에서 service로 데이터를 넘기고.
dao에서 mapper를 통해 day1 과 day2 날짜 사이의 데이터들을 출력할 것이다.
그 데이터들은 list 에 담아서 활용한다고 가정해보자 !
*/
List<String> list = new ArrayList<String>();
// 활용 팁 : service dao 를 통해 입력받은 2021-05-01 ~ 2021-05-30 일자로
// 데이터 조회를 후 list에 담았다고 가정하자.
list.add("강감찬"); // 샘플데이터1
list.add("이순신"); // 샘플데이터2
list.add("성춘향"); // 샘플데이터3
list.add("이몽룡"); // 샘플데이터4
list.add("신사임당"); // 샘플데이터5
list.add("홍길동"); // 샘플데이터6
//필수로 만들어야할 것 Workbook과 Sheet
Workbook wb = new HSSFWorkbook(); // 엑셀파일 객체 생성
Sheet sheet = wb.createSheet("테스트 시트"); //시트 생성 ( 첫번째 시트이며, 시트명은 테스트 시트 )
CellStyle style = wb.createCellStyle(); // 셀 스타일 생성
Font font = wb.createFont(); // 폰트 스타일 생성
// font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 글자 진하게
font.setFontHeight((short)(16*18)); // 글자 크기
font.setFontName("맑은 고딕"); // 글씨체
// 자바의 배열처럼 첫번째 인덱스가 0 부터 시작한다. 첫번째는 0 , 두번째는 1 , 세번째는 2
Row titleRow = sheet.createRow(0); // 타이틀행을 생성한다. 첫번째줄이기때문에 createRow(0)
int titleColNum = 0; // 첫번째 열이기 때문에 0
Cell titleCell = titleRow.createCell(titleColNum); // 첫번째행의 첫번째열을 지정한다.
titleCell.setCellValue("안녕하세요 \n 테스트입니다."); // setCellValue 셀에 값넣기.
titleRow.setHeight((short)920); // Row에서 setHeight를 하면 행 높이가 조정된다.
sheet.addMergedRegion(new CellRangeAddress(0,0,0,9)); // 셀 병합 첫번째줄~아홉번째 열까지 병합
// new CellRangeAddress(시작 row, 끝 row, 시작 col, 끝 col)
style.setWrapText(true); //문자열을 입력할때 \n 같은 개행을 인식해준다.
// style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 수직 가운데 정렬
// style.setAlignment(CellStyle.ALIGN_CENTER); // 수평 가운데 정렬
style.setFont(font); // 스타일에 font 스타일 적용하기
titleCell.setCellStyle(style); // 정리한 스타일들을 titleCell에 적용해주자 !
//입력받은 날짜 출력하기
Row dayRow = sheet.createRow(1);
int dayCol = 0;
Cell dayCell = dayRow.createCell(dayCol); // 두번째줄의 첫번째열을 셀로 지정. 즉 두번째줄 첫째칸
dayCell.setCellValue("조회날짜 : " + day1 + " ~ " + day2); // 두번째 행은 입력받은 날짜를 출력
//헤더 만들기
Row headerRow = sheet.createRow(3); // 네번째줄 생성
int headerCol = 0;
Cell headerCell = headerRow.createCell(headerCol++);
headerCell.setCellValue("순번");
headerCell = headerRow.createCell(headerCol);
headerCell.setCellValue("이름");
CellStyle dataStyle = wb.createCellStyle(); // 데이터스타일은 테두리를 만들어보자
// dataStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); //오른쪽 테두리
// dataStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); //왼쪽 테두리
// dataStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 상단 테두리
// dataStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 하단 테두리
//데이터 삽입하기
int rowNum = 4; // 네번째줄이 헤더니까 그 밑에서부터 데이터 삽입
int cellNum = 0;
Row dataRow = null; // for문을 돌려주기위해.
Cell dataCell = null;
for(int i = 0; i<list.size(); i++) {
cellNum = 0;
dataRow = sheet.createRow(rowNum++); // for문 돌면서 행 1줄씩 추가
dataCell = dataRow.createCell(cellNum++); //열 한줄씩 추가
dataCell.setCellValue(i+1); // 첫번째칸은 순번이기때문에
dataCell.setCellStyle(dataStyle); // 테두리 스타일 적용
dataCell = dataRow.createCell(cellNum); // 두번째 열은 이름이니까
dataCell.setCellValue(list.get(i)); // list에 저장된 이름 출력
dataCell.setCellStyle(dataStyle); // 테두리 스타일 적용
}
dataRow = sheet.createRow(rowNum++); // 총 인원을 작성해보자
dataCell = dataRow.createCell(0); // 첫번쨰칸
dataCell.setCellValue("총 인원");
dataCell = dataRow.createCell(1); // 두번쨰칸
dataCell.setCellFormula("COUNT(A5:A10)"); // 함수식을 입력할 수 있는 기능
//-------------------------------------사진-----------------------------------------
try {
/* 사진 삽입 위치 : filePath*/
String filePath = "template/img/test.jpg";
InputStream is = new FileInputStream(getClass().getClassLoader().getResource(filePath).getFile());
byte[] bytes = IOUtils.toByteArray(is);
int picIdx = wb.addPicture(bytes, XSSFWorkbook.PICTURE_TYPE_PNG);
is.close();
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
// 이미지 출력할 엑셀 cell,row 위치
anchor.setRow1(12); //13번째줄
anchor.setCol1(4); //3번째 셀
//이미지 그리기
Picture pic = drawing.createPicture(anchor, picIdx);
pic.resize();
} catch (Exception e) {
e.printStackTrace();
}
/* 엑셀 파일 생성 */
response.setContentType("ms-vnd/excel");
//* 엑셀 파일 이름 지정 *
response.setHeader("Content-Disposition", "attachment;filename=poiTest.xls");
wb.write(response.getOutputStream());
}
}
|
cs |
Apache poi | 설명 |
Workbook | 엑셀 객체 |
Sheet | 엑셀 시트 |
CellStyle | 엑셀의 셀 스타일 |
Font | 엑셀의 폰트 스타일 |
Row / Col / Cell | 행 / 열 / 셀 |
setCellValue | 셀의 값 |
# 출력되는 http/ jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>엑셀 파일 다운로드</title>
</head>
<body>
<h1>Apache poi</h1>
<h2>엑셀 다운로드</h2>
<br>
<form id="ExcelForm" name="ExcelForm" action="/poi/page" method="post">
<!-- 엑셀파일로 다운로드 받을 날짜를 입력받았다고 가정해보자 ! -->
<input type="date" id="day1" name="day1" value="">
<input type="date" id="day2" name="day2" value="">
<button type="submit" class="btn btn-default">엑셀 다운</button>
</form>
</body>
|
cs |
# 결과
다른 입력을 받아서 파라미터로 다양하게 문서를 입맛대로 만들수 있다는게
신기하다. 프로젝트에도 넣어볼까 생각중이기에 좀더 다듬어서 원하는대로 만들어봐야겠다.
참조사이트:
https://dailylifecoding.tistory.com/entry/apache-POI-%EA%B0%84%EB%8B%A8-%EC%82%AC%EC%9A%A9%EB%B2%95-1
https://myjamong.tistory.com/111
https://bcdragonfly.tistory.com/6#
'Coding story > go Spring,JSP' 카테고리의 다른 글
(JSP) 테이블의 값을 엑셀 파일로 다운로드(SheetJs) (1) | 2022.03.11 |
---|---|
(Spring) 엑셀 파일값을 DB에 업로드하기(Apache Poi) (0) | 2022.02.25 |
구글 차트 ($.ajax를 활용한 차트 출력) (0) | 2022.02.17 |
다음 주소창을 이용한 주소 입력/datepicker를 통한 날짜입력 (0) | 2022.02.17 |
(스프링) 세션을 통한 로그인 (0) | 2022.02.15 |
최근댓글