2using System.Collections;
3using System.Collections.Generic;
5using System.Reflection;
6using NPOI.SS.UserModel;
7using NPOI.XSSF.UserModel;
14 public Dictionary<string, Dictionary<string, string>>
map;
16 public List<Dictionary<string, string>>
list;
25 public Dictionary<string, Sheet>
sheets =
new Dictionary<string, Sheet>();
61 TextAsset textAsset = Resources.Load(
path) as TextAsset;
64 Stream @is =
new MemoryStream(textAsset.bytes);
65 book =
new XSSFWorkbook(@is);
68 using FileStream is2 = File.Open(
path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
69 book =
new XSSFWorkbook((Stream)is2);
77 public virtual void BuildMap(
string sheetName =
"_default")
81 if (sheet.
map !=
null)
85 sheet.map =
new Dictionary<string, Dictionary<string, string>>();
86 foreach (Dictionary<string, string>
item in sheet.list)
92 public List<Dictionary<string, string>>
BuildList(
string sheetName =
"_default")
95 if (sheetName.IsEmpty())
97 sheetName =
"_default";
103 sheets[sheetName] = sheet;
105 if (sheet.
list !=
null)
109 sheet.list =
new List<Dictionary<string, string>>();
110 ISheet sheet2 = (
string.IsNullOrEmpty(sheetName) ?
book.GetSheetAt(0) : (
book.GetSheet(sheetName) ??
book.GetSheetAt(0)));
113 for (
int i =
startIndex - 1; i <= sheet2.LastRowNum; i++)
115 IRow row = sheet2.GetRow(i);
129 Dictionary<string, string> dictionary =
new Dictionary<string, string>();
130 for (
int j = 0; j <
rowIndex.LastCellNum; j++)
132 ICell cell = row.GetCell(j);
133 string text =
rowIndex.GetCell(j).ToString();
140 dictionary.Add(text, (cell ==
null) ?
"" : cell.ToString());
146 sheet.
list.Add(dictionary);
151 public void Override(Dictionary<string, SourceData> sources,
bool canAddData =
true)
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++)
157 ISheet sheetAt =
book.GetSheetAt(i);
158 ExcelParser.rowDefault = sheetAt.GetRow(2);
160 if (!sources.TryGetValue(sheetAt.SheetName, out value))
162 Debug.Log(sheetAt.SheetName +
" does not exist in sourceMap.");
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>();
171 for (
int j = 0; j < index.Count; j++)
173 list2.Add(type2.GetField(index[j].name));
175 for (
int k =
startIndex - 1; k <= sheetAt.LastRowNum; k++)
177 string stringCellValue = (ExcelParser.row = sheetAt.GetRow(k)).GetCell(0).StringCellValue;
180 if (dictionary.Contains(stringCellValue))
182 obj = dictionary[stringCellValue];
191 obj = Activator.CreateInstance(type2);
193 for (
int l = 0; l < index.Count; l++)
195 string type3 = index[l].type;
196 if (type3 ==
"str" || type3 ==
"string")
209 public string GetText(
string id,
string topic =
"text")
212 return sheets[
"_default"].map.TryGetValue(topic)?[id];
List< Dictionary< string, string > > list
Dictionary< string, Dictionary< string, string > > map
List< Dictionary< string, string > > BuildList(string sheetName="_default")
ExcelData(string _path, int _index)
void Override(Dictionary< string, SourceData > sources, bool canAddData=true)
Dictionary< string, Sheet > sheets
virtual void BuildMap(string sheetName="_default")
string GetText(string id, string topic="text")
static List< ExcelIndex > GetIndex(ISheet sheet)
static string GetString(int id)