C# 读取 Excel 表格并显示数据到 Unity UI
using UnityEngine; using Excel; using System.Data; using System.IO; using UnityEngine.UI; using OfficeOpenXml; using System;
public class Read : MonoBehaviour {
public static int c;
public static int row;
public static int col;
public static string[,,] item;
void Start()
{
}
public void OnClick()
{
FileInfo _excelName = new FileInfo("C:/Users/Administrator/My project/Assets/信息.xlsx");
using (ExcelPackage package = new ExcelPackage(_excelName)) {
c = package.Workbook.Worksheets.Count;
ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; // 这里假设读取第一个工作表
row = worksheet.Dimension.End.Row;
col = worksheet.Dimension.End.Column;
// 初始化 item 数组
item = new string[c, row, col];
// 读取标题行
int tit = 11;
for (int cou = 0; cou < c; cou++)
{
for (int co = 1; co <= col; co++) // 从 1 开始遍历列
{
item[cou, 0, co - 1] = worksheet.Cells[1, co].Value.ToString(); // 读取标题行数据
Text text = NewUpdate.TiTSetter(tit++);
text.text = item[cou, 0, co - 1];
}
}
// 读取表格数据
int num = 21;
for (int cou = 0; cou < c; cou++)
{
for (int ro = 1; ro <= row; ro++) // 从 1 开始遍历行
{
for (int co = 1; co <= col; co++) // 从 1 开始遍历列
{
item[cou, ro - 1, co - 1] = worksheet.Cells[ro, co].Value.ToString();
InputField cell = NewUpdate.Setter(num++);
cell.text = item[cou, ro - 1, co - 1];
}
}
}
}
}
}
原文地址: https://www.cveoy.top/t/topic/ns6m 著作权归作者所有。请勿转载和采集!