1. 정의

엑셀 파일 템플릿을 이용해 엑셀 파일을 쉽게 다운로드 하는 패키지이다.

또한, XML 설정 파일을 통해 엑셀 파일 데이터를 자바 객체로 읽는 장치도 제공한다.

jXLS 라이브러리는 POI 패키지를 기반으로 동작하기 때문에 jXLS 라이브러리 사용시 POI 라이브러리도 함께 사용해야 한다.

XTLS 객체를 사용하는 방식을 선호하는 이유는, 자바의 MVC 패턴을 그대로 활용 가능하기 때문이다.

기존 코드에 view 단을 담당하는 jsp 대신 엑셀 파일을 view로 활용하여 처리하면 된다.

템플릿을 기반으로 하여 엑셀 파일을 그대로 사용 가능하기 때문에 엑셀 기능의 대부분도 사용 가능하다.



2. 예제

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
    <dependency>
        <groupId>net.sf.jxls</groupId>
        <artifactId>jxls-core</artifactId>
        <version>1.0.6</version>
    </dependency>
    <dependency>
        <groupId>org.jxls</groupId>
        <artifactId>jxls-poi</artifactId>
        <version>1.0.9</version>
    </dependency>
    <dependency>
        <groupId>org.jxls</groupId>
        <artifactId>jxls-jexcel</artifactId>
        <version>1.0.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.14</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.14</version>
    </dependency>
cs

pom.xml 파일에 다섯 개의 라이브러리를 추가해준다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    @RequestMapping(value = "/listExcel.do")
    public void listExcel(HttpServletRequest request,
            HttpServletResponse response, UserSearchVO searchVO,
            ModelMap modelMap) {

        // Service의 해당 메소드로 실현될 쿼리로는 list select문을 그대로 사용하면 된다.
        List<?> listUser = userService.selectUserList(searchVO);
        Map<String, Object> beans = new HashMap<String, Object>();
        
        beans.put("listUser", listUser);
        
        // 엑셀 다운로드 메소드가 담겨 있는 객체
        MakeExcel me = new MakeExcel();
        
        // 인자로 request, response, Map Collection 객체, 파일명, 폴더명, 견본파일을 받는다.
        me.download(request, response, beans, "파일명""폴더명""견본 파일.확장자");
    }
cs


엑셀 다운로드 컨트롤러이다. jsp에 listExcel.do로 하이퍼링크를 걸어준 뒤 클릭하면 해당 컨트롤러로 이동하게 된다.

엑셀 다운로드가 담겨 있는 MakeExcel 이라는 객체는 이미 존재하는 라이브러리가 아닌 다운로드를 위해 임의로 만든 객체이다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MakeExcel {
    public void download(HttpServletRequest request, HttpServletResponse response,
                    Map<String, Object> bean, String fileName, String templateFile)
                    throws ParsePropertyException, InvalidFormatException {
        
        String tempPath = request.getSession().getServletContext().getRealPath("/WEB-INF/templete");
        
        try {
            InputStream is = new BufferedInputStream(new FileInputStream(tempPath + "\\" + templateFile));
            XLSTransformer xls = new XLSTransformer();
            Workbook workbook = xls.transformXLS(is, bean);
            
            response.setHeader("Content-Disposition""attachment; filename=\"" + fileName + ".xlsx\"");
            
            OutputStream os = response.getOutputStream();
            
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
cs


임의로 생성한 MakeExcel 클래스의 소스이다. InputStream과 OutputStream 객체를 사용해 엑셀 다운로드를 가능하도록 해준다.

tempPath 변수로 값을 복사한 getRealPath() 메소드에 엑셀 템플릿이 들어갈 경로를 적어주면 된다.

엑셀 템플릿에는 실제로 jstl 형식으로 파라미터를 전달하듯 Map에 담은 객체를 ${객체명.파라미터} 를 사용해

jsp list를 출력하듯 엑셀 파일 내에 그대로 적어주면 된다.

+ Recent posts