728x90
JXLS = JXLS์ ํ ํ๋ฆฟ์ ๊ธฐ๋ฐ์ผ๋ก ์ต์ข ์์ ํ์ผ์ ์์ฑ
๋ฐ์ดํฐ (java ๊ฐ์ฒด) + ์์ ํ ํ๋ฆฟ = ์์ ๊ฒฐ๊ณผ ์ถ๋ ฅ
์์ ์ ๊ทธ๋๋ก ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์์ ์ ์์, ์ฐจํธ, ์์๋ฑ์ ๋ฃ์ ์ ์๋ค.
pom.xml
<!-- Create Excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-contrib</artifactId>
<version>3.7-beta3</version>
</dependency>
<!-- apache poi -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>1.0.5</version>
</dependency>
ExcelView.java
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.view.document.AbstractXlsxStreamingView;
import net.sf.jxls.transformer.XLSTransformer;
public class ExcelView extends AbstractXlsxStreamingView {
private static final String sample = "/excel/sample.xlsx";// ํด๋์คํจ์ค์ ์๋ Resource ๊ฒฝ๋ก
@Override
protected void buildExcelDocument(final Map<String, Object> model, final Workbook workbook, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
OutputStream os = null;
InputStream is = null;
try {
String fileName = "excelTest";
is = new ClassPathResource(sample).getInputStream();
response.setHeader("Content-Type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
os = response.getOutputStream();
XLSTransformer transformer = new XLSTransformer();
Workbook excel = transformer.transformXLS(is, model);
excel.write(os);
os.flush();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
}
}
ExcelController.java (๋ฐ์ดํฐ๋ฅผ ์์ฑํ์ฌ ExcelView๋ฅผ ๋ฆฌํด)
@Controller
public class ExcelController {
@RequestMapping(value = "/api/excel")
public View reportExcelDownload2(final HttpServletResponse response,
@RequestParam(value = "reqData", required = false, defaultValue = "") final String reqData, final Model model) throws Exception {
try {
//๋ฐ์ดํฐ ๋ง๋ค๊ธฐ
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> obj = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
obj = new HashMap<String, Object>();
obj.put("name", "๊ฐํธ๋ฃจ");
obj.put("age", "20");
obj.put("address", "๋์ ");
list.add(obj);
obj = new HashMap<String, Object>();
obj.put("name", "๋ํธ๋ฃจ");
obj.put("age", "27");
obj.put("address", "์์ธ");
list.add(obj);
obj = new HashMap<String, Object>();
obj.put("name", "๋คํธ๋ฃจ");
obj.put("age", "50");
obj.put("address", "๋ถ์ฐ");
list.add(obj);
obj = new HashMap<String, Object>();
obj.put("name", "๋ผํธ๋ฃจ");
obj.put("age", "100");
obj.put("address", "์ ์ฃผ");
list.add(obj);
model.addAttribute("list", list);
model.addAttribute("count", list.size());
model.addAttribute("DownloadDate", sdf.format(new Date()));
} catch (Exception e) {
e.printStackTrace();
}
return new ExcelView();
}
}
sample.xlsx ํ์ผ
์ ์ ์นํํ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ธฐ ์ํด์๋ ${} ํ์ ์ฌ์ฉ. (JSTL๊ณผ ๋งค์ฐ ํก์ฌ)
<jx:forEach>ํ๊ทธ๋ด์ ๋ด์ฉ์ ์์ดํ ๊ฐ์ ๋งํผ ๋ฐ๋ณตํ๋ ๋ฐฉ์.
<jx:forEach items="${list}" var="info">
${info.age}
</jx:forEach>
class path Resource ๊ฒฝ๋ก๋๋ก ์์์ ์์ฑํ .xlsx ํ์ผ์ ๋ฃ์ด์ค๋ค.
๊ฒฐ๊ณผ ํ๋ฉด
์ฐธ๊ณ :
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JXLS] JXLS EXCEL Templates์ ๊ทธ๋ฃน์ ์ฌ๋ฌ ๊ฐ ์ฌ์ฉ ์์ ๋ค์ด๋ก๋์ ๋ฐ์ํ๋ ์ด์ (0) | 2021.08.04 |
---|---|
[Spring] @RequestBody / @ResponseBody ์ฐจ์ด (0) | 2021.05.28 |
[Mybatis] ๋ฐฉ๊ธ insert ๊ฐ select ํ๊ธฐ <selectKey> (6) | 2021.05.04 |
[File] ํ๋ก์ ํธ ์ธ๋ถ์ ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ (3) | 2021.04.22 |
[java] mybatis foreach ๋ค์ค insert (0) | 2021.04.06 |