using Excel; using Newtonsoft.Json; using OfficeOpenXml; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using System.Text; using System.Text.RegularExpressions; using UnityEditor; using UnityEngine;
publicclassExcelToCSharpClass { [MenuItem("Assets/ExcelTool/ExcelToCSharpClass")] publicstaticvoidExcelToCSharpClassMethod() { var selectedObject = Selection.activeObject; if (selectedObject == null) { Debug.LogError("没有选中需要转换的文件"); return; } var assetPath = AssetDatabase.GetAssetPath(selectedObject); if (Path.GetExtension(assetPath).Equals(".xlsx", StringComparison.OrdinalIgnoreCase)) { //说明选中的是单个Excel文件 ExcelToCSharpClassMethod(assetPath); } elseif(AssetDatabase.IsValidFolder(assetPath)) { //说明选中的是一个文件夹->遍历文件夹下的Excel文件 var filePaths = Directory.GetFiles(assetPath); foreach (var filePath in filePaths) { if (Path.GetExtension(filePath).Equals(".xlsx", StringComparison.OrdinalIgnoreCase)) { ExcelToCSharpClassMethod(filePath); } } } else { Debug.LogError("选中的文件不符合转换要求"); } }
//表格格式 //字段描述 序号 名称 //字段名 ID Name //字段类型 int string publicstaticvoidExcelToCSharpClassMethod(string filePath) {
var file = new FileInfo(filePath); using (ExcelPackage package = new ExcelPackage(file)) { var worksheet = package.Workbook.Worksheets["Sheet1"]; if (worksheet == null) { Debug.Log($"没有找到路径为{filePath}的工作表"); } else { ExceltToJsonData(filePath, worksheet); } package.Save(); } }
privatestaticvoidExceltToJsonData(string filePath,ExcelWorksheet worksheet) { //这里两个List记录了Excel里的字段(字段名和字段类型,一一对应 var dataNameList = new List<string>(); var dataTypeList = new List<string>();
var rowCount = worksheet.Dimension.End.Row; var colCount = worksheet.Dimension.End.Column; var tableData = new List<Dictionary<string, object>>(); //从1开始计数 for (var i = 4; i <= rowCount; i++) { var rowData = new Dictionary<string, object>(); var isRowEmpty = true; for (var j = 2; j <= colCount; j++) { //添加字段信息 var dataName = worksheet.Cells[2,j].Value.ToString().Trim(); var dataType = worksheet.Cells[3,j].Value.ToString().Trim().ToLowerInvariant(); if (!string.IsNullOrEmpty(dataName) && !string.IsNullOrEmpty(dataType)) { //避免重复添加 if (!dataNameList.Contains(dataName)) { dataNameList.Add(dataName); dataTypeList.Add(dataType); } } else { Debug.LogError($"{i + 1}行{j + 1}列格式有误,终止转换"); return; } //TODO:自行补充需要的数据类型 var valueStr = worksheet.Cells[i,j].Value.ToString().Trim(); if (!string.IsNullOrEmpty(valueStr)) { isRowEmpty = false; } switch (dataType) { case"int": if (string.IsNullOrEmpty(valueStr)) { rowData[dataName] = 0; } else { rowData[dataName] = int.Parse(valueStr); } break; case"float": if (string.IsNullOrEmpty(valueStr)) { rowData[dataName] = 0.0f; } else { rowData[dataName] = float.Parse(valueStr); } break; case"bool": if (string.IsNullOrEmpty(valueStr) || valueStr == "0" || string.Equals(valueStr, "false", StringComparison.OrdinalIgnoreCase)) { rowData[dataName] = false; } else { rowData[dataName] = true; } break; default: rowData[dataName] = valueStr; break; } } if (!isRowEmpty) { tableData.Add(rowData); } }