首先需要下載解析的庫??EPPlus,? Excel,? ICSharpCode.SharpZipLib? ?
下載鏈接:
https://download.csdn.net/download/weixin_46472622/87238048
使用方法
我的Excel 表格是這樣的,每一列有一個(gè)關(guān)鍵詞
我用一個(gè)結(jié)構(gòu)體對(duì)象來表示
public struct ExaminationQuestions
{
public string equipmentType;//設(shè)備類型
public string questionType;//題目類型
public string index;//題目編號(hào)
public string questionPattern;//題目模式
public string questionDescription; //題目描述
public string questionOption;//題目選項(xiàng)
public string currentOption;//正確選項(xiàng)
public string explain;//說明
}
讀取的方法
/// <summary>
/// 讀取Excel 表格
/// </summary>
/// <param name="path">表格路徑</param>
/// <returns></returns>
public static DataSet ReadExcel(string path)
{
IExcelDataReader excelReader = null;
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
var str = path.Split('.');
if(str[1]=="xlsx")
{
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
if(str[1]=="xls")
{
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
// CreateOpenXmlReader用于讀取Excel2007版本及以上的文件
DataSet result = excelReader.AsDataSet();
stream.Close();
excelReader.Close();
return result;
}
/// <summary>
/// 加載表格的數(shù)據(jù)并轉(zhuǎn)換為對(duì)象
/// </summary>
/// <param name="path">表格路徑</param>
/// <returns>返回一個(gè)結(jié)構(gòu)題對(duì)象鏈表</returns>
public static List<ExaminationQuestions> Load(string path)
{
DataSet resultds = ReadExcel(path);//調(diào)用讀取的方法
int column = resultds.Tables[0].Columns.Count;
int row = resultds.Tables[0].Rows.Count;
Debug.LogWarning(column + " " + row);//行,列
List<ExaminationQuestions> _data = new List<ExaminationQuestions>();
for (int i =1; i < row; i++)
{
ExaminationQuestions temp_data;
temp_data.equipmentType = resultds.Tables[0].Rows[i][0].ToString();
temp_data.questionType = resultds.Tables[0].Rows[i][1].ToString();
temp_data.index = resultds.Tables[0].Rows[i][2].ToString();
temp_data.questionPattern = resultds.Tables[0].Rows[i][3].ToString();
temp_data.questionDescription = resultds.Tables[0].Rows[i][4].ToString();
temp_data.questionOption = resultds.Tables[0].Rows[i][5].ToString();
temp_data.currentOption = resultds.Tables[0].Rows[i][6].ToString();
temp_data.explain = resultds.Tables[0].Rows[i][7].ToString();
Debug.Log(temp_data.equipmentType + " " + temp_data.questionType+ " "+ temp_data.index + " " + temp_data.questionPattern + " " + temp_data.questionDescription + " " + temp_data.questionOption + " " + temp_data.currentOption + " " + temp_data.explain);
_data.Add(temp_data);
}
return _data;
}
}
?全部代碼,以及調(diào)用:
using Excel;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using UnityEngine;
public class XLSXConvert
{
void Start()
{
// 這里設(shè)置需要讀取的文件的路徑
string FilePath = Application.streamingAssetsPath + "/ExaminationQuestions.xlsx";
Load(FilePath);
}
/// <summary>
/// 讀取Excel 表格
/// </summary>
/// <param name="path">表格路徑</param>
/// <returns></returns>
public static DataSet ReadExcel(string path)
{
IExcelDataReader excelReader = null;
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
var str = path.Split('.');
if(str[1]=="xlsx")
{
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
if(str[1]=="xls")
{
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
// CreateOpenXmlReader用于讀取Excel2007版本及以上的文件
DataSet result = excelReader.AsDataSet();
stream.Close();
excelReader.Close();
return result;
}
/// <summary>
/// 加載表格的數(shù)據(jù)并轉(zhuǎn)換為對(duì)象
/// </summary>
/// <param name="path">表格路徑</param>
/// <returns>返回一個(gè)結(jié)構(gòu)題對(duì)象鏈表</returns>
public static List<ExaminationQuestions> Load(string path)
{
DataSet resultds = ReadExcel(path);//調(diào)用讀取的方法
int column = resultds.Tables[0].Columns.Count;
int row = resultds.Tables[0].Rows.Count;
Debug.LogWarning(column + " " + row);//行,列
List<ExaminationQuestions> _data = new List<ExaminationQuestions>();
for (int i =1; i < row; i++)
{
ExaminationQuestions temp_data;
temp_data.equipmentType = resultds.Tables[0].Rows[i][0].ToString();
temp_data.questionType = resultds.Tables[0].Rows[i][1].ToString();
temp_data.index = resultds.Tables[0].Rows[i][2].ToString();
temp_data.questionPattern = resultds.Tables[0].Rows[i][3].ToString();
temp_data.questionDescription = resultds.Tables[0].Rows[i][4].ToString();
temp_data.questionOption = resultds.Tables[0].Rows[i][5].ToString();
temp_data.currentOption = resultds.Tables[0].Rows[i][6].ToString();
temp_data.explain = resultds.Tables[0].Rows[i][7].ToString();
Debug.Log(temp_data.equipmentType + " " + temp_data.questionType+ " "+ temp_data.index + " " + temp_data.questionPattern + " " + temp_data.questionDescription + " " + temp_data.questionOption + " " + temp_data.currentOption + " " + temp_data.explain);
_data.Add(temp_data);
}
return _data;
}
}
public struct ExaminationQuestions
{
public string equipmentType;//設(shè)備類型
public string questionType;//題目類型
public string index;//題目編號(hào)
public string questionPattern;//題目模式
public string questionDescription; //題目描述
public string questionOption;//題目選項(xiàng)
public string currentOption;//正確選項(xiàng)
public string explain;//說明
}
如果是打包PC端的exe,需要將編輯器下的I18N導(dǎo)入工程,打包就可以成功解析了。
(打包方式為mono的可以,為IL2Cpp的不行)2019.4.25f1c1\Editor\Data\MonoBleedingEdge\lib\mono\unity
?這樣就可以成功解析,如果是IL2cpp要解析Excel表格,可以參考這篇文章來源:http://www.zghlxwxcb.cn/news/detail-520888.html
[Unity]FlexReader插件讀取excel解決IL2Cpp與I18N打包沖突的問題_DAGUNIANGZHOU的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-520888.html
到了這里,關(guān)于【Unity】用Excel庫讀取Excel表格(.xlsx或者.xls)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!