UnityExcel数据查看以及文件导入

  • UnityExcel数据查看以及文件导入已关闭评论
  • 105 次浏览
  • A+
所属分类:.NET技术
摘要

需要插件EPPlus.dll、Excel.dll/// <summary>
/// 读取 Excel 表并返回一个 DataRowCollection 对象
/// </summary>
/// <param name=”_path”>Excel 表路径</param>
/// <param name=”_sheetIndex”>读取的 Sheet 索引。Excel 表中是有多个 Sheet 的</param>
/// <returns></returns>
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版本
Excel.IExcelDataReader excelReader = Excel.ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
DataSet result = excelReader.AsDataSet();
return result.Tables[_sheetIndex].Rows;
}
/// <summary>
/// 读取 Excel 表并返回一个 DataRowCollection 对象
/// </summary>
/// <param name=”_path”>Excel 表路径</param>
/// <param name=”_sheetIndex”>读取的 Sheet 名称。Excel 表中是有多个 Sheet 的</param>
/// <returns></returns>
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版本
Excel.IExcelDataReader excelReader = Excel.ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
DataSet result = excelReader.AsDataSet();
return result.Tables[_sheetName].Rows;
}

需要插件EPPlus.dll、Excel.dll

/// <summary>
/// 读取 Excel 表并返回一个 DataRowCollection 对象
/// </summary>
/// <param name="_path">Excel 表路径</param>
/// <param name="_sheetIndex">读取的 Sheet 索引。Excel 表中是有多个 Sheet 的</param>
/// <returns></returns>
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版本
Excel.IExcelDataReader excelReader = Excel.ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
DataSet result = excelReader.AsDataSet();
return result.Tables[_sheetIndex].Rows;
}
/// <summary>
/// 读取 Excel 表并返回一个 DataRowCollection 对象
/// </summary>
/// <param name="_path">Excel 表路径</param>
/// <param name="_sheetIndex">读取的 Sheet 名称。Excel 表中是有多个 Sheet 的</param>
/// <returns></returns>
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版本
Excel.IExcelDataReader excelReader = Excel.ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
DataSet result = excelReader.AsDataSet();
return result.Tables[_sheetName].Rows;
}

----------------------------------------------------------------------------------------------------------------------------------------

Excel文件导入--单例

private static SelectFileOrPath _Instance; //本类实例
#region 属性
private string _SelectFilePath; //选择的文件路径
private string _SelectFile; //选择文件
private string _FileName; //文件名称
//选择文件路径
public string SelectFilePath
{
get
{
return _SelectFilePath;
}
set
{
_SelectFilePath = value;
}
}
//选择文件
public string SelectFile
{
get
{
return _SelectFile;
}
set
{
_SelectFile = value;
}
}
//文件名称
public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
}
}
#endregion
/// <summary>
/// 本类实例
/// </summary>
/// <returns></returns>
public static SelectFileOrPath GetInstance()
{
if (_Instance == null)
{
_Instance = new SelectFileOrPath();
}
return _Instance;
}
/// <summary>
/// 选择文件路径(Unity自带的方法)
/// </summary>
public void SelectFolderPath_Unity()
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
SelectFilePath = fbd.SelectedPath;
}
}

/// <summary>
/// 选择文件路径(调用Windows方法)
/// </summary>
public void SelectExcelFilePath_Windows()
{
OpenSelectFileName openFileName = new OpenSelectFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "Excel文件(*.xlsx)*.xlsx";
openFileName.file = new string(new char[256]);
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = new string(new char[64]);
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.initialDir = UnityEngine.Application.streamingAssetsPath.Replace('/', '\');//默认路径
openFileName.title = "请选择文件";
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
if (LocalDialog.GetSaveFileName(openFileName))
{
SelectFilePath = openFileName.file;
FileName = openFileName.file;
}
}