WaiXie_QuestionSystem/Assets/Script/Excel/Read/ReadExcel.cs

157 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using UnityEngine;
using Excel;
using System.Data;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using OfficeOpenXml;
using OfficeOpenXml.Table;
using Unity.VisualScripting;
namespace ZXL.Excel
{
public class ReadExcel
{
//public static string Excel = "Book";
public static List<Player_DataInfo> SelectPlayerTable(string name, string sheetName = "Sheet1")
{
string excelName = name + ".xlsx";
List<Player_DataInfo> array = new List<Player_DataInfo>();
var objs = ReadExcel.Read(excelName, sheetName);
var xLeng = objs.GetLength(1);
var yLeng = objs.GetLength(0);
for (var i = 2; i < yLeng; i++) //列
{
string id = objs[i, 1].ToString();
string playerName = objs[i, 2].ToString();
Player_DataInfo info = new Player_DataInfo(
id,
playerName
);
array.Add(info);
}
return array;
}
public static List<GroupData_DataInfo> SelectGroupTable(string name, string sheetName = "Sheet1")
{
string excelName = name + ".xlsx";
List<GroupData_DataInfo> array = new List<GroupData_DataInfo>();
var objs = ReadExcel.Read(excelName, sheetName);
var xLeng = objs.GetLength(1);
var yLeng = objs.GetLength(0);
for (var i = 2; i < yLeng; i++) //列
{
string id = objs[i, 1].ToString();
string groupName = objs[i, 2].ToString();
GroupData_DataInfo info = new GroupData_DataInfo(
id,
groupName
);
array.Add(info);
}
return array;
}
/// <summary>
/// 读取 Excel ; 需要添加 Excel.dll; System.Data.dll;
/// </summary>
/// <param name="excelName">excel文件名</param>
/// <param name="sheetName">sheet名称</param>
/// <returns>DataRow的集合</returns>
static object[,] Read(string excelName, string sheetName)
{
object[,] objs = new object[,] { };
using FileStream fs = new FileStream(Application.streamingAssetsPath + "/Excel/" + excelName, FileMode.Open, FileAccess.Read, FileShare.Read);
using (var pck = new ExcelPackage(fs))
{
var sheet = pck.Workbook.Worksheets[sheetName];
var excelRange = sheet.Cells["b:g"];
int row = excelRange.Count(x => x.Address.Length == 2 && x.Address[1] == '2');
var enumerable = excelRange.Select((x, i) =>
{
if (x.Address.Length == 2 && x.Address[1] == '1')
{
return null;
}
return x.Value.ToString();
});
var strings = enumerable.Where(x => x is not null).ToArray();
List<string[]> list = new List<string[]>();
var stringsLength = strings.Length;
for (var i = 0; (i + row - 1) < stringsLength; i += row)
{
var strings1 = new string[row];
for (var i1 = 0; i1 < row; i1++)
{
strings1[i1] = strings[i + i1];
}
list.Add(strings1);
}
foreach (var strings1 in list)
{
Debug.Log(string.Join(',', strings1));
}
objs = sheet.Cells.Value as object[,];
return objs;
}
// string path = Application.streamingAssetsPath + "/Excel/" + excelName;
// //string path = Application.dataPath + "/" + excelName;
// FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
// IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//
// DataSet result = excelReader.AsDataSet();
// //int columns = result.Tables[0].Columns.Count;
// //int rows = result.Tables[0].Rows.Count;
//
// //tables可以按照sheet名获取也可以按照sheet索引获取
// //return result.Tables[0].Rows;
// return result.Tables[sheetName].Rows;
}
private static DataTable GetDataTable()
{
return new DataTable("");
}
/// <summary>
/// 读取 Excel ; 需要添加 Excel.dll; System.Data.dll;
/// </summary>
/// <param name="excelName">excel文件名</param>
/// <param name="sheetName">sheet索引</param>
/// <returns>DataRow的集合</returns>
static DataRowCollection Read(string excelName, int sheetIndex)
{
string path = Application.streamingAssetsPath + "/Excel/" + excelName;
//string path = Application.dataPath + "/" + excelName;
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
//int columns = result.Tables[0].Columns.Count;
//int rows = result.Tables[0].Rows.Count;
//tables可以按照sheet名获取也可以按照sheet索引获取
//return result.Tables[0].Rows;
return result.Tables[sheetIndex].Rows;
}
}
}