UnityExcel数据查看以及文件导入

汕尾海湾  金牌会员 | 2023-8-28 12:06:50 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 946|帖子 946|积分 2838

需要插件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;
        }
    }

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

汕尾海湾

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表