unity3d读写EXCEL文件的方法
对 Excel 表的操作少不了要引入第三方库,首先我们需要引入 Excel.dll 和 ICSharpCode.SharpZipLib.dll,这两个类库在网上都能找到;然后我们还需要引入 System.Data.dll,这个类库在 Unity3D 的安装路径下的 Editor\Data\Mono\lib\mono\unity 文件夹下能找到。wiseglove数据手套客户,可以在我们提供的数据手套FOR UNITY3D演示项目下找到。
using Excel;
using System.Data;
using System.IO;
using UnityEngine;
public class Test : MonoBehaviour
{
#region -- 变量定义
#endregion
#region -- 系统函数
private void Start()
{
DataRowCollection _dataRowCollection = ReadExcel(Application.streamingAssetsPath + "/手套录制数据.xlsx");
//这里从 1 开始循环,因为第一行被表头占据了。所以具体解析数据的时候需要根据具体情况来定。
for (int i = 1; i < _dataRowCollection.Count; i++)
{
Debug.Log("拇指" + _dataRowCollection[i][0] + "--" + "食指" + _dataRowCollection[i][1] + "--" + "中指" + _dataRowCollection[i][2])+ "--" + "无名指" + _dataRowCollection[i][2])+ "--" + "小指" + _dataRowCollection[i][2]);
} }
#endregion
#region -- 自定义函数
////// 读取 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版本
IExcelDataReader excelReader = 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版本
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
DataSet result = excelReader.AsDataSet();
return result.Tables[_sheetName].Rows;
}
#endregion
}
这里需要注意的是,根据 Excel 表的版本不同,使用的方法也不一致,我在代码中也有注释,大家看一下就行。还有就是 Sheet ,在读取的时候,我们可以根据索引去读取,也可以根据名称去读取,我也写了重载方法。
如果这样写,发布后运行,也许会报错,这时我们就又需要引入第三方库了,去 Unity3D 安装路径下的Editor\Data\Mono\lib\mono\unity,找到所有 I18N 开头的类库导入Unity中,就不会报错了。
OK, 现在Excel 表的读取功能解决了,那我们如何生成一张 Excel 表,并写入数据呢?这时我们需要导入一个叫 EPPlus.dll 的类库,网上也有,大家可以自己下载。
代码如下:
private void Start()
{
string _filePath = Application.streamingAssetsPath + "/录制数据.xlsx";
string _sheetName = "详情";
FileInfo _excelName = new FileInfo(_filePath);
if (_excelName.Exists)
{
//删除旧文件,并创建一个新的 excel 文件。
_excelName.Delete();
_excelName = new FileInfo(_filePath);
}
//通过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[1, 4].Value = "无名指";
worksheet.Cells[1, 5].Value = "小指";
//添加一行数据
worksheet.Cells[2, 1].Value = 10; //ms
worksheet.Cells[2, 2].Value = 33.0f;
worksheet.Cells[2, 3].Value = 34.0f;
worksheet.Cells[2, 4].Value = 35.0f;
worksheet.Cells[2, 5].Value = 36.0f;
//添加一行数据
worksheet.Cells[3, 1].Value = 20; //ms
worksheet.Cells[3, 2].Value = 33.0f;
worksheet.Cells[3, 3].Value = 34.0f;
worksheet.Cells[3, 4].Value = 35.0f;
worksheet.Cells[3, 5].Value = 36.0f;
//添加一行数据
worksheet.Cells[4, 1].Value = 30; //ms
worksheet.Cells[4, 2].Value = 33.0f;
worksheet.Cells[4, 3].Value = 34.0f;
worksheet.Cells[4, 4].Value = 35.0f;
worksheet.Cells[4, 5].Value = 36.0f;
//保存excel
package.Save();
}
}
Excel 表的读写操作大致就是这样的。因为 Excel 表 包含太多的格式信息,好是将 Excel 表另存为纯文本的 CSV 文件再去读取,我们另一篇技术文章讨论关于 CSV 文件的读取。
- 上一篇:UNITY3D读写CVS格式文件录制与存储数据手套数据 2019/11/12
- 下一篇:UNITY3D使用C#脚本的几种读写TXT文本文件的方法 2019/11/12
