package cn.report.excel.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
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.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import cn.report.excel.util.base.SlashLinePosition;

public class PoiDrawLineUtil
{
	public static final float PXTOPT = 0.75f;
	public static void main(String[] args) throws Exception {
       /**/
		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet("Sheet1");
		
		XSSFRow row = sheet.createRow(0);
		row.setHeightInPoints(60 * PXTOPT);//单元格高度
		XSSFCell cell = row.createCell(0);
		XSSFCellStyle cellStyle = getCellFormat(wb);
		//cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
		cell.setCellStyle(cellStyle);

		//设置文字
		final String text = "年龄 \n\n 工资";
		cell.setCellValue(text);
		//sheet.setColumnWidth(0, 40 *50);
		ArrayList<SlashLinePosition> slpList = 
				getSlashLinePositionXlsx("LEFT_TOP",2,sheet,0,0);
		
        drawLineXlsx(sheet, 0, 0, "LEFT_TOP", slpList);
	  	File file = new File("D:\\data\\Tes6t.xlsx");
		try (FileOutputStream fis = new FileOutputStream(file);) {
			wb.write(fis);
			wb.close();
		} catch (Exception e) {}
		
		
		//drawLine();
		
	
		
		
	}
	
	public static void drawLine()
	{
		/**绘线OK*/
		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet("Sheet1");
		CreationHelper helper = wb.getCreationHelper();
		XSSFDrawing drawing = sheet.createDrawingPatriarch();
		ClientAnchor anchor = helper.createClientAnchor();
		// 设置斜线的开始位置
		anchor.setCol1(0);
		anchor.setRow1(0);
		// 设置斜线的结束位置
		anchor.setCol2(1);
		anchor.setRow2(1);
		XSSFSimpleShape shape = drawing.createSimpleShape((XSSFClientAnchor) anchor);
		// 设置形状类型为线型
		shape.setShapeType(ShapeTypes.LINE);
		// 设置线宽
		shape.setLineWidth(0.5);
		// 设置线的风格
		shape.setLineStyle(0);
		// 设置线的颜色
		shape.setLineStyleColor(0, 0, 0);
		XSSFRow row = sheet.createRow(0);
		row.setHeightInPoints(40 * PXTOPT);
		XSSFCell cell = row.createCell(0);

		XSSFCellStyle cellStyle = getCellFormat(wb);
		//cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
		cell.setCellStyle(cellStyle);

		//设置文字
		final String text = "年龄 \n\n 工资";
		cell.setCellValue(text);

		File file = new File("D:\\data\\Test.xlsx");
		try (FileOutputStream fis = new FileOutputStream(file);) {
			wb.write(fis);
			wb.close();
		} catch (Exception e) {}
		
	}
	
	/**
	 * @Description 模拟测试使用的XSSFWorkbook对象
	 **/
	public static XSSFWorkbook getTestXSSFWorkbook()
	{
		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet("line");
		XSSFRow row0 = sheet.createRow(0);
		row0.setHeightInPoints(100);
		XSSFRow row1 = sheet.createRow(1);
		row1.setHeightInPoints(70);
		XSSFCellStyle cellStyle = getCellFormat(wb);
		sheet.setColumnWidth(0, 80 * 90);
		sheet.setColumnWidth(1, 60 * 90);
		XSSFCell cell0_0 = row0.createCell(0);
		XSSFCell cell0_1 = row0.createCell(1);
		XSSFCell cell1_0 = row1.createCell(0);
		XSSFCell cell1_1 = row1.createCell(1);
		cell0_0.setCellStyle(cellStyle);
		cell0_1.setCellStyle(cellStyle);
		cell1_0.setCellStyle(cellStyle);
		cell1_1.setCellStyle(cellStyle);
		return wb;
	}
    
    
	/**
	 * @Description 在XLSX文件中画斜线
	 * @param sheet XSSFSheet格式对象
	 * @param rowIndex 斜线表头单元格行索引
	 * @param colIndex 斜线表头单元格列索引
	 * @param slashType 斜线类型
	 * @param slpList 斜线表头线集合
	 **/
	public static void drawLineXlsx(XSSFSheet sheet,
			int rowIndex, int colIndex, String slashType, 
			ArrayList<SlashLinePosition> slpList) 
	{
		XSSFDrawing xssfDrawing = sheet.createDrawingPatriarch();
		for (SlashLinePosition slp : slpList)
		{
			XSSFClientAnchor xssfClientAnchor = new XSSFClientAnchor(
					slp.getStartX(), slp.getStartY(), slp.getEndX(), slp.getEndY(),
					colIndex, rowIndex, colIndex, rowIndex);
			XSSFSimpleShape shape = xssfDrawing.createSimpleShape(xssfClientAnchor);
			if (slashType == "LEFT_TOP" || slashType == "RIGHT_BOTTOM") {
				//斜线类型为左上，右下的时候，使用斜线
				shape.setShapeType(ShapeTypes.LINE);
			} else {
				//斜线类型为左下，右上的时候，使用反斜线
				shape.setShapeType(ShapeTypes.LINE_INV);
			}
			// 设置线宽
			shape.setLineWidth(0.5);
			// 设置线的风格
			shape.setLineStyle(0);
			// 设置线的颜色
			shape.setLineStyleColor(0, 0, 0);
		}
	}

