Exemple #1
0
        /// <summary>
        /// 导出Excel
        /// </summary>
        public void Export(ExcelFormat rExcelFormat)
        {
            string rConfigFile = UtilTool.PathCombine(this.ExcelConfigRootPath, "Excel", rExcelFormat.ExcelName);
            string rExportDir  = UtilTool.PathCombine(this.ExcelConfigRootPath, "Text");

            FileStream       rStream      = File.Open(rConfigFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            IExcelDataReader rExcelReader = ExcelReaderFactory.CreateOpenXmlReader(rStream);
            DataSet          rResult      = rExcelReader.AsDataSet();
            DataTable        rDataTable   = rResult.Tables[rExcelFormat.SheetName];

            if (rDataTable == null)
            {
                Debug.LogErrorFormat("Excel {0} has not sheet {1}.", rExcelFormat.ExcelName, rExcelFormat.SheetName);
                rExcelReader.Close();
                rStream.Close();
                return;
            }
            int rColumns = rDataTable.Columns.Count;
            int rRows    = rDataTable.Rows.Count;

            if (rRows == 0)
            {
                Debug.LogErrorFormat("Excel {0} has empty rows.", rExcelFormat.ExcelName);
                rExcelReader.Close();
                rStream.Close();
                return;
            }

            Type rDataType = MainAssemblyExpand.GetType(rExcelFormat.ClassName);

            if (rDataType == null)
            {
                Debug.LogErrorFormat("Excel {0} can not find Class {1}, please check it.", rExcelFormat.ExcelName, rExcelFormat.ClassName);
                rExcelReader.Close();
                rStream.Close();
                return;
            }

            var rTitleRow = rDataTable.Rows[0];
            var rFields   = new Dict <string, FieldInfo>();
            var rKeyIDs   = new Dict <string, int>();

            for (int i = 0; i < rColumns; i++)
            {
                FieldInfo rFileInfo = rDataType.GetField(rTitleRow[i].ToString());
                rFields.Add(rTitleRow[i].ToString(), rFileInfo);
                rKeyIDs.Add(rTitleRow[i].ToString(), i);
            }

            JsonNode rDataJson = new JsonClass();

            for (int i = 1; i < rRows; i++)
            {
                JsonNode rItemJson = new JsonClass();
                foreach (var rPair in rFields)
                {
                    string     rFieldValue = rDataTable.Rows[i][rKeyIDs[rPair.Key]].ToString();
                    JsonParser rJsonParser = new JsonParser(rFieldValue);
                    JsonNode   rTempNode   = null;
                    try {
                        rTempNode = rJsonParser.Parser();
                    }
                    catch (Exception) {
                        rJsonParser.isValid = false;
                    }
                    if (!rJsonParser.isValid)
                    {
                        rTempNode = new JsonData(rFieldValue);
                    }

                    rItemJson.Add(rPair.Key, rTempNode);
                }
                rDataJson.Add(rDataTable.Rows[i][rKeyIDs[rExcelFormat.PrimaryKey]].ToString(), rItemJson);
            }
            File.WriteAllText(UtilTool.PathCombine(rExportDir, rExcelFormat.SheetName + ".json"), rDataJson.ToString());
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            rExcelReader.Close();
            rStream.Close();
        }