Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74939
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-30 14:22
文章分类

全部博文(48)

文章存档

2014年(47)

2013年(1)

我的朋友

分类: 其他平台

2014-03-03 10:56:37

在中,Mac下解析Excel ,这东西在Windows下有N种方法可以解析,但是在MAC上基本上都是不兼容的。。一般 Excel的格式分为两种一种是 .xls 还有一种是.xlsx ,这里我们只说.xlsx 

那么这里我就是在通过代码来解析UserLevel.xlsx了,代码比较简单我就不注释了。 这个例子我在MACWindows下都试验过都能很好的运行。喔对,这里我还引入了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在去把数据写在别的文件中,方面以后加密拓展等等。

阅读(956) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~