Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
ExcelData.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Reflection;
6using NPOI.SS.UserModel;
7using NPOI.XSSF.UserModel;
8using UnityEngine;
9
10public class ExcelData
11{
12 public class Sheet
13 {
14 public Dictionary<string, Dictionary<string, string>> map;
15
16 public List<Dictionary<string, string>> list;
17 }
18
19 public int startIndex = 5;
20
21 public XSSFWorkbook book;
22
23 public IRow rowIndex;
24
25 public Dictionary<string, Sheet> sheets = new Dictionary<string, Sheet>();
26
27 public string path;
28
29 public int maxEmptyRows;
30
31 public DateTime lastModified;
32
33 public ExcelData()
34 {
35 }
36
37 public ExcelData(string _path, int _index)
38 {
39 path = _path;
40 startIndex = _index;
41 BuildMap();
42 }
43
44 public ExcelData(string _path)
45 {
46 path = _path;
47 }
48
49 public void LoadBook()
50 {
51 if (book != null)
52 {
53 if (lastModified.Equals(File.GetLastWriteTime(path)))
54 {
55 return;
56 }
57 Debug.Log("Excel file modified:" + path);
58 sheets.Clear();
59 }
60 lastModified = File.GetLastWriteTime(path);
61 TextAsset textAsset = Resources.Load(path) as TextAsset;
62 if ((bool)textAsset)
63 {
64 Stream @is = new MemoryStream(textAsset.bytes);
65 book = new XSSFWorkbook(@is);
66 return;
67 }
68 using FileStream is2 = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
69 book = new XSSFWorkbook((Stream)is2);
70 }
71
72 public bool IsModified()
73 {
74 return !lastModified.Equals(File.GetLastWriteTime(path));
75 }
76
77 public virtual void BuildMap(string sheetName = "_default")
78 {
79 BuildList(sheetName);
80 Sheet sheet = sheets[sheetName];
81 if (sheet.map != null)
82 {
83 return;
84 }
85 sheet.map = new Dictionary<string, Dictionary<string, string>>();
86 foreach (Dictionary<string, string> item in sheet.list)
87 {
88 sheet.map[item["id"]] = item;
89 }
90 }
91
92 public List<Dictionary<string, string>> BuildList(string sheetName = "_default")
93 {
94 LoadBook();
95 if (sheetName.IsEmpty())
96 {
97 sheetName = "_default";
98 }
99 Sheet sheet = sheets.TryGetValue(sheetName);
100 if (sheet == null)
101 {
102 sheet = new Sheet();
103 sheets[sheetName] = sheet;
104 }
105 if (sheet.list != null)
106 {
107 return sheet.list;
108 }
109 sheet.list = new List<Dictionary<string, string>>();
110 ISheet sheet2 = (string.IsNullOrEmpty(sheetName) ? book.GetSheetAt(0) : (book.GetSheet(sheetName) ?? book.GetSheetAt(0)));
111 rowIndex = sheet2.GetRow(0);
112 int num = 0;
113 for (int i = startIndex - 1; i <= sheet2.LastRowNum; i++)
114 {
115 IRow row = sheet2.GetRow(i);
116 if (row == null)
117 {
118 if (i >= 5)
119 {
120 num++;
121 if (num > maxEmptyRows)
122 {
123 break;
124 }
125 }
126 continue;
127 }
128 num = 0;
129 Dictionary<string, string> dictionary = new Dictionary<string, string>();
130 for (int j = 0; j < rowIndex.LastCellNum; j++)
131 {
132 ICell cell = row.GetCell(j);
133 string text = rowIndex.GetCell(j).ToString();
134 if (text.IsEmpty())
135 {
136 break;
137 }
138 try
139 {
140 dictionary.Add(text, (cell == null) ? "" : cell.ToString());
141 }
142 catch
143 {
144 }
145 }
146 sheet.list.Add(dictionary);
147 }
148 return sheet.list;
149 }
150
151 public void Override(Dictionary<string, SourceData> sources, bool canAddData = true)
152 {
153 using FileStream @is = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
154 book = new XSSFWorkbook((Stream)@is);
155 for (int i = 0; i < book.NumberOfSheets; i++)
156 {
157 ISheet sheetAt = book.GetSheetAt(i);
158 ExcelParser.rowDefault = sheetAt.GetRow(2);
159 SourceData value = null;
160 if (!sources.TryGetValue(sheetAt.SheetName, out value))
161 {
162 Debug.Log(sheetAt.SheetName + " does not exist in sourceMap.");
163 continue;
164 }
165 Type type = value.GetType();
166 IList list = type.GetField("rows").GetValue(value) as IList;
167 IDictionary dictionary = type.GetField("map").GetValue(value) as IDictionary;
168 Type type2 = list.GetType().GetGenericArguments()[0];
169 List<FieldInfo> list2 = new List<FieldInfo>();
170 List<ExcelIndex> index = ExcelIndex.GetIndex(sheetAt);
171 for (int j = 0; j < index.Count; j++)
172 {
173 list2.Add(type2.GetField(index[j].name));
174 }
175 for (int k = startIndex - 1; k <= sheetAt.LastRowNum; k++)
176 {
177 string stringCellValue = (ExcelParser.row = sheetAt.GetRow(k)).GetCell(0).StringCellValue;
178 bool flag = true;
179 object obj;
180 if (dictionary.Contains(stringCellValue))
181 {
182 obj = dictionary[stringCellValue];
183 flag = false;
184 }
185 else
186 {
187 if (!canAddData)
188 {
189 continue;
190 }
191 obj = Activator.CreateInstance(type2);
192 }
193 for (int l = 0; l < index.Count; l++)
194 {
195 string type3 = index[l].type;
196 if (type3 == "str" || type3 == "string")
197 {
198 list2[l].SetValue(obj, ExcelParser.GetString(l));
199 }
200 }
201 if (flag)
202 {
203 list.Add(obj);
204 }
205 }
206 }
207 }
208
209 public string GetText(string id, string topic = "text")
210 {
211 BuildMap();
212 return sheets["_default"].map.TryGetValue(topic)?[id];
213 }
214}
List< Dictionary< string, string > > list
Definition: ExcelData.cs:16
Dictionary< string, Dictionary< string, string > > map
Definition: ExcelData.cs:14
bool IsModified()
Definition: ExcelData.cs:72
List< Dictionary< string, string > > BuildList(string sheetName="_default")
Definition: ExcelData.cs:92
ExcelData(string _path, int _index)
Definition: ExcelData.cs:37
IRow rowIndex
Definition: ExcelData.cs:23
int maxEmptyRows
Definition: ExcelData.cs:29
DateTime lastModified
Definition: ExcelData.cs:31
void Override(Dictionary< string, SourceData > sources, bool canAddData=true)
Definition: ExcelData.cs:151
Dictionary< string, Sheet > sheets
Definition: ExcelData.cs:25
int startIndex
Definition: ExcelData.cs:19
void LoadBook()
Definition: ExcelData.cs:49
virtual void BuildMap(string sheetName="_default")
Definition: ExcelData.cs:77
XSSFWorkbook book
Definition: ExcelData.cs:21
ExcelData()
Definition: ExcelData.cs:33
string path
Definition: ExcelData.cs:27
ExcelData(string _path)
Definition: ExcelData.cs:44
string GetText(string id, string topic="text")
Definition: ExcelData.cs:209
static List< ExcelIndex > GetIndex(ISheet sheet)
Definition: ExcelIndex.cs:14
static string GetString(int id)
Definition: ExcelParser.cs:102