package com.yifu.cloud.plus.v1.yifu.social.util;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @Author fxj
 * @Date 2024/12/9
 * @Description
 * @Version 1.0
 */
@Component
public class ExcelMergeImage {
	public static void main(String[] args) throws IOException {
		String excelFilePath = "E:\\test\\11\\1.xls";
		String imageFilePath = "E:\\test\\11\\1.png";
		//convertExcelToImage(excelFilePath, imageFilePath);
		//stampAdder();
	}

	public static void convertExcelToImage(String excelFilePath, String imageFilePath) throws IOException {
		FileInputStream fis = new FileInputStream(new File(excelFilePath));
		Workbook workbook;
		if (excelFilePath.endsWith(".xls")) {
			workbook = new HSSFWorkbook(fis); // For Excel 97-2003 (.xls) format
		} else if (excelFilePath.endsWith(".xlsx")) {
			workbook = new XSSFWorkbook(fis); // For Excel 2007 and later (.xlsx) format
		} else {
			throw new IllegalArgumentException("Unsupported file type");
		}
		Sheet sheet = workbook.getSheetAt(0);

		int rowCount = 16; //sheet.getPhysicalNumberOfRows();
		int colCount = 25;//sheet.getRow(0).getPhysicalNumberOfCells();

		BufferedImage image = new BufferedImage(896, 424, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = image.createGraphics();
		for (int i = 0; i < rowCount; i++) {
			Row row = sheet.getRow(i);
			for (int j = 0; j < colCount; j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					graphics.fillRect(j*150, i*47, 896, 424);
					graphics.setColor(Color.black); // 设置边框颜色
					graphics.drawRect(j*150, i*47, 896, 424);
					String cellValue = getCellValueAsString(cell);
					graphics.drawString(cellValue, j * 150+10, i * 45+30);
				}
			}
		}

		graphics.dispose();
		ImageIO.write(image, "png", new File(imageFilePath));
		workbook.close();
		fis.close();
	}

	private static String getCellValueAsString(Cell cell) {
		String cellValue = "";
		if (cell != null) {
			switch (cell.getCellType()) {
				case STRING:
					cellValue = cell.getStringCellValue();
					break;
				case NUMERIC:
					cellValue = String.valueOf(cell.getNumericCellValue());
					break;
				case BOOLEAN:
					cellValue = String.valueOf(cell.getBooleanCellValue());
					break;
				case FORMULA:
					cellValue = cell.getCellFormula();
					break;
				default:
					cellValue = "";
			}
		}
		return cellValue;
	}
	/**
	 * @Author fxj
	 * @Description 03格式excel添加电子印章或图片
	 * @Date 15:32 2024/12/9
	 * @Param
	 * @return
	**/
	public  FileOutputStream addPictureToExcel(FileInputStream fis,
													  InputStream imageIs,
													  FileOutputStream fileOut,
													  int SheetNum){
		Workbook workbook = null;
		try {
			workbook = new HSSFWorkbook(fis);
			Sheet sheet = workbook.getSheetAt(SheetNum);
			// 在合适的位置添加印章
			Drawing<?> drawing = sheet.createDrawingPatriarch();
			CreationHelper helper = workbook.getCreationHelper();
			ClientAnchor anchor = helper.createClientAnchor();
			anchor.setCol1(2);
			anchor.setRow1(0);
			anchor.setCol2(4);
			anchor.setRow2(7);
			// 添加印章图片
			int pictureIdx = workbook.addPicture(IOUtils.toByteArray(imageIs), Workbook.PICTURE_TYPE_PNG);
			drawing.createPicture(anchor, pictureIdx);
			workbook.write(fileOut);
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			try {
				if (null != fis){
					fis.close();
				}
				if (null != workbook){
					workbook.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return fileOut;
	}
}
