package kcg.imis.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;

/**
  * @FileName : ExcelView.java
  * @Project : 국제해양프로젝트
  * @Date : 2018. 3. 29. 
  * @작성자 : Moon
  * @변경이력 : 
  * @프로그램 설명 :
 */
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");
		}
		
	}
}
