unity读取excel以及写入excel

1.首先需要导入所需要的库文件,下载地址:https://download.csdn.net/download/qq_42987967/20035960

然后将这4个dll导入unity


2.接下来是读取,新建一个WriteExcel的脚本如下,我要读取的文件名是测试名单.xlsx,测试名单.xlsx直接放在了Asset下面。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
using Excel;
using System.Data;
using OfficeOpenXml;

public class WriteExcel : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // 项目根目录下的test.xlsx文件的路径:Application.dataPath + "/测试名单.xlsx" 
        FileStream fileStream = File.Open(Application.dataPath + "/测试名单.xlsx", FileMode.Open, FileAccess.Read);
        IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
        // 表格数据全部读取到result里
        DataSet result = excelDataReader.AsDataSet();

        // 获取表格列数
        int columns = result.Tables[0].Columns.Count;
        // 获取表格行数
        int rows = result.Tables[0].Rows.Count;

        // 根据行列依次打印表格中的每个数据
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                // 获取表格中指定行指定列的数据
                string value = result.Tables[0].Rows[i][j].ToString();
                Debug.Log(i + "行" + j + "列:" + value);
            }            
        }       
    }

}

3.接下来是写入。先新建一个要写入的excel,我命名为测试名单1.xlsx,也是直接放在了Asset目录下。然后新建一个ReadExcel脚本,如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
using Excel;
using System.Data;
using OfficeOpenXml;

public class ReadExcel : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string _filePath = Application.dataPath + "/测试名单2.xlsx";
        string _sheetName = "详情";

        FileInfo _excelName = new FileInfo(_filePath);
        if (_excelName.Exists)
        {
            //删除旧文件,并创建一个新的 excel 文件。
            _excelName.Delete();
            _excelName = new FileInfo(_filePath);
            Debug.Log("exist");
        }

        //通过ExcelPackage打开文件
        using (ExcelPackage package = new ExcelPackage(_excelName))
        {
            //在 excel 空文件添加新 sheet,并设置名称。
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(_sheetName);

            //添加列名
            worksheet.Cells[1, 1].Value = "学号";
            worksheet.Cells[1, 2].Value = "姓名";
            worksheet.Cells[1, 3].Value = "性别";

            //添加一行数据
            worksheet.Cells[2, 1].Value = 100001;
            worksheet.Cells[2, 2].Value = "张三";
            worksheet.Cells[2, 3].Value = "男";

            //添加一行数据
            worksheet.Cells[3, 1].Value = 100002;
            worksheet.Cells[3, 2].Value = "Hammer";
            worksheet.Cells[3, 3].Value = "女";

            //添加一行数据
            worksheet.Cells[4, 1].Value = 120033;
            worksheet.Cells[4, 2].Value = "Saw";
            worksheet.Cells[4, 3].Value = "男";

            //保存excel
            package.Save();
        }     
    }

}

猜你喜欢

转载自blog.csdn.net/qq_42987967/article/details/118499352
今日推荐