146 lines
4.0 KiB
C#
146 lines
4.0 KiB
C#
using UnityEngine;
|
|
using System.IO;
|
|
using NPOI.SS.UserModel;
|
|
using NPOI.XSSF.UserModel;
|
|
using NPOI.HSSF.UserModel;
|
|
using TMPro;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Runtime.InteropServices;
|
|
using System;
|
|
using System.Reflection;
|
|
|
|
public class ReadExlAndSetValue : MonoBehaviour
|
|
{
|
|
string basePath=Application.streamingAssetsPath+ "/ExcelData";
|
|
|
|
public string excelName;
|
|
public string sheetName;
|
|
public string keyName;
|
|
|
|
|
|
public void Awake()
|
|
{
|
|
//System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
// 手动注册编码提供程序
|
|
|
|
FillFromExcel();
|
|
}
|
|
|
|
|
|
|
|
private void FillFromExcel()
|
|
{
|
|
string filePath = Path.Combine( basePath, excelName+ ".xlsx");
|
|
if (!File.Exists(filePath))
|
|
{
|
|
Debug.LogWarning($"Excel 文件不存在: {filePath}");
|
|
return;
|
|
}
|
|
|
|
IWorkbook workbook;
|
|
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
|
{
|
|
//if (Path.GetExtension(fileName).ToLower() == ".xls")
|
|
// workbook = new HSSFWorkbook(fs);
|
|
//else
|
|
// workbook = new XSSFWorkbook(fs);
|
|
workbook = new XSSFWorkbook(fs);
|
|
}
|
|
|
|
//string sheetName = sheetName; // Sheet 名与挂载物体名一致
|
|
ISheet sheet = workbook.GetSheet(sheetName);
|
|
if (sheet == null)
|
|
{
|
|
Debug.LogWarning($"Sheet 不存在: {sheetName}");
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i <= sheet.LastRowNum; i++)
|
|
{
|
|
IRow row = sheet.GetRow(i);
|
|
if (row == null) continue;
|
|
|
|
ICell keyCell = row.GetCell(0);
|
|
ICell valueCell = row.GetCell(1);
|
|
if (keyCell == null || valueCell == null) continue;
|
|
|
|
string key = GetCellStringValue(keyCell);
|
|
|
|
|
|
|
|
if (key == keyName)
|
|
{
|
|
string value = GetCellStringValue(valueCell);
|
|
ApplyValueToChild(gameObject, value);
|
|
break;
|
|
}
|
|
//Transform target = FindTargetByKey(transform, key);
|
|
//if (target != null)
|
|
//{
|
|
// ApplyValueToChild(target.gameObject, value);
|
|
//}
|
|
//else
|
|
//{
|
|
// Debug.LogWarning($"子物体未找到: {key}");
|
|
//}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private string GetCellStringValue(ICell cell)
|
|
{
|
|
if (cell == null) return "";
|
|
switch (cell.CellType)
|
|
{
|
|
case CellType.String: return cell.StringCellValue;
|
|
case CellType.Numeric: return cell.NumericCellValue.ToString();
|
|
case CellType.Boolean: return cell.BooleanCellValue.ToString();
|
|
case CellType.Formula:
|
|
if (cell.CachedFormulaResultType == CellType.String)
|
|
return cell.StringCellValue;
|
|
if (cell.CachedFormulaResultType == CellType.Numeric)
|
|
return cell.NumericCellValue.ToString();
|
|
return cell.ToString();
|
|
default: return "";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 将 Excel value 填充到自身组件
|
|
/// </summary>
|
|
private void ApplyValueToChild(GameObject go, string value)
|
|
{
|
|
// TMP_InputField 优先
|
|
TMP_InputField tmpInput = go.GetComponent<TMP_InputField>();
|
|
if (tmpInput != null)
|
|
{
|
|
tmpInput.text = value;
|
|
return; // 不再操作子物体
|
|
}
|
|
|
|
TMP_Dropdown tmpDropdown = go.GetComponent<TMP_Dropdown>();
|
|
if (tmpDropdown != null)
|
|
{
|
|
string[] options = value.Split('-');
|
|
tmpDropdown.ClearOptions();
|
|
tmpDropdown.AddOptions(new List<string>(options));
|
|
if (options.Length > 0)
|
|
{
|
|
tmpDropdown.value = 0; // 默认选第一个
|
|
tmpDropdown.RefreshShownValue();
|
|
}
|
|
return;
|
|
}
|
|
|
|
TMP_Text tmpText = go.GetComponent<TMP_Text>();
|
|
if (tmpText != null)
|
|
{
|
|
tmpText.text = value;
|
|
}
|
|
}
|
|
}
|