static void WriteTitle(ExcelPackage package, Type type, FieldInfo[] fields) { var worksheet = package.Workbook.Worksheets.Add("info"); worksheet.Cells[1, 1].Value = type.FullName; for (int i = 0; i < fields.Length; i++) { string typeStr = DataDealer.ToString(fields[i].FieldType); worksheet.Cells[2, i + 1].Value = $"{typeStr} {fields[i].Name}"; //Debug.Log(worksheet.Cells[2, i + 1].Value); } }
public static void GenrateExcel(Type type) { string name = null; var fileAtt = type.GetCustomAttribute <FileName>(); if (fileAtt != null) { name = fileAtt.name; } else { name = type.FullName; } string path = Path.Combine(DirectroyWatcher.configExcel, $"{name}.xlsx"); //if (!File.Exists(path)) // File.Create(path).Dispose(); var currinfo = new FileInfo(path); using (ExcelPackage package = new ExcelPackage(currinfo)) { var fields = type.GetFields(); ExcelWorksheet worksheet = package.Workbook.Worksheets["info"]; if (worksheet == null) { WriteTitle(package, type, fields); package.Save(); } else { var strFields = DataDealer.ToStringArray(fields); if (!isMatch(worksheet, strFields)) { if (!Directory.Exists(backupPath)) { Directory.CreateDirectory(backupPath); } string backPath = $"{backupPath}/{name}-{DateTime.Now.ToString("yyyy-MM-dd-hh_mm_ss")}.xlsx"; File.Copy(path, backPath); File.Delete(path); using (ExcelPackage newPack = new ExcelPackage(new FileInfo(path))) { WriteTitle(newPack, type, fields); var newsheet = newPack.Workbook.Worksheets["info"]; WriteDatas(worksheet, newsheet, strFields); newPack.Save(); } } } } AssetDatabase.Refresh(); Debug.Log($"File ({name}) Loaction = {path}"); }
public static bool isMatch(string fieldTypeStr, Type fieldType, out DataDealer dealer) { dealer = default; for (int i = 0; i < dealers.Length; i++) { if (dealers[i].isSupport(fieldTypeStr)) { bool contain = dealers[i].isContain(fieldType); if (contain) { dealer = dealers[i]; } return(contain); } } return(false); }
bool isTypeVailed(string[] values, FieldInfo[] fields, out DataDealer[] typeStrs) { typeStrs = default; if (values.Length != fields.Length) { return(false); } typeStrs = new DataDealer[values.Length]; for (int i = 0; i < values.Length; i++) { string[] info = values[i].Split(' '); if (!DataDealer.isMatch(info[0], fields[i].FieldType, out var dealer) || fields[i].Name != info[1].Trim()) { return(false); } typeStrs[i] = dealer; } return(true); }