package com.qiankun.excel.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ReadExcel {
	public static String S_PATH="C:\\eclipse-jee-juno-SR2-win32-x86_64\\workspace\\chengji\\src\\main\\webapp\\excel";
	public static String S_HTML_PATH="C:\\eclipse-jee-juno-SR2-win32-x86_64\\workspace\\chengji\\src\\main\\webapp\\html";
	public static void main(String[] args) {
		try {				
			getallExcel();
		} catch (Exception ex) {
		}
		System.out.println("ok");
	}

	public static List<String> readfile(String filepath) throws Exception {
		File file = new File(filepath);
		List<String> excelList = new ArrayList<String>();
		try {
			if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					excelList.add(filelist[i]);
				}
			}
		} catch (Exception e) {

		}
		return excelList;
	}
	public static Map<String,List<ReadExcelVO>> getallExcel()throws Exception{
		Map<String,List<ReadExcelVO>> t_map=new HashMap<String, List<ReadExcelVO>>();
		try {
			List<String> fileList = readfile(S_PATH);
			if(fileList.size()>0){
				for(String filename:fileList){
					List<ReadExcelVO> excelList=new ArrayList<ReadExcelVO>();
					excelList=readExcels(filename);
					t_map.put(filename, excelList);
				}
			}
			
			for(String t_key:t_map.keySet()){
				System.out.println("*********文件名************="+t_key+"\r\n");
				List<ReadExcelVO> excelList=t_map.get(t_key);
				if(excelList.size()>0){
				   for(ReadExcelVO vo:excelList){
					   System.out.println("编号="+vo.getStudenNo()+"\r\n"); 
					   System.out.println("姓名="+vo.getName()+"\r\n");   
					   System.out.println("成绩="+vo.getName()+"\r\n");   
				   }	
				}
				System.out.println("#########文件名############"+t_key+"结束\r\n");
			}
			
		} catch (Exception e) {
			
		}
		
		return t_map;
	}
	
	public static List<ReadExcelVO> readExcels(String filepath)
			throws Exception {
		List<ReadExcelVO> excelList = new ArrayList<ReadExcelVO>();
		List<ExcelListVO> listVO = new ArrayList<ExcelListVO>();
		Workbook rwb = null;
		Cell cell = null;
		String path = S_HTML_PATH+"/"+filepath.split("\\.")[0]+"/";
		File f = new File(path);
        if(!f.isDirectory()){
            f.mkdir();
        }   
        
		try {
			// 创建输入流
			InputStream stream = new FileInputStream(S_PATH + "\\" + filepath);
			// 获取Excel文件对象
			rwb = Workbook.getWorkbook(stream);
			// 获取文件的指定工作表 默认的第一个
			Sheet[] sheets = rwb.getSheets();
			for(Sheet sheet : sheets){
				
				String name = sheet.getName();
				
				File file = new File(path+name+"/");
				if(!file.isDirectory()){
					file.mkdir();
				}	
				
				Integer num = 1;
				// 行数(表头的目录不需要，从1开始)
				for (int i = 1; i < sheet.getRows(); i++) {
				    
				    Integer n = Integer.valueOf(sheet.getCell(3, i).getContents());
                    if (n!=num){
                        ExcelListVO listvo = new ExcelListVO();
                        listvo.setExcelList(excelList);
                        listvo.setNum(num);
                        num = n;
                        listVO.add(listvo);
                        excelList = new ArrayList<ReadExcelVO>();
                    }
                    
					// 列数
					ReadExcelVO vo = new ReadExcelVO();
					// 获取第i行，第j列的值
					vo.setStudenNo(sheet.getCell(0, i).getContents());
					vo.setName(sheet.getCell(1, i).getContents());
					vo.setScore(sheet.getCell(2, i).getContents());
					
					excelList.add(vo);
					
				}
				
				ExcelListVO listvo = new ExcelListVO();
                listvo.setExcelList(excelList);
                listvo.setNum(num);
                listVO.add(listvo);
                excelList = new ArrayList<ReadExcelVO>();
				
				for(ExcelListVO vo : listVO){
				    String filePath = path+name+"/" + vo.getNum()+".html";
				    
				    try {
	                    PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
	                    
	                    StringBuffer sb = new StringBuffer();
	                    sb.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>2015陈省身数学周·国际青少年数学邀请活动</title><style type=\"text/css\">");
	                    sb.append("body {margin-top: 0px;margin-bottom: 0px;}.bt {font-size: 14px;color:#1675b9;text-align:center;font-family: \"宋体\";font-weight: bold;line-height:30px}.biao {    font-size: 12px;text-align:center;font-family: \"宋体\";}");
	                    sb.append(".nr {font: \"宋体\"; font-size: 12px; line-height:30px}.bot{ text-align:center; color:#FFFFFF; line-height:22px; font-size:12px}.xbt {font-size: 14px;color:#1675b9;;text-align:center;font-family: \"宋体\";line-height:30px}</style></head><body>");
	                    sb.append("<table width=\"1004\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td><img src=\"../images/index_top.jpg\" width=\"1004\" height=\"437\"></td></tr></table><table width=\"1004\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td><table width=\"910\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td><img src=\"../images/index_cjcx.jpg\" width=\"910\" height=\"49\"></td></tr><tr><td height=\"2\"></td></tr></table><table width=\"910\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"1\" colspan=\"3\" bgcolor=\"#1675b9\"></td></tr><tr><td width=\"1\" bgcolor=\"#1675b9\"></td><td><table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"30\" colspan=\"2\"></td></tr><tr><td colspan=\"2\" class=\"bt\">");
	                    
	                    sb.append("2015陈省身数学周·国际青少年数学邀请活动成绩查询（三年级组）</td></tr><tr><td height=\"20\" colspan=\"2\" class=\"xbt\">");
	                    sb.append("高法考区&nbsp;&nbsp;三年级组&nbsp;&nbsp;第一考场</td></tr><tr><td width=\"50%\"><table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#1675b9\"><tr height=\"20\"><td height=\"20\" bgcolor=\"#FFFFFF\" class=\"biao\"><strong>考号</strong></td><td width=\"50%\" height=\"20\" bgcolor=\"#FFFFFF\" class=\"biao\"><strong>学校</strong></td><td height=\"20\" bgcolor=\"#FFFFFF\" class=\"biao\"><strong>成绩</strong></td></tr>");
	                    
	                    
	                    
	                    
				    for (ReadExcelVO volist : vo.getExcelList()){
				        sb.append("<tr height=\"20\">");
	                    sb.append("<td height=\"20\" bgcolor=\"#FFFFFF\" class=\"biao\">"+volist.getStudenNo()+"</td>");
	                    sb.append("<td height=\"20\" bgcolor=\"#FFFFFF\" class=\"biao\">"+volist.getName()+"</td>");
	                    sb.append("<td height=\"20\" bgcolor=\"#FFFFFF\" class=\"biao\">"+volist.getScore()+"</td>");
	                    sb.append("</tr>");
				    }
				    
				    sb.append("</table></td></tr><tr><td height=\"20\" colspan=\"2\" >&nbsp;</td></tr> <tr><td colspan=\"2\" class=\"biao\"><a href=\"../cjcx_cxs_3.jsp\">返回</a></td></tr><tr><td height=\"20\" colspan=\"2\">&nbsp;</td></tr></table></td><td width=\"1\" bgcolor=\"#1675b9\"></td></tr><tr><td height=\"1\" colspan=\"3\" bgcolor=\"#1675b9\"></td></tr></table></td></tr></table><table width=\"1004\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>&nbsp;</td></tr><tr><td height=\"83\" bgcolor=\"#1675b9\" class=\"bot\">咨询电话：27057788 &nbsp;&nbsp; 网址：www.huayn.com www.huayn.net <br>(copyright2002) TIANJIN HUAYING SCIENCE TECHNOLOGY AND EDUCATION DEVELOPMENT CO.,LTD. <br>津ICP备05004622号 　津教备0516号　建议使用IE6.0以上版本及1024*768分辨率</td></tr></table></body></html>");
                    
                    
                    pw.println(sb.toString());
                    pw.close();             
                    
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
				}
				
			
					
					
				
				
			}
			

		} catch (Exception e) {

		}
		return excelList;
	}	

}
