/** 
 * ExcelView.java
 * @author 임새미 
 * @since 2016. 10. 14.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 14. 	    임새미				       최초생성
 * 
 */
package kcg.faics.cmmn.excel;

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

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

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import egovframework.rte.fdl.excel.util.AbstractPOIExcelView;

/** 
 * ExcelView.java
 * @author 임새미 
 * @since 2016. 10. 14.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 14. 	    임새미				       최초생성
 * 
 */
public class ExcelView extends AbstractPOIExcelView {

	/**
	 * 엑셀파일을 다운로드한다.
	 * 
	 * @param model Model객체
	 * @param model Model객체
	 * @param model Model객체
	 * @param model Model객체
	 */
	@Override
	protected void buildExcelDocument(Map<String, Object> model, XSSFWorkbook wb, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		if(model.get("filename") != null){
			String userAgent = req.getHeader("User-Agent");
	        String fileName = (String)model.get("filename");
			if(userAgent.indexOf("MSIE") > -1){
		         fileName = URLEncoder.encode(fileName, "utf-8");
		    }else{
	        	fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
	        }
			model.put("filename", fileName);
		}
		
		ExcelExport export = (ExcelExport) model.get("excel");
		try {
			export.makeExcel(wb);
		} catch (Exception e) {
			e.printStackTrace();
			XSSFSheet sheet = wb.createSheet("sheet1");  
			XSSFCell cell = getCell(sheet, 0, 0);  
	        setText(cell, "Error");
		}
		
	}
}
