package com.qmth.qrzk.repository.utils.excel;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
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.util.StringUtils;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.qmth.qrzk.repository.user.model.Paper;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;

public class ExcelUtil extends AbstractExcelView {

	@Override
	protected void buildExcelDocument(Map<String, Object> obj,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HSSFSheet sheet = workbook.createSheet("list");

		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("试卷编码");

		cell = row.createCell(1);
		cell.setCellValue("状态");

		cell = row.createCell(2);
		cell.setCellValue("专卡");

		cell = row.createCell(3);
		cell.setCellValue("课程代码");
		cell = row.createCell(4);
		cell.setCellValue("课程名称");
		cell = row.createCell(5);
		cell.setCellValue("教材名称");
		cell = row.createCell(6);
		cell.setCellValue("编者");
		cell = row.createCell(7);
		cell.setCellValue("出版社");
		cell = row.createCell(8);
		cell.setCellValue("版次");
		cell = row.createCell(9);
		cell.setCellValue("学校");
		
		int i=0;
		for(Map.Entry<String, Object> entry:obj.entrySet()){
			Object object=entry.getValue();
			if(object instanceof Paper){
				Paper p=(Paper) object;
				String status="";
				String ZK="";
				row=sheet.createRow(i+1);
				cell=row.createCell(0);
				cell.setCellValue(p.getCode());
				
				cell=row.createCell(1);
				if(p.getStatus().equals("DRAFT")){
					status="毛坯";
				}else if(p.getStatus().equals("FORMAL")){
					status="清样";
				}else if(p.getStatus().equals("USED")){
					status="启用";
				}
				cell.setCellValue(status);
				if(p.getIsSpecialAnswerSheet()==1){
					ZK="有";
				}else{
					ZK="不详";
				}
	
				cell=row.createCell(2);
				cell.setCellValue(ZK);
				
				cell=row.createCell(3);
				cell.setCellValue(p.getCourseCode());
				
				cell=row.createCell(4);
				cell.setCellValue(p.getCourseName());
				
				cell=row.createCell(5);
				cell.setCellValue(p.getBookName());
				
				cell=row.createCell(6);
				cell.setCellValue(p.getBookAuthor());
				
				cell=row.createCell(7);
				cell.setCellValue(p.getBookPress());
				
				cell=row.createCell(8);
				cell.setCellValue(p.getBookPressYear());
				
				cell=row.createCell(9);
				cell.setCellValue(p.getSchool());
				i++;
			}
		}

		sheet.setColumnWidth(0, 15 * 256);
		sheet.setColumnWidth(1, 7 * 256);
		sheet.setColumnWidth(2, 7 * 256);
		sheet.setColumnWidth(3, 10 * 256);
		sheet.setColumnWidth(4, 30 * 256);
		sheet.setColumnWidth(5, 30 * 256);
		sheet.setColumnWidth(6, 17 * 256);
		sheet.setColumnWidth(7, 25 * 256);
		sheet.setColumnWidth(8, 7 * 256);
		sheet.setColumnWidth(9, 25 * 256);
		
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		String filename = sdf.format(new Date())+".xls";// 设置下载时客户端Excel的名称
		filename = encodeFilename(filename, request);// 处理中文文件名
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename="+ filename);
		OutputStream ouputStream = response.getOutputStream();
		workbook.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();
	}

	/**
	 * 设置下载文件中文件的名称
	 * 
	 * @param filename
	 * @param request
	 * @return
	 */
	private static String encodeFilename(String filename,
			HttpServletRequest request) {
		/**
		 * 获取客户端浏览器和操作系统信息 在IE浏览器中得到的是：User-Agent=Mozilla/4.0 (compatible; MSIE
		 * 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
		 * 在Firefox中得到的是：User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1;
		 * zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
		 */
		String agent = request.getHeader("USER-AGENT");
		try {
			if ((agent != null) && (-1 != agent.indexOf("MSIE"))) {
				String newFileName = URLEncoder.encode(filename, "UTF-8");
				newFileName = StringUtils.replace(newFileName, "+", "%20");
				if (newFileName.length() > 150) {
					newFileName = new String(filename.getBytes("GB2312"),
							"ISO8859-1");
					newFileName = StringUtils.replace(newFileName, " ", "%20");
				}
				return newFileName;
			}
			if ((agent != null) && (-1 != agent.indexOf("Mozilla")))
				return MimeUtility.encodeText(filename, "UTF-8", "B");

			return filename;
		} catch (Exception ex) {
			return filename;
		}
	}
}
