Esempio n. 1
0
        /// <summary>
        /// 验证用户输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkUserInput(object sender, RoutedEventArgs e)
        {
            //验证文件夹位置
            try
            {
                //文件夹不存在
                if (Directory.Exists(tbFileSavePath.Text) == false)
                {
                    //尝试创建
                    Directory.CreateDirectory(tbFileSavePath.Text);
                }
                //文件夹已存在,保存设置值
                Argument.OutPutDiretory = tbFileSavePath.Text;

                //验证列位置
                try
                {
                    //保存列位置
                    string strTarget = TargetColumn.Text;
                    if (strTarget.Trim().Equals("0"))
                    {
                        throw new Exception();
                    }
                    Argument.TargetColumn = Convert.ToInt32(strTarget);
                    //成功提示
                    OpenWindowsUtils.OpenMyMessageBox(true, "保存成功", "Save Success", this);
                }
                catch (Exception)
                {
                    OpenWindowsUtils.OpenMyMessageBox(true, "你输入的数字不正确", "The number you entered is incorrect.", this);
                    TargetColumn.Text        = String.Empty;
                    TargetColumn.BorderBrush = Brushes.Red;
                }
                //保存输出类型
                if ((bool)rbJSON.IsChecked)
                {
                    Argument.OutPutType = "json";
                }
                else
                {
                    Argument.OutPutType = "xml";
                }
            }
            catch (Exception)
            {
                OpenWindowsUtils.OpenMyMessageBox(true, "你输入的文件夹不存在", "The folder you entered does not exist.", this);
                tbFileSavePath.Text        = String.Empty;
                tbFileSavePath.BorderBrush = Brushes.Red;
            }

            //保存用户设置
            configura.OutPutDiretory = Argument.OutPutDiretory;
            configura.OutPutType     = Argument.OutPutType;
            configura.TargetColumn   = Argument.TargetColumn;
            SetConfiguraToJSON(configura, jsonSetFile);
        }
 /// <summary>
 /// 进度完成时的反馈
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void BackgroundWorker_OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
     //若不是控制台传参,发出提示与打开文件对话框
     if (Argument.FilePath == null && IsError == false)
     {
         OpenWindowsUtils.OpenMyMessageBox(true, "检查完毕", "Check complete", this);
         MopenFileDialog(Argument.OutPutDiretory);
     }
     btnHandle.IsEnabled   = true;
     btnOpenFile.IsEnabled = true;
 }
        /// <summary>
        /// 打开文件对话框
        /// </summary>
        private void MopenFileDialog(string initialDirectory)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            fileDialog.InitialDirectory = initialDirectory;
            try
            {
                fileDialog.ShowDialog();
            }
            catch (Exception e)
            {
                OpenWindowsUtils.OpenMyMessageBox(false, "无法打开结果文件!", "Unable to open the result file!", this);
            }
        }
        /// <summary>
        /// 语言选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BitmapImage bitmapImage = null;

            //英文
            if (cmbSelectLanguage.SelectedIndex == 0)
            {
                try
                {
                    //根据名字载入语言文件//相对路径
                    MyResourceDictionary.resource = Application.LoadComponent(new Uri(@"language\" + "en_US" + ".xaml", UriKind.Relative)) as ResourceDictionary;
                    bitmapImage = new BitmapImage(new Uri(@"image/us.jpg", UriKind.RelativeOrAbsolute));
                }
                catch (Exception e2)
                {
                    OpenWindowsUtils.OpenMyMessageBox(false, "语言包不存在", "Language packages do not exist", this);
                }
            } //中文
            else if (cmbSelectLanguage.SelectedIndex == 1)
            {
                try
                {
                    //根据名字载入语言文件//相对路径
                    MyResourceDictionary.resource = Application.LoadComponent(new Uri(@"language\" + "zh_CN" + ".xaml", UriKind.Relative)) as ResourceDictionary;
                    bitmapImage = new BitmapImage(new Uri(@"image/china.jpg", UriKind.RelativeOrAbsolute));
                }
                catch (Exception e2)
                {
                    OpenWindowsUtils.OpenMyMessageBox(false, "语言包不存在", "Language packages do not exist", this);
                }
            }
            //更新
            if (MyResourceDictionary.resource != null)
            {
                //如果已使用其他语言,先清空
                if (this.Resources.MergedDictionaries.Count > 0)
                {
                    this.Resources.MergedDictionaries.Clear();
                }
                this.Resources.MergedDictionaries.Add(MyResourceDictionary.resource);
            }
            if (imCountry != null)
            {
                imCountry.Source = bitmapImage;
            }
        }
        /// <summary>
        /// 开始检查译文
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void click_BeginCheckFile(object sender, RoutedEventArgs e)
        {
            //1 首先要有译文
            string filePath = tbFilePath.Text;

            if (File.Exists(filePath))
            {
                //还没设置译文列位置
                if (Argument.TargetColumn > 0 == false)
                {
                    User_SetUp(sender, e);
                }
                //格式转换xls-->xlsx
                if (filePath.EndsWith(".xls"))
                {
                    filePath = ConvertWorkbook(filePath, Argument.OutPutDiretory);
                }
                try
                {
                    //2 判断是否规范
                    if (ExcelUtils.IsTranslationFile(filePath))
                    {
                        //3 将参数交由后台线程去做主要的事情
                        backgroundWorker.RunWorkerAsync(new InputContainer(filePath));
                        btnHandle.IsEnabled   = false;
                        btnOpenFile.IsEnabled = false;
                    }
                }
                catch (Exception)
                {
                    OpenWindowsUtils.OpenMyMessageBox(false, "文件已被其它程序占用,请关闭占用该文件的程序",
                                                      "The file has been occupied by other programs.",
                                                      this);
                }
            }
            else
            {
                OpenWindowsUtils.OpenMyMessageBox(false, "请您选择待检测的译文文件", "Please select the translation file", this);
            }
        }
        /// <summary>
        /// 后台主要处理过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackgroundWorker_OnDoWork(object sender, DoWorkEventArgs e)
        {
            #region 初始化处理
            //清空容器中上一次的数据
            resultList.Clear();
            dicOverWidthLocation.Clear();
            IsError = false;
            //UI参数
            InputContainer container = (InputContainer)e.Argument;

            //工作簿 BUG
            IXLWorkbook  wbTrans = null;
            IXLWorksheet wsTrans = null;
            try
            {
                wbTrans = new XLWorkbook(container.FilePath);
                wsTrans = wbTrans.Worksheet(1);
            }
            catch (Exception)
            {
                OpenWindowsUtils.OpenMyMessageBox(false, "文件已被其它程序占用,请关闭占用该文件的程序", "The file has been occupied by other programs.", this);
            }
            //sheet

            #endregion

            #region 数据入队
            try
            {
                QueueUtis.JoinDataQueue(wsTrans, dataQueues, Argument.TargetColumn, threadCount);
            }
            catch (Exception)
            {
                //数据有误,结束本次检查,
                resultList.Clear();
                Dispatcher.Invoke(delegate()
                {
                    MDataGrid.ItemsSource = null;
                });
                //UI方式 非正常关闭处理
                if (Argument.FilePath == null)
                {
                    Dispatcher.Invoke(delegate()
                    {
                        OpenWindowsUtils.OpenMyMessageBox(false, "译文不正确,存在空值", "There is a null value", this);
                        IsError = true;
                    });
                    return;
                }
                //控制台 非正常关闭处理
                else
                {
                    if (Argument.FilePath != null)
                    {
                        ConsoleClose(false);
                    }
                }
            }
            #endregion

            #region 处理数据
            //显示
            HiddenOrDisplay(btnTest, false);

            //启动线程
            for (int i = 0; i < threadCount; i++)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(DataQueueThreadStart));
                thread.Start(dataQueues[i]);
            }
            #endregion

            #region  等待数据处理完毕

            AutoResetEvent.WaitAll(autoResetEvent);

            #endregion

            #region 数据显示
            //隐藏
            HiddenOrDisplay(btnTest, true);
            //排序
            SortList(resultList);
            Dispatcher.Invoke(delegate()
            {
                MDataGrid.ItemsSource = null;
                MDataGrid.ItemsSource = resultList;
            });
            #endregion

            #region 数据输出 xml、json  还有excel文件(标记超长宽度的位置)
            //1 输出路径获取
            string baseDiretory = string.Empty;
            if (Argument.OutPutDiretory != null && Directory.Exists(Argument.OutPutDiretory))
            {
                baseDiretory            = Argument.OutPutDiretory;
                Argument.OutPutDiretory = baseDiretory;
            }
            //默认文件输出路径
            else
            {
                baseDiretory = AppDomain.CurrentDomain.BaseDirectory;
            }

            //2 输出
            if (Argument.OutPutType != null)
            {
                try
                {
                    OutPutOperator.GetJsonOrXmlResult(resultList, baseDiretory, Argument.OutPutType);
                }
                catch (Exception)
                {
                    if (Argument.FilePath != null)
                    {
                        //文件输出出错
                        Console.WriteLine((int)StatusCode.code.WriteError);
                    }
                    else
                    {
                        OpenWindowsUtils.OpenMyMessageBox(false, "文件输出出错", "File output error", this);
                    }
                }
            }
            //若输出类型未指定 默认文件类型json
            else
            {
                OutPutOperator.OutPutToJSON(resultList, Path.Combine(baseDiretory, "result.json"));
            }

            //输出excel
            var t1 = dicOverWidthLocation;
            PaintCellColorOfDic(dicOverWidthLocation, wsTrans, XLColor.PastelRed);
            wbTrans.SaveAs(Path.Combine(baseDiretory, "输出结果.xlsx"));
            #endregion

            #region 控制台正常关闭处理
            if (Argument.FilePath != null)
            {
                ConsoleClose(true);
            }
            #endregion
        }