using DG.Tweening.Plugins.Core.PathCore;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;
using UnityEngine;
using Path = System.IO.Path;

public class ExcelTool
{


    //拷贝 一份表 。
    public static bool CopyExcell(string path, string targetPath)
    {
        try
        {


        }
        catch (IOException e)
        {
            Debug.LogError($"文件拷贝失败：{e.Message}");
            return false;
        }
        return true;
    }

    public static (ISheet, string) ReadExcel(string excelPath, string sheetName = "Sheet1")
    {
        string extension = ".xlsx";
        if (Path.GetExtension(excelPath) != extension) return (null, string.Empty);
        string oldName = Path.GetFileNameWithoutExtension(excelPath);
        string tmpExcellPath = excelPath.Replace(oldName + extension, oldName + "Copy" + extension);

        if (File.Exists(tmpExcellPath)) File.Delete(tmpExcellPath);


        //try
        //{
        // 拷贝文件
        File.Copy(excelPath, tmpExcellPath, true);

        //打开文件流
        using (FileStream fs = File.Open(excelPath, FileMode.Open, FileAccess.Read))
        {

            // 创建工作簿
            IWorkbook workbook = new XSSFWorkbook(fs);
            // 获取工作表
            ISheet sheet = workbook.GetSheet(sheetName);

            return (sheet, tmpExcellPath); ;
        }

        //}
        //catch (Exception e)
        //{
        //    File.Delete(tmpExcellPath);
        //    Debug.LogError(e);
        //    return (null, string.Empty); ;
        //}



    }







}
