分类: 其他平台
2014-03-03 10:56:37
在中,Mac下解析Excel ,这东西在Windows下有N种方法可以解析,但是在MAC上基本上都是不兼容的。。一般 Excel的格式分为两种一种是 .xls 还有一种是.xlsx ,这里我们只说.xlsx 。
那么这里我就是在通过代码来解析UserLevel.xlsx了,代码比较简单我就不注释了。 这个例子我在MAC和Windows下都试验过都能很好的运行。喔对,这里我还引入了System.Data.dll 因为这里我使用了DataSet来遍历数据表。
01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.Text.RegularExpressions;
06using System.IO;
07using Excel;
08using System.Data;
09
10public class NewBehaviourScript : MonoBehaviour
11{
12 void Start ()
13 {
14 XLSX();
15
16 }
17
18 void XLSX()
19 {
20 FileStream stream = File.Open(Application.dataPath + "/UserLevel.xlsx", FileMode.Open, FileAccess.Read);
21 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
22
23 DataSet result = excelReader.AsDataSet();
24
25 int columns = result.Tables[0].Columns.Count;
26 int rows = result.Tables[0].Rows.Count;
27
28 for(int i = 0; i< rows; i++)
29 {
30 for(int j =0; j < columns; j++)
31 {
32 string nvalue = result.Tables[0].Rows[i][j].ToString();
33 Debug.Log(nvalue);
34 }
35 }
36 }
37
38}
最好不要在程序运行时去动态解析这个Excel 。我的做法是把利用这个类库把Excel里面的数据读取出来,然后自己用File在去把数据写在别的文件中,方面以后加密拓展等等。