	/**
	 * @Description 获取斜线表头斜线开始结束位置坐标对象集合
	 * @param slashType 斜线类型 LEFT_TOP左上角为起点 LEFT_BOTTOM左下角为起点 
	 *        RIGHT_TOP右上角为起点 RIGHT_BOTTOM右下角为起点
	 * @param slashCount 斜线数量  一条斜线=2
	 * @param xssfSheet XLSX格式sheet对象
	 * @param rowIndex 斜线表头单元格行索引
	 * @param colIndex 斜线表头单元格列索引
	 * @return java.util.ArrayList 位置坐标对象集合
	 **/
	public static ArrayList<SlashLinePosition> getSlashLinePositionXlsx(
			  String slashType,int slashCount,
			  XSSFSheet xssfSheet, int rowIndex, int colIndex) {
		ArrayList<SlashLinePosition> slpList = new ArrayList<>();
		int width = xssfSheet.getColumnWidth(colIndex) * 300;
		int height = xssfSheet.getRow(rowIndex).getHeight() * 700;
		switch (slashType) {
		case "LEFT_TOP":
			if (slashCount == 2) {
				//表示只有一条斜线
				SlashLinePosition slp = new SlashLinePosition(0, 0, width, height);
				slpList.add(slp);
			} 
			else if (slashCount == 3) {
				//表示有两条斜线
				SlashLinePosition slp1 = new SlashLinePosition(0, 0, width / 2, height);
				slpList.add(slp1);
				SlashLinePosition slp2 = new SlashLinePosition(0, 0, width, height / 2);
				slpList.add(slp2);
			}
			break;
		case "LEFT_BOTTOM":
			if (slashCount == 2) {
				//表示只有一条斜线
				SlashLinePosition slp = new SlashLinePosition(0, 0, width, height);
				slpList.add(slp);
			} else if (slashCount == 3) {
				//表示有两条斜线
				SlashLinePosition slp1 = new SlashLinePosition(0, 0, width / 2, height);
				slpList.add(slp1);
				SlashLinePosition slp2 = new SlashLinePosition(0, height / 2, width, height);
				slpList.add(slp2);
			}
			break;
		case "RIGHT_TOP":
			if (slashCount == 2) {
				//表示只有一条斜线
				SlashLinePosition slp = new SlashLinePosition(0, 0, width, height);
				slpList.add(slp);
			} else if (slashCount == 3) {
				//表示有两条斜线
				SlashLinePosition slp1 = new SlashLinePosition(0, 0, width, height / 2);
				slpList.add(slp1);
				SlashLinePosition slp2 = new SlashLinePosition(width / 2, 0, width, height);
				slpList.add(slp2);
			}
			break;
		case "RIGHT_BOTTOM":
			if (slashCount == 2) {
				//表示只有一条斜线
				SlashLinePosition slp = new SlashLinePosition(0, 0, width, height);
				slpList.add(slp);
			} else if (slashCount == 3) {
				//表示有两条斜线
				SlashLinePosition slp1 = new SlashLinePosition(0, height / 2, width, height);
				slpList.add(slp1);
				SlashLinePosition slp2 = new SlashLinePosition(width / 2, 0, width, height);
				slpList.add(slp2);
			}
			break;
		default:
			break;
		}
		return slpList;
	}
	public static XSSFCellStyle getCellFormat(XSSFWorkbook wb) 
	{
		XSSFCellStyle cellStyle = wb.createCellStyle();
		if (cellStyle.getBorderBottom() != BorderStyle.THIN) {
			cellStyle.setBorderBottom(BorderStyle.THIN);
		}
		if (cellStyle.getBorderLeft() != BorderStyle.THIN) {
			cellStyle.setBorderLeft(BorderStyle.THIN);
		}
		if (cellStyle.getBorderTop() != BorderStyle.THIN) {
			cellStyle.setBorderTop(BorderStyle.THIN);
		}
		if (cellStyle.getBorderRight() != BorderStyle.THIN) {
			cellStyle.setBorderRight(BorderStyle.THIN);
		}
		cellStyle.setBottomBorderColor((short) 0);
		cellStyle.setLeftBorderColor((short) 0);
		cellStyle.setRightBorderColor((short) 0);
		cellStyle.setTopBorderColor((short) 0);
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		XSSFFont xssfFont = wb.createFont();
		xssfFont.setFontName("Arial");
		cellStyle.setFont(xssfFont);
		return cellStyle;
	}
}
