package com.zhiwei.util;

import java.io.File;//引入类
import java.net.URL;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zw.entity.NewsResult;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableHyperlink;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * excel操作类
 * 
 * @author admin
 *
 */
@Component
public class ExcelUtil {
	private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
	
	/**
	 * 删除文件
	 * @param url
	 *            文件的路径
	 */
	public static void delete(String url) {
		File file = new File(url);
		if (file.isFile()) {
			file.delete();
		}
		log.info("删除完成");
	}

	/**
	 * 日匹配所有新闻数据写入excel
	 * @param list
	 * @param time
	 * @param fileName
	 * @throws Exception
	 */
	public void pushDataWithFinance(List<NewsResult> list, Date time, String fileName) throws Exception {
		File tempFile = new File(fileName);
		// 若是文件不存在就创建新文件
		if (!tempFile.exists()) {
			tempFile.createNewFile();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
		WritableSheet sheet = workbook.createSheet("table1", 0);
		// 一些临时变量，用于写到excel中
		Label l = null;
		// 预定义的一些字体和格式，格式： 字形 大小 加粗 倾斜 下划线 颜色
		WritableFont font1 = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false,
				UnderlineStyle.NO_UNDERLINE, Colour.WHITE);
		WritableFont font2 = new WritableFont(WritableFont.ARIAL, 18, WritableFont.BOLD, false,
				UnderlineStyle.NO_UNDERLINE, Colour.WHITE);
		WritableCellFormat MainTitle = new WritableCellFormat(font2);
		MainTitle.setAlignment(Alignment.CENTRE);
		MainTitle.setBackground(Colour.BLUE_GREY);
		MainTitle.setVerticalAlignment(VerticalAlignment.CENTRE);
		// 财新数据标题格式
		WritableCellFormat mainTitle = new WritableCellFormat(font1);
		mainTitle.setAlignment(Alignment.CENTRE);
		mainTitle.setBackground(Colour.BLUE_GREY);
		// 标题格式
		WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 14, WritableFont.NO_BOLD, false,
				UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
		WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
		titleFormat.setAlignment(Alignment.CENTRE);
		// 正文字体
		WritableFont detFont = new WritableFont(WritableFont.ARIAL, 13, WritableFont.NO_BOLD, false,
				UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
		WritableCellFormat detFormat = new WritableCellFormat(detFont);
		detFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
		WritableFont urlf = new WritableFont(WritableFont.ARIAL, 11, WritableFont.NO_BOLD, false,
				UnderlineStyle.NO_UNDERLINE, Colour.BLUE);
		WritableCellFormat urlFormat = new WritableCellFormat(urlf);
		urlFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
		int seq = 0;
		seq = 0;
		sheet.mergeCells(0, seq, 7, seq + 2);
		l = new Label(0, seq, "金融负面新闻数据", MainTitle);
		sheet.addCell(l);
		// 上方3个单位合并
		seq += 3;
		l = new Label(0, seq, "时间");
		sheet.addCell(l);
		l = new Label(1, seq, sdf.format(time));
		sheet.addCell(l);
		sheet.mergeCells(2, seq, 4, seq);
		l = new Label(2, seq, "");
		sheet.addCell(l);
		sheet.mergeCells(5, seq, 7, seq);
		l = new Label(5, seq, "推送");
		sheet.addCell(l);
		seq += 2;
		sheet.mergeCells(0, seq, 7, seq + 1);
		l = new Label(0, seq, "概览", mainTitle);
		sheet.addCell(l);
		seq += 3;

		seq += 3;
		sheet.mergeCells(0, seq, 7, seq + 1);
		l = new Label(0, seq, "", mainTitle);
		sheet.addCell(l);
		seq += 2;
		int row = seq++;
		l = new Label(0, row, "序号", titleFormat);
		sheet.addCell(l);
		l = new Label(1, row, "标题", titleFormat);
		sheet.addCell(l);
		l = new Label(2, row, "时间", titleFormat);
		sheet.addCell(l);
		l = new Label(3, row, "来源", titleFormat);
		sheet.addCell(l);
		int num = 1;

		for (NewsResult news : list) {
			row = seq++;
			l = new Label(0, row, String.valueOf(num++), detFormat);// 序号
			sheet.addCell(l);
			try {
				WritableHyperlink link = new WritableHyperlink(1, row, 1, row, new URL(news.getUrl()), news.getTitle());
				sheet.addHyperlink(link);
			} catch (Exception e) {
				log.error("获取url失败，错误信息{}", e);
			}
			l = new Label(1, row, news.getTitle(), urlFormat);// 标题
			sheet.addCell(l);
			l = new Label(2, row, news.getTime(), detFormat);// 时间
			sheet.addCell(l);
			l = new Label(3, row, news.getSource(), detFormat);// 时间
			sheet.addCell(l);
		}
		int column = 0;
		sheet.setColumnView(column++, 10);
		sheet.setColumnView(column++, 25);
		sheet.setColumnView(column++, 20);
		sheet.setColumnView(column++, 22);
		sheet.setColumnView(column++, 22);
		workbook.write();
		workbook.close();
		log.info("内容写入成功");
	}
}
