Esempio n. 1
0
        /* Parse Model */
        public static EiaModel ParseModelOld(String ModelPath)
        {
            /* Open Workbook */
            SpreadsheetGear.IWorkbook _TestWb =
                SpreadsheetGear.Factory.GetWorkbook(ModelPath);

            /* Get parameter model, Index 0 always */
            SpreadsheetGear.IWorksheet _ModelSheet = _TestWb.Worksheets[0];
            SpreadsheetGear.IWorksheet _ListSheet = _TestWb.Worksheets["Lists"];

            /* Get access cells */
            SpreadsheetGear.IRange CellAccess = _ModelSheet.Cells;

            String DataStr = "";
            String ModelName = "";
            String Country = "";

            /* Cell Access Example: CellAccess["B1"] */
            int ListCount = 0;
            int Version = 0;

            /* Get version */
            Version = int.Parse(CellAccess["B150"].Formula);

            /* Parse model & country */
            String[] mcTokens = (Path.GetFileNameWithoutExtension(_TestWb.Name)).Split(new Char[] { '-' });
            ModelName = mcTokens[0].Trim();
            Country = mcTokens[1].Trim();

            /* Model Start */
            DataStr += "ModelName|" + ModelName + "|Country|" + Country + "|";

            /* Locate EIA Parser-Header */
            if (CellAccess["A150"].Formula.Contains("EIA"))
            {
                /* Parse */
                int DataGroup = 151;

                while (CellAccess["A" + DataGroup.ToString()].Formula != "")
                {
                    /* Allocate */
                    int NewListCount = 0;
                    DataStr += ParseDataGroup(_ListSheet, CellAccess, ListCount,
                        CellAccess["B" + DataGroup.ToString()].Formula,
                        CellAccess["C" + DataGroup.ToString()].Formula,
                        CellAccess["D" + DataGroup.ToString()].Formula, out NewListCount);
                    ListCount = NewListCount;

                    /* Increament */
                    DataGroup++;
                }
            }

            /* Create Object */
            EiaModel eModel = new EiaModel();
            eModel.Name = ModelName;
            eModel.Country = Country;
            eModel.Version = Version;
            eModel.Data = DataStr;
            eModel.FilePath = ModelPath;

            /* Done */
            return eModel;
        }
Esempio n. 2
0
        /* Parse Model - Version 2 */
        public static EiaModel ParseModel(String ModelPath)
        {
            AesEncryption Decryptor = new AesEncryption(Key, Vector);
            String ModelDescription = "";
            EiaModel eModel = new EiaModel();
            String mGuid = "";
            int Version = 0;

            /* Load in file -> It's f*****g encrypted */
            Byte[] EncryptedWb = File.ReadAllBytes(ModelPath);
            Byte[] WbData = Decryptor.StrToByteArray(Decryptor.Decrypt(EncryptedWb));

            /* Does model have a description? */
            if (File.Exists(Path.GetDirectoryName(ModelPath) + "\\"
                    + Path.GetFileNameWithoutExtension(ModelPath) + ".txt"))
                ModelDescription = File.ReadAllText(Path.GetDirectoryName(ModelPath) + "\\"
                    + Path.GetFileNameWithoutExtension(ModelPath) + ".txt");

            /* Calculate Path */
            using (MemoryStream mStream = new MemoryStream(WbData))
            {
                /* Open Workbook */
                SpreadsheetGear.IWorkbook _TestWb =
                    SpreadsheetGear.Factory.GetWorkbookSet().Workbooks.OpenFromStream(mStream);

                /* Get parameter model, Index 0 always */
                SpreadsheetGear.IWorksheet _ModelSheet = _TestWb.Worksheets[0];
                SpreadsheetGear.IWorksheet _ListSheet = _TestWb.Worksheets["Lists"];

                /* Get access cells */
                SpreadsheetGear.IRange CellAccess = _ModelSheet.Cells;

                /* Get version */
                try {
                    mGuid = CellAccess["A150"].Formula;
                    Version = int.Parse(CellAccess["B150"].Formula);
                }
                catch (Exception) {
                    Version = 1;
                }

                /* Parse model & country */
                String[] mcTokens = (Path.GetFileNameWithoutExtension(ModelPath)).Split(new Char[] { '-' });
                eModel.Name = mcTokens[0].Trim();
                eModel.Country = mcTokens[1].Trim();

                if (mcTokens.Length == 3)
                    eModel.Type = mcTokens[2].Trim();
                else
                    eModel.Type = "Default";

                /* Done with workbook */
                _TestWb.Close();

                /* Calculate the template name */
                String TempPath = Path.GetDirectoryName(ModelPath)
                    + "\\" + Path.GetFileNameWithoutExtension(ModelPath)
                    + " Template.xlsx";

                /* Finish object data */
                eModel.Guid = mGuid;
                eModel.Version = Version;
                eModel.Data = "";
                eModel.FilePath = ModelPath;
                eModel.Template = File.ReadAllBytes(TempPath);
                eModel.ShortDescription = (ModelDescription.Length > 20) ? ModelDescription.Substring(0, 20) + "..." : ModelDescription;
                eModel.LongDescription = ModelDescription;
            }

            /* Done */
            return eModel;
        }