翼度科技»论坛 编程开发 .net 查看内容

UnityExcel数据查看以及文件导入

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
需要插件EPPlus.dll、Excel.dll
///
    /// 读取 Excel 表并返回一个 DataRowCollection 对象
    ///
    /// Excel 表路径
    /// 读取的 Sheet 索引。Excel 表中是有多个 Sheet 的
    ///
    private static DataRowCollection ReadExcel(string _path, int _sheetIndex = 0)
    {
        FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read);
        //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本
        Excel.IExcelDataReader excelReader = Excel.ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
        DataSet result = excelReader.AsDataSet();
        return result.Tables[_sheetIndex].Rows;
    }
    ///
    /// 读取 Excel 表并返回一个 DataRowCollection 对象
    ///
    /// Excel 表路径
    /// 读取的 Sheet 名称。Excel 表中是有多个 Sheet 的
    ///
    private static DataRowCollection ReadExcel(string _path, string _sheetName)
    {
        FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read);
        //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本
        Excel.IExcelDataReader excelReader = Excel.ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
        DataSet result = excelReader.AsDataSet();
        return result.Tables[_sheetName].Rows;
    }
----------------------------------------------------------------------------------------------------------------------------------------
Excel文件导入--单例
private static SelectFileOrPath _Instance; //本类实例
    #region 属性
    private string _SelectFilePath; //选择的文件路径
    private string _SelectFile; //选择文件
    private string _FileName; //文件名称
                              //选择文件路径
    public string SelectFilePath
    {
        get
        {
            return _SelectFilePath;
        }
        set
        {
            _SelectFilePath = value;
        }
    }
    //选择文件
    public string SelectFile
    {
        get
        {
            return _SelectFile;
        }
        set
        {
            _SelectFile = value;
        }
    }
    //文件名称
    public string FileName
    {
        get
        {
            return _FileName;
        }
        set
        {
            _FileName = value;
        }
    }
    #endregion
    ///
    /// 本类实例
    ///
    ///
    public static SelectFileOrPath GetInstance()
    {
        if (_Instance == null)
        {
            _Instance = new SelectFileOrPath();
        }
        return _Instance;
    }
    ///
    /// 选择文件路径(Unity自带的方法)
    ///
    public void SelectFolderPath_Unity()
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog() == DialogResult.OK)
        {
            SelectFilePath = fbd.SelectedPath;
        }
    }
    ///
    /// 选择文件路径(调用Windows方法)
    ///
    public void SelectExcelFilePath_Windows()
    {
        OpenSelectFileName openFileName = new OpenSelectFileName();
        openFileName.structSize = Marshal.SizeOf(openFileName);
        openFileName.filter = "Excel文件(*.xlsx)\0*.xlsx";
        openFileName.file = new string(new char[256]);
        openFileName.maxFile = openFileName.file.Length;
        openFileName.fileTitle = new string(new char[64]);
        openFileName.maxFileTitle = openFileName.fileTitle.Length;
        openFileName.initialDir = UnityEngine.Application.streamingAssetsPath.Replace('/', '\\');//默认路径
        openFileName.title = "请选择文件";
        openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
        if (LocalDialog.GetSaveFileName(openFileName))
        {
            SelectFilePath = openFileName.file;
            FileName = openFileName.file;
        }
    }

来源:https://www.cnblogs.com/TriggerF/archive/2023/08/15/17629739.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具