Unity读写.xlsx表格文件

Unity读写.xlsx表格文件

用到的dll文件,下载地址
在这里插入图片描述

1、打开系统弹窗文件夹-写入文件

在这里插入图片描述

 private void SaveExcel()
    {
        OpenFileName ofn = new OpenFileName();
        ofn.structSize = Marshal.SizeOf(ofn);
        ofn.filter = "Excel Files(*.xlsx)\0*.xlsx\0";  //指定打开格式
        ofn.file = new string(new char[256]);
        ofn.maxFile = ofn.file.Length;
        ofn.fileTitle = new string(new char[64]);
        ofn.maxFileTitle = ofn.fileTitle.Length;
        ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
        ofn.title = "打开Excel";
        ofn.defExt = "xlsx";
        //注意 一下项目不一定要全选 但是0x00000008项不要缺少
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        //打开windows框
        if (DllTest.GetSaveFileName(ofn))
        {
            //TODO

            //把文件路径格式替换一下
            ofn.file = ofn.file.Replace("\\", "/");
           //Debug.Log(ofn.file); 
            FileInfo newFile = new FileInfo(ofn.file);
            if (newFile.Exists)
            {
                newFile.Delete();  // ensures we create a new workbook
                newFile = new FileInfo(ofn.file);
            }
            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                // 添加一个sheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("信息");
                //添加一点测试数据
                List<ImportClass> importClasses = new List<ImportClass>();
                importClasses.Add(new ImportClass("小刚", "789", "789789000"));
                importClasses.Add(new ImportClass("小亮", "147", "147147"));

                worksheet.Cells[1, 1, 1, 6].Merge = true;//合并单元格(1行1列到1行6列)
                worksheet.Cells["A1"].Value = "学生信息"; //显示
                worksheet.Cells["A1"].Style.Font.Size = 16; //字体大小
                worksheet.Cells["A1"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; //对其方式
                worksheet.Cells["A1"].Style.Border.BorderAround(ExcelBorderStyle.Thin); //表格边框
                package.Save();
            }
        }
    }

2、打开系统弹窗文件夹-读取文件

 private void ReadExcel()
    {
        OpenFileName ofn = new OpenFileName();
        ofn.structSize = Marshal.SizeOf(ofn);
        ofn.filter = "Excel Files(*.xlsx)\0*.xlsx\0";  //指定打开格式
        ofn.file = new string(new char[256]);
        ofn.maxFile = ofn.file.Length;
        ofn.fileTitle = new string(new char[64]);
        ofn.maxFileTitle = ofn.fileTitle.Length;
        ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
        ofn.title = "打开Excel";
        ofn.defExt = "xlsx";
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;

        //打开windows框
        if (DllTest.GetOpenFileName(ofn))
        {
            ofn.file = ofn.file.Replace("\\", "/");
            FileInfo info = new FileInfo(ofn.file);FileShare.Read);
            FileStream stream = info.Open(FileMode.Open, FileAccess.Read);

            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            DataSet result = excelReader.AsDataSet();
            int columns = result.Tables[0].Columns.Count;//获取列数    240
            int rows = result.Tables[0].Rows.Count;//获取行数          1001
                                                   // Debug.Log("行数:" + rows + "   列数:" + columns);
            for (int i = 0; i < columns; i++)
            {
                for (int j = 1; j < rows; j++)
                {
                   Debug.Log(result.Tables[0].Rows[j][i].ToString());
                }
            }
        }
    }

3、使用协程后台自动写入文件

    string _path;
 IEnumerator SaveEexcel_Times()
    {
        _path = "D:/GIS/shili/test.xlsx";//文件路径
        if (!File.Exists(_path))
        {
            Debug.LogError("文件不存在: " + _path);
            yield return null;
        }
        FileInfo newFile = new FileInfo(_path);
        if (newFile.Exists)
        {
            newFile.Delete();  // ensures we create a new workbook
            newFile = new FileInfo(_path);
        }
        using (ExcelPackage package = new ExcelPackage(newFile))
        {
            // 添加一个sheet
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("信息");
          
            //添加一点测试数据
            List<ImportClass> importClasses = new List<ImportClass>();
            importClasses.Add(new ImportClass("小刚", "789", "789789000"));
            importClasses.Add(new ImportClass("小亮", "147", "147147"));

            worksheet.Cells[1, 1, 1, 6].Merge = true;//合并单元格(1行1列到1行6列)
            worksheet.Cells["A1"].Value = "学生信息"; //显示
            worksheet.Cells["A1"].Style.Font.Size = 16; //字体大小
            worksheet.Cells["A1"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; //对其方式
            worksheet.Cells["A1"].Style.Border.BorderAround(ExcelBorderStyle.Thin); //表格边框

            Debug.Log("保存数据成功!");
            package.Save();
        }
    }

4、使用协程后台读取文件

 string _path;
 IEnumerator ReadEexcel_cunhuo()
    {
        _path = "D:/GIS/shili/result.xlsx";
        if (!File.Exists(_path))
        {
            Debug.LogError("文件不存在: " + _path);
            yield return null;
        }
        using (FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); ;//读取 2007及以后的版本
            DataSet result = excelReader.AsDataSet();
            int columns = result.Tables[0].Columns.Count;//获取列数    240
            int rows = result.Tables[0].Rows.Count;//获取行数          1001
                                                   // Debug.Log("行数:" + rows + "   列数:" + columns);
            for (int i = 0; i < columns; i++)
            {
                for (int j = 1; j < rows; j++)
                {
                   Debug.Log(result.Tables[0].Rows[j][i].ToString());
                }
            }
            yield return null;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ke-Di

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值