zoukankan      html  css  js  c++  java
  • Unity3D中Excel表的读取与写入

    废话不多说,对 Excel 表的操作少不了要引入第三方库,首先我们需要引入 Excel.dll 和 ICSharpCode.SharpZipLib.dll,这两个类库在网上都能找到;然后我们还需要引入 System.Data.dll,这个类库在 Unity3D 的安装路径下的 EditorDataMonolibmonounity 文件夹下能找到。

    代码如下:

     1 using Excel;
     2 using System.Data;
     3 using System.IO;
     4 using UnityEngine;
     5 
     6 public class Test : MonoBehaviour 
     7 {
     8     #region -- 变量定义
     9 
    10     #endregion
    11 
    12     #region -- 系统函数
    13     private void Start()
    14     {
    15         DataRowCollection _dataRowCollection = ReadExcel(Application.streamingAssetsPath + "/学生信息.xlsx");
    16         //这里从 1 开始循环,因为第一行被表头占据了。所以具体解析数据的时候需要根据具体情况来定。
    17         for (int i = 1; i < _dataRowCollection.Count; i++)
    18         {
    19             Debug.Log("学号:" + _dataRowCollection[i][0] + "--" + "姓名:" + _dataRowCollection[i][1] + "--" + "年龄:" + _dataRowCollection[i][2]);
    20         }
    21     }
    22     #endregion
    23 
    24     #region -- 自定义函数
    25     /// <summary>
    26     /// 读取 Excel 表并返回一个 DataRowCollection 对象
    27     /// </summary>
    28     /// <param name="_path">Excel 表路径</param>
    29     /// <param name="_sheetIndex">读取的 Sheet 索引。Excel 表中是有多个 Sheet 的</param>
    30     /// <returns></returns>
    31     private static DataRowCollection ReadExcel(string _path, int _sheetIndex = 0)
    32     {
    33         FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read);
    34         //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本
    35         IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
    36         DataSet result = excelReader.AsDataSet();
    37         return result.Tables[_sheetIndex].Rows;
    38     }
    39     /// <summary>
    40     /// 读取 Excel 表并返回一个 DataRowCollection 对象
    41     /// </summary>
    42     /// <param name="_path">Excel 表路径</param>
    43     /// <param name="_sheetIndex">读取的 Sheet 名称。Excel 表中是有多个 Sheet 的</param>
    44     /// <returns></returns>
    45     private static DataRowCollection ReadExcel(string _path, string _sheetName)
    46     {
    47         FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read);
    48         //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本
    49         IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
    50         DataSet result = excelReader.AsDataSet();
    51         return result.Tables[_sheetName].Rows;
    52     }
    53     #endregion
    54 
    55 }

    运行效果如下:

    这里需要注意的是,根据 Excel 表的版本不同,使用的方法也不一致,我在代码中也有注释,大家看一下就行。还有就是 Sheet ,在读取的时候,我们可以根据索引去读取,也可以根据名称去读取,我也写了重载方法。

     如果这样写,发布后运行,也许会报错,这时我们就又需要引入第三方库了,去 Unity3D 安装路径下的EditorDataMonolibmonounity,找到所有 I18N 开头的类库导入Unity中,就不会报错了,如下图:

     Excel 表的读取功能解决了,那我们如何生成一张 Excel 表,并写入数据呢?这时我们需要导入一个叫 EPPlus.dll 的类库,网上也有,大家可以自己下载。

    代码如下:

     1 private void Start()
     2     {
     3         string _filePath = Application.streamingAssetsPath + "/学生信息2.xlsx";
     4         string _sheetName = "详情";
     5 
     6         FileInfo _excelName = new FileInfo(_filePath);
     7         if (_excelName.Exists)
     8         {
     9             //删除旧文件,并创建一个新的 excel 文件。
    10             _excelName.Delete();
    11             _excelName = new FileInfo(_filePath);
    12         }
    13 
    14         //通过ExcelPackage打开文件
    15         using (ExcelPackage package = new ExcelPackage(_excelName))
    16         {
    17             //在 excel 空文件添加新 sheet,并设置名称。
    18             ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(_sheetName);
    19 
    20             //添加列名
    21             worksheet.Cells[1, 1].Value = "学号";
    22             worksheet.Cells[1, 2].Value = "姓名";
    23             worksheet.Cells[1, 3].Value = "性别";
    24  
    25             //添加一行数据
    26             worksheet.Cells[2, 1].Value = 100001;
    27             worksheet.Cells[2, 2].Value = "张三";
    28             worksheet.Cells[2, 3].Value = "";
    29 
    30             //添加一行数据
    31             worksheet.Cells[3, 1].Value = 100002;
    32             worksheet.Cells[3, 2].Value = "Hammer";
    33             worksheet.Cells[3, 3].Value = "";
    34 
    35             //添加一行数据
    36             worksheet.Cells[4, 1].Value = 120033;
    37             worksheet.Cells[4, 2].Value = "Saw";
    38             worksheet.Cells[4, 3].Value = "";
    39 
    40             //保存excel
    41             package.Save();
    42         }
    43     }

    运行效果图如下:

    Excel 表的读写操作大致就是这样的。但是我不建议,大家直接读取 Excel 表,因为 Excel 表 包含太多的格式信息,最好是将 Excel 表另存为纯文本的 CSV 文件再去读取,关于 CSV 文件的读取,网上有一大堆,有时间我自己也写一篇。

  • 相关阅读:
    December 23rd 2016 Week 52nd Friday
    December 22nd 2016 Week 52nd Thursday
    December 21st 2016 Week 52nd Wednesday
    December 20th 2016 Week 52nd Tuesday
    December 19th 2016 Week 52nd Sunday
    December 18th 2016 Week 52nd Sunday
    uva294(唯一分解定理)
    uva11624Fire!(bfs)
    fzu2150Fire Game(双起点bfs)
    poj3276Face The Right Way
  • 原文地址:https://www.cnblogs.com/xiaoyulong/p/11026079.html
Copyright © 2011-2022 走看看