package cn.com.countrygarden.bi.main.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExcelCopyUtil {

	private ExcelCopyUtil() {}

//	/**
//	 * 把一个excel中的cellstyletable复制到另一个excel，这里会报错，不能用这种方法，不明白呀？？？？？
//	 * @param fromBook
//	 * @param toBook
//	 */
//	public static void copyBookCellStyle(HSSFWorkbook fromBook,HSSFWorkbook toBook){
//		for(short i=0;i<fromBook.getNumCellStyles();i++){
//			HSSFCellStyle fromStyle=fromBook.getCellStyleAt(i);
//			HSSFCellStyle toStyle=toBook.getCellStyleAt(i);
//			if(toStyle==null){
//				toStyle=toBook.createCellStyle();
//			}
//			copyCellStyle(fromStyle,toStyle);
//		}
//	}
		/**
		 * 复制一个单元格样式到目的单元格样式
		 * @param fromStyle
		 * @param toStyle
		 */
		public static void copyCellStyle(XSSFCellStyle fromStyle,
										 XSSFCellStyle toStyle) {
			toStyle.setAlignment(fromStyle.getAlignment());
			//边框和边框颜色
			toStyle.setBorderBottom(fromStyle.getBorderBottom());
			toStyle.setBorderLeft(fromStyle.getBorderLeft());
			toStyle.setBorderRight(fromStyle.getBorderRight());
			toStyle.setBorderTop(fromStyle.getBorderTop());
			toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
			toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
			toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
			toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());

			//背景和前景
			toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
			toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());

			toStyle.setDataFormat(fromStyle.getDataFormat());
			toStyle.setFillPattern(fromStyle.getFillPattern());
//		toStyle.setFont(fromStyle.getFont(null));
			toStyle.setHidden(fromStyle.getHidden());
			toStyle.setIndention(fromStyle.getIndention());//首行缩进
			toStyle.setLocked(fromStyle.getLocked());
			toStyle.setRotation(fromStyle.getRotation());//旋转
			toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
			toStyle.setWrapText(fromStyle.getWrapText());

		}
		/**
		 * Sheet复制
		 * @param fromSheet
		 * @param toSheet
		 * @param copyValueFlag
		 */
		public static void copySheet(XSSFWorkbook wb,XSSFSheet fromSheet, XSSFSheet toSheet,
									 boolean copyValueFlag) {
			//合并区域处理1111
			mergerRegion(fromSheet, toSheet);
			for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext();) {
				XSSFRow tmpRow = (XSSFRow) rowIt.next();
				XSSFRow newRow = toSheet.createRow(tmpRow.getRowNum());
				//行复制
				copyRow(wb,tmpRow,newRow,copyValueFlag);
			}
		}
		/**
		 * 行复制功能
		 * @param fromRow
		 * @param toRow
		 */
		public static void copyRow(XSSFWorkbook wb,XSSFRow fromRow,XSSFRow toRow,boolean copyValueFlag){
			toRow.setHeight(fromRow.getHeight());
			for (Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext();) {
				XSSFCell tmpCell = (XSSFCell) cellIt.next();
				XSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex());
				copyCell(wb,tmpCell, newCell, copyValueFlag);
			}
		}
		/**
		 * 复制原有sheet的合并单元格到新创建的sheet
		 *
		 * @param sheetCreat 新创建sheet
		 * @param sheet      原有的sheet
		 */
		public static void mergerRegion(XSSFSheet fromSheet, XSSFSheet toSheet) {
			int sheetMergerCount = fromSheet.getNumMergedRegions();
			for (int i = 0; i < sheetMergerCount; i++) {
				CellRangeAddress mergedRegionAt = fromSheet.getMergedRegion(i);
				toSheet.addMergedRegion(mergedRegionAt);
			}
//			复制行宽
			try{
				short lastCellNum = fromSheet.getRow(1).getLastCellNum();
				for (int i = 0; i < lastCellNum; i++) {
					toSheet.setColumnWidth(i,fromSheet.getColumnWidth(i));
				}
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		/**
		 * 复制单元格
		 *
		 * @param srcCell
		 * @param distCell
		 * @param copyValueFlag
		 *            true则连同cell的内容一起复制
		 */
		public static void copyCell(XSSFWorkbook wb,XSSFCell srcCell, XSSFCell distCell,
									boolean copyValueFlag) {
			XSSFCellStyle newstyle=wb.createCellStyle();
			copyCellStyle(srcCell.getCellStyle(), newstyle);
//			distCell.setEncoding(srcCell.getEncoding());
			//样式
			distCell.setCellStyle(newstyle);
			//评论
			if (srcCell.getCellComment() != null) {
				distCell.setCellComment(srcCell.getCellComment());
			}
			// 不同数据类型处理
			int srcCellType = srcCell.getCellType();
			distCell.setCellType(srcCellType);
			if (copyValueFlag) {
				if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) {
					if (HSSFDateUtil.isCellDateFormatted(srcCell)) {
						distCell.setCellValue(srcCell.getDateCellValue());
					} else {
						distCell.setCellValue(srcCell.getNumericCellValue());
					}
				} else if (srcCellType == HSSFCell.CELL_TYPE_STRING) {
					distCell.setCellValue(srcCell.getRichStringCellValue());
				} else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) {
					// nothing21
				} else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) {
					distCell.setCellValue(srcCell.getBooleanCellValue());
				} else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) {
					distCell.setCellErrorValue(srcCell.getErrorCellValue());
				} else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) {
					distCell.setCellFormula(srcCell.getCellFormula());
				} else { // nothing29
				}
			}
		}

}