전체소스 : spring304.zip

 

스프링은 엑셀 다운로드를 생성할 수 있도록 다음의 두 View 클래스제공

  ● AbstractExcelView : POI API를 이용하여 엑셀 응답을 생성

    ▶ 별도의 LIB 필요 (poi-3.8-20120326.jar) : http://poi.apache.org/

  ● AbstractJExcelView : JExcel API를 이용하여 엑셀 응답을 생성

 

 

FileDownloadController.java

package sp.mvc.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

 

@Controller
public class FileDownloadController {


 @RequestMapping(value="/filedown.sp")
 public ModelAndView fileDownload(){
  System.out.println("----- FileDownloadController.fileDownload() -----");
  
  ModelAndView modelAndView = new ModelAndView("filedownload");
   
  return modelAndView;
 }
 
 @RequestMapping("/exceldown.sp")
 public String excelDownload(Model model){
  System.out.println("----- FileDownloadController.excelDownload() -----");
  
  List<String> list = new ArrayList<String>();
  list.add("사이트 관리");
  list.add("관리자 관리");
  list.add("공통 코드 관리");
  list.add("접속 이력");
  
  model.addAttribute("menuList", list);
  
  return "excelDownload";
 }
}

 

ExcelDownloadView.java

package sp.mvc.view;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

 

public class ExcelDownloadView extends AbstractExcelView {

 

 @SuppressWarnings("unchecked")
 @Override

 protected void buildExcelDocument(Map<String, Object> model,
   HSSFWorkbook workbook, HttpServletRequest req, HttpServletResponse res)
   throws Exception {
  // TODO Auto-generated method stub
  System.out.println("---- ExcelDownloadView.buildExcelDocument() ----");
  
  String userAgent = req.getHeader("User-Agent");
  String fileName = "test.xls";
  
  if(userAgent.indexOf("MSIE") > -1){
   fileName = URLEncoder.encode(fileName, "utf-8");
  }else{
   fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
  }
  
  res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
  res.setHeader("Content-Transfer-Encoding", "binary");
    
  HSSFSheet sheet = createFirstSheet(workbook);
  createColumnLabel(sheet);
  
  List<String> menuList = (List<String>)model.get("menuList");

  for(int i=0; i <= menuList.size()-1; i++){
   createPageRow(sheet, menuList, i);
  }
 }
  
 private HSSFSheet createFirstSheet(HSSFWorkbook workbook){
  HSSFSheet sheet = workbook.createSheet();
  workbook.setSheetName(0, "테스트");
  sheet.setColumnWidth(1, 256*30);
  return sheet;
 }
 
 private void createColumnLabel(HSSFSheet sheet){
  HSSFRow firstRow = sheet.createRow(0);
  
  HSSFCell cell = firstRow.createCell(0);
  cell.setCellValue("순위");
  
  cell = firstRow.createCell(1);
  cell.setCellValue("페이지");
 }

 

 private void createPageRow(HSSFSheet sheet, List<String> menuList, int rowNum){
  HSSFRow row = sheet.createRow(rowNum + 1);
  
  HSSFCell cell = row.createCell(0);
  cell.setCellValue(rowNum + 1);
  
  cell = row.createCell(1);
  cell.setCellValue(menuList.get(rowNum)); 
 }
}

 

main_config.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

 <context:annotation-config/>
 <context:component-scan base-package="sp.mvc.controller"/>
 
 <!-- bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:alwaysUseFullPath="true"/ -->
 <!-- bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" p:alwaysUseFullPath="true"/ --> 
 
  
 <!-- ViewResolver 설정 -->
 <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1"/>
 
 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:order="2" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"></bean>


 
 <!-- 각종 bean 설정 -->
 <bean id="excelDownload" class="sp.mvc.view.ExcelDownloadView"></bean>
 
</beans>

 

 

결과

요청 URL

   http://localhost:8090/spring304/exceldown.sp

 

----- FileDownloadController.excelDownload() -----
---- ExcelDownloadView.buildExcelDocument() ----

 

 

 

 

 

[참고자료] Spring 3.0 프로그래밍-최범균

+ Recent posts