Exemple #1
0
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="theList"></param>
        /// <returns></returns>
        private static bool WriteExcel(I18NItems theList)
        {
            foreach (var lang in AppConfig.I18NLanguages)
            {
                var excelPath = string.Format("{0}/I18N/{1}.xlsx", AppConfig.SettingSourcePath, lang);

                var       changedFile = false;
                ExcelFile excelFile;
                try
                {
                    excelFile = new ExcelFile(excelPath);
                }
                catch (IOException e)
                {
                    Debug.LogError("无法打开Excel,是否正在编辑: " + excelPath + " " + e.Message);
                    return(false);
                }
                // 把表中的值缓存起来! 方便查询是否已经存在过了!
                var id2RowMap     = new Dictionary <string, int>();
                var excelRowCount = excelFile.GetRowsCount();
                for (int row = 3; row < excelRowCount; row++) // 跳过前3行
                {
                    var key = excelFile.GetString("Id", row);
                    id2RowMap[key] = row;
                }

                var rowCount = excelFile.GetRowsCount();

                // 存在的不动,不存在的添加
                HashSet <string> useList = new HashSet <string>(); // 用来缓存处理过的,后面把旧的标记出来方便后面删
                foreach (var kv in theList.Items)
                {
                    var newKey   = kv.Key;
                    var srcsList = kv.Value; // 来源,几个字符串来源
                    int existRow;
                    if (!id2RowMap.TryGetValue(newKey, out existRow))
                    {
                        existRow = rowCount++;
                        excelFile.SetRow("Id", existRow, newKey);
                        changedFile = true;
                    }

                    // 来源列, 覆盖上去
                    //var sourcesStr = excelFile.GetString("CommentSrcFile", existRow);
                    //var existStrsList = CTool.Split<string>(sourcesStr, '|');
                    //var unionStrs = existStrsList.Union(srcsList);
                    var srcStr        = string.Join("|", srcsList.ToArray());
                    var srcStrInExcel = excelFile.GetString("CommentSrcFile", existRow);
                    if (srcStr != srcStrInExcel)
                    {
                        excelFile.SetRow("CommentSrcFile", existRow, srcStr);
                        changedFile = true;
                    }

                    // 已存在,不处理,添加到列表,后面检测没用的
                    useList.Add(newKey);
                }

                // useList 和 theList做差集,找出没用的!
                if (id2RowMap.Count > useList.Count)
                {
                    var exceptList = id2RowMap.Keys.Except(useList);
                    //var exceptList2 = useList.Except(id2RowMap.Keys);
                    //var exCount = exceptList.Count();
                    foreach (var exceptStr in exceptList)
                    {
                        var exceptRow = id2RowMap[exceptStr];
                        Log.Info("多出来的: {0}, 旧的,所在行 {1}, 删除!", exceptStr, exceptRow);
                        //excelFile.SetRowGrey(exceptRow);
                        excelFile.ClearRow(exceptRow);
                        changedFile = true;
                    }
                }
                if (changedFile) // 有修改的才保存
                {
                    excelFile.Save();
                }
                Log.Info("Write Excel: {0}", excelPath);
            }
            return(true);
        }