# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/1/10
# 描述:
import warnings

from openpyxl import load_workbook


class Excel(object):
    __init_flag = False
    __instance = None

    def __new__(cls, file, sheet_name):
        cls.__instance = object.__new__(cls) if cls.__instance is None else cls.__instance
        return cls.__instance

    def __init__(self, file, sheet_name):
        if not Excel.__init_flag:
            Excel.__init_flag = True
            self.__file = file
            with warnings.catch_warnings(record=True):
                self.__wb = load_workbook(file)
                self.__sheet = self.__wb[sheet_name]

    def excel_save(self):
        self.__wb.save(self.__file)

    def excel_close(self):
        self.__wb.close()

    def excel_write(self, i, j, value):
        """
        excel写
        :param i: 行
        :param j: 列
        :param value: 值
        :return:
        """
        item = self.__sheet.cell(row=i, column=j)
        item.value = value

    def excel_get_max_line(self):
        """
        获取sheet最大行
        :return:
        """
        return self.__sheet.max_row

    def excel_read(self, i, j):
        """
        excel读取单元格
        :param i: 行
        :param j: 列
        :return:
        """
        item = self.__sheet.cell(row=i, column=j)
        return item.value
