package org.ccpit.business.registerManage;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


/**
 * @author sunqipeng
 * @Description 描述：excel助手类
 * @Version v0.1
 * @2015-6-5上午8:38:45 JDK版本 sun jdk1.6
 * ********************************更新记录******************************
 * 版本：  <版本号>        修改日期：  <日期>        修改人： <修改人姓名>
 * 修改内容：  <修改内容描述>
 * *********************************************************************
 */
public class ExcelHelper {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

    public static void exportExcelRegisterInfo(OutputStream os, List<RegisterInfo> newsInfoList, String sheetName) {
        //创建Excel工作薄
        XSSFWorkbook book = new XSSFWorkbook();
        //在Excel工作薄中建一张工作表
        XSSFSheet sheet = book.createSheet(sheetName);
        //创建cell背景颜色
        XSSFCellStyle style = book.createCellStyle();
        XSSFFont font = book.createFont();
        font.setColor(HSSFColor.BLACK.index);//字体颜色
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);         //字体增粗
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); //垂直
        style.setFillBackgroundColor(HSSFColor.ROYAL_BLUE.index);
        //设置单元格格式(文本)
        XSSFCellStyle cellStyle = book.createCellStyle();
        //创建第一行为标题行
        XSSFRow row = sheet.createRow(0);
        row.setRowStyle(style);
        XSSFCell cell0 = row.createCell(0);
        XSSFCell cell1 = row.createCell(1);
        XSSFCell cell2 = row.createCell(2);
        XSSFCell cell3 = row.createCell(3);
        XSSFCell cell4 = row.createCell(4);
        XSSFCell cell5 = row.createCell(5);
        XSSFCell cell6 = row.createCell(6);
        XSSFCell cell7 = row.createCell(7);
        XSSFCell cell8 = row.createCell(8);
        XSSFCell cell9 = row.createCell(9);
        XSSFCell cell10 = row.createCell(10);
        XSSFCell cell11 = row.createCell(11);
        XSSFCell cell12 = row.createCell(12);
        XSSFCell cell13 = row.createCell(13);
        XSSFCell cell14 = row.createCell(14);
        XSSFCell cell15 = row.createCell(15);
        XSSFCell cell16 = row.createCell(16);
        XSSFCell cell17 = row.createCell(17);
        XSSFCell cell18 = row.createCell(18);
        XSSFCell cell19 = row.createCell(19);
        XSSFCell cell20 = row.createCell(20);
        XSSFCell cell21 = row.createCell(21);
        XSSFCell cell22 = row.createCell(22);
        XSSFCell cell23 = row.createCell(23);
        XSSFCell cell24 = row.createCell(24);
        XSSFCell cell25 = row.createCell(25);
        XSSFCell cell26 = row.createCell(26);
        XSSFCell cell27 = row.createCell(27);
        XSSFCell cell28 = row.createCell(28);
        XSSFCell cell29 = row.createCell(29);
        //定义单元格为字符串类型
        cell0.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell1.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell2.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell3.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell4.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell5.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell6.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell7.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell8.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell9.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell10.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell11.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell12.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell13.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell14.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell15.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell16.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell17.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell18.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell19.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell20.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell21.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell22.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell23.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell24.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell25.setCellType(XSSFCell.CELL_TYPE_STRING);

        //在单元格中输入数据
        cell0.setCellValue("提交时间");
        cell1.setCellValue("城市选择");
        cell2.setCellValue("姓名");
        cell3.setCellValue("性别");
        cell4.setCellValue("出生日期");
        cell5.setCellValue("电话");
        cell6.setCellValue("手机");
        cell7.setCellValue("邮箱");
        cell8.setCellValue("工作单位");
        cell9.setCellValue("发票抬头");
        cell10.setCellValue("发票类型");
        cell11.setCellValue("地址");
        cell12.setCellValue("TITLE");
        cell13.setCellValue("DATE OF BIRTH");
        cell14.setCellValue("GENDER");
        cell15.setCellValue("NAME");
        cell16.setCellValue("COMPANY NAME");
        cell17.setCellValue("ADDRESS");
        cell18.setCellValue("COUNTRY");
        cell19.setCellValue("PHONE");
        cell20.setCellValue("EMAIL");
        cell21.setCellValue("CITY SELECT");
        cell22.setCellValue("考试科目");
        cell23.setCellValue("报名类型");
        cell24.setCellValue("报名状态");
        cell25.setCellValue("是否购买CDCS/CSDG考前培训");
        cell0.setCellStyle(style);
        //循环导出数据到excel中
        for (int i = 0; i < newsInfoList.size(); i++) {
            RegisterInfo registerInfo = newsInfoList.get(i);
            //创建第i行
            XSSFRow rowi = sheet.createRow(i + 1);
            rowi.createCell(0).setCellValue(sdf.format(registerInfo.getSubmitTime()));  //提交时间
            rowi.createCell(1).setCellValue(registerInfo.getCity());                    //城市
            rowi.createCell(2).setCellValue(registerInfo.getName());                    //姓名
            rowi.createCell(3).setCellValue("male".equals(registerInfo.getGender()) ? "男" : "女");    //性别
            rowi.createCell(4).setCellValue(sdf.format(registerInfo.getBirthday()));    //出生日期
            rowi.createCell(5).setCellValue(registerInfo.getTelphone());                //电话
            rowi.createCell(6).setCellValue(registerInfo.getMobileTel());                    //手机
            rowi.createCell(7).setCellValue(registerInfo.getEmail());                //邮箱
            rowi.createCell(8).setCellValue(registerInfo.getCompany());                //工作单位
            rowi.createCell(9).setCellValue(registerInfo.getInvoiceTitle());                //发票抬头
            rowi.createCell(10).setCellValue(registerInfo.getInvoiceType());                //发票类型
            rowi.createCell(11).setCellValue(registerInfo.getAddress());                //地址
            rowi.createCell(12).setCellValue(registerInfo.getTitle());    //TITLE
            rowi.createCell(13).setCellValue(sdf.format(registerInfo.getBirthday()));    //DATE OF BIRTH
            rowi.createCell(14).setCellValue("male".equals(registerInfo.getGender()) ? "MALE" : "FEMALE");    //GENDER
            rowi.createCell(15).setCellValue(registerInfo.getName_en());    //NAME
            rowi.createCell(16).setCellValue(registerInfo.getCompany_en());    //COMPANY NAME
            rowi.createCell(17).setCellValue(registerInfo.getAddress_en());    //ADDRESS
            rowi.createCell(18).setCellValue(registerInfo.getCountry());    //COUNTRY
            rowi.createCell(19).setCellValue(registerInfo.getSort());    //PHONE
            rowi.createCell(20).setCellValue(registerInfo.getEmail());    //EMAIL
            rowi.createCell(21).setCellValue(registerInfo.getCity_en());    //CITY SELECT
            rowi.createCell(22).setCellValue(registerInfo.getSubject());    //考试科目
            rowi.createCell(24).setCellValue(registerInfo.getRegisterStatus());        //报名状态
            if(null != registerInfo.getIfBuyFee()){
                rowi.createCell(25).setCellValue(1 == registerInfo.getIfBuyFee() ? "是":"否");        //是否购买CDCS/CSDG靠前培训
            }
        }
        try {
            book.write(os);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
