Beispiel #1
0
        static void TransferFilesFromExcelToJson()
        {
            string exceDirectorylPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + EXCEL_DIRECTORY;
            string jsonDirectoryPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + JSON_DIRECTORY;
            if (!Directory.Exists(jsonDirectoryPath)) // 如果資料夾不存在
            {
                Directory.CreateDirectory(jsonDirectoryPath); // 建立目錄
            }

            ExcelToJsonString tableToJson = new ExcelToJsonString();
            int successFileCount = 0;

            Array dataLoadTags = Enum.GetValues(typeof(GlobalConst.DataLoadTag));
            foreach (GlobalConst.DataLoadTag dataLoadTag in dataLoadTags)
            {
                string dataJsonString;
                EnumClassValue dataConvertInfo;
                bool isSuccesGetAttr = CommonFunction.GetAttribute<EnumClassValue>(dataLoadTag, out dataConvertInfo);
                if (!isSuccesGetAttr) { continue; }

                string fileName = dataConvertInfo.FileName;
                System.Type dataType = dataConvertInfo.ClassType;

                ReadExcelToJsonStringError error = tableToJson.ReadExcelFile(exceDirectorylPath, dataConvertInfo, NeedReadSite.CLIENT, out dataJsonString, out debugMessage);

                if (error == ReadExcelToJsonStringError.NONE)
                {
                    #region JsonString To File
                    string filePath = jsonDirectoryPath + Path.DirectorySeparatorChar + fileName + JSON_EXT;
                    using (StreamWriter sw = new StreamWriter(filePath))
                    {
                        sw.Write(dataJsonString);
                    }
                    #endregion
                    debugMessage = string.Format("{0}將 {1} 資料轉換成json成功\n", debugMessage, exceDirectorylPath + Path.DirectorySeparatorChar + fileName + ".xlsx");
                    fileListMessage = string.Format("{0}{1}:O\n", fileListMessage, fileName);
                    ++successFileCount;
                }
                else
                {
                    debugMessage = string.Format("{0}取得{1}內資料失敗\n", debugMessage, fileName);
                    fileListMessage = string.Format("{0}{1}:X\n", fileListMessage, fileName);
                }
            }
            debugMessage = string.Format("{0}共轉換 {1}個檔案成功,{2}個檔案失敗\n", debugMessage, successFileCount, dataLoadTags.Length - successFileCount);
        }
Beispiel #2
0
 public void TearDown()
 {
     _excelToJsonString = null;
 }
Beispiel #3
0
 public void SetUp()
 {
     _excelToJsonString = new ExcelToJsonString();
 }
Beispiel #4
0
        public void TestJsonStringNotEmptyField()
        {
            EventConditionData ecd = new EventConditionData();
            ecd.CheckCondition1 = 2;
            ecd.CheckCondition2 = 1;
            ecd.CheckType = 0;
            ExcelToJsonString temp = new ExcelToJsonString();
            string jsonStr = temp.ObjectToJsonString(ecd);
            string realJsonStr = "{\"CheckType\":0,\"CheckCondition1\":2,\"CheckCondition2\":1}";

            Assert.AreEqual(realJsonStr, jsonStr);
        }