Esempio n. 1
0
        private void Preview(bool write)
        {
            var progressDialog = new ProgressDialog(false);

            progressDialog.Show();
            Previewer.Document = null;
            try
            {
                Task.Run(() =>
                {
                    var xpsFilePath   = Environment.CurrentDirectory + $"\\{OfficeToXps.TempNum++}.xps";
                    var excelFilePath = (Properties.Settings.Default.TamplatePath == "") ?
                                        Environment.CurrentDirectory + "\\库存模板.xlsx" : Properties.Settings.Default.TamplatePath;
                    var tempFilePath = Environment.CurrentDirectory + "\\temp.xlsx";
                    File.Copy(excelFilePath, Environment.CurrentDirectory + "\\temp.xlsx", true);
                    if (write)
                    {
                        WriteWarehouseToExcel(tempFilePath);
                    }
                    var convertResults = OfficeToXps.ConvertToXps(tempFilePath, ref xpsFilePath);
                    switch (convertResults.Result)
                    {
                    case ConversionResult.OK:
                        xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(xpsFilePath, FileAccess.ReadWrite);
                        Dispatcher.Invoke(() =>
                        {
                            Previewer.Document = xpsDoc.GetFixedDocumentSequence();
                        });

                        break;

                    default:
                        throw new Exception();
                    }
                });
            }
            catch
            {
                new InfoDialog("请安装Microsoft Office!", false).Show();
            }
            finally
            {
                progressDialog.Close();
            }
        }
        private void dgList_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            #region 加载基本信息
            rulefile = new RuleFile();
            rulefile = dgList.SelectedItem as RuleFile;
            if (rulefile == null)
            {
                return;        //防止单击空白或标题等触发该事件
            }
            isAddOrUpdate = 1; //可以进行修改操作
            if (rulefile.IsChecked == true)
            {
                rulefile.IsChecked = false;
                currentFileList.Remove(rulefile);
            }
            else
            {
                rulefile.IsChecked = true;
                currentFileList.Add(rulefile);
            }
            bool checkedState = rulefile.IsChecked;

            #endregion

            #region //预览文件
            //
            string isxpsPath = FileManageHelper.GetPath() + rulefile.FILEFORM.Substring(0, rulefile.FILEFORM.LastIndexOf(".")).ToString() + ".xps";
            if (System.IO.File.Exists(isxpsPath))
            {
                //已经存xps文件,直接加载
                using (XpsDocument xpsDoc = new XpsDocument(isxpsPath, FileAccess.Read))
                {
                    var fsxps = xpsDoc.GetFixedDocumentSequence();
                    xpsDocViewr.Document = fsxps;
                }
            }
            else
            {
                //创建xps文件,启动进程耗时
                string serviceDocPathXPS = FileManageHelper.ByteConvertDocService(rulefile.FILEPATH, rulefile.FILEFORM);
                string xpsFilePath       = serviceDocPathXPS.Substring(0, serviceDocPathXPS.LastIndexOf(".")).ToString() + ".xps";
                var    convertResults    = OfficeToXps.ConvertToXps(serviceDocPathXPS, ref xpsFilePath);
                switch (convertResults.Result)
                {
                case ConversionResult.OK:
                    try
                    {
                        using (XpsDocument xpsDoc = new XpsDocument(xpsFilePath, FileAccess.Read))
                        {
                            var fsxps = xpsDoc.GetFixedDocumentSequence();
                            xpsDocViewr.Document = fsxps;
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    break;

                case ConversionResult.InvalidFilePath:
                    // 处理文件路径错误或文件不存在
                    break;

                case ConversionResult.UnexpectedError:

                    break;

                case ConversionResult.ErrorUnableToInitializeOfficeApp:
                    // Office 未安装会出现这个异常
                    break;

                case ConversionResult.ErrorUnableToOpenOfficeFile:
                    // 文件被占用会出现这个异常
                    break;

                case ConversionResult.ErrorUnableToAccessOfficeInterop:
                    // Office 未安装会出现这个异常
                    break;

                case ConversionResult.ErrorUnableToExportToXps:
                    // 微软 OFFICE Save As PDF 或 XPS  插件未安装异常
                    break;
                }
            }
            #endregion

            #region 判断全选和保存状态
            int ischeckCount = (dgList.ItemsSource as List <RuleFile>).Count(p => p.IsChecked == true);
            if (ischeckCount == 1 || ischeckCount == 0)
            {
                btnSave.IsEnabled = true;
                isAddOrUpdate     = 1;//可以进行修改操作
            }
            else
            {
                btnSave.IsEnabled = false;
                isAddOrUpdate     = 0;
            }
            foreach (RuleFile result in dgList.ItemsSource)
            {
                if (result.IsChecked != checkedState)
                {
                    this.chkAll.IsChecked = null;
                    return;
                }
            }
            chkAll.IsChecked = checkedState;
            #endregion
        }
        /// <summary>
        /// 附件上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click_1(object sender, RoutedEventArgs e)
        {
            // isAddOrUpdate = 0;//上传即新建
            RuleFile[] getRuleFiles = FileManageHelper.GetRuleFiles(currentRegulate.GUID);
            if (getRuleFiles != null && getRuleFiles.Count() > 0)
            {
                currentFileList = getRuleFiles.ToList();
            }
            upfilestr = "";
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.CheckFileExists = true;  //检查文件是否存在
            dlg.Multiselect     = false; //是否允许多选,false表示单选
            dlg.CheckPathExists = true;
            //只限制上传word和excel
            dlg.Filter = "Office Files|*.doc;*.docx;*.xls;*.xlsx";
            if ((bool)dlg.ShowDialog())
            {
                string filePath = dlg.FileName;
                upfilestr = dlg.FileName;
                string xpsFilePath    = filePath.Substring(0, filePath.LastIndexOf(".")).ToString() + ".xps";
                var    convertResults = OfficeToXps.ConvertToXps(filePath, ref xpsFilePath);
                switch (convertResults.Result)
                {
                case ConversionResult.OK:
                    try
                    {
                        using (XpsDocument xpsDoc = new XpsDocument(xpsFilePath, FileAccess.Read))
                        {
                            var fsxps = xpsDoc.GetFixedDocumentSequence();
                            xpsDocViewr.Document = fsxps;
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    break;

                case ConversionResult.InvalidFilePath:
                    // 处理文件路径错误或文件不存在
                    break;

                case ConversionResult.UnexpectedError:

                    break;

                case ConversionResult.ErrorUnableToInitializeOfficeApp:
                    // Office 未安装会出现这个异常
                    break;

                case ConversionResult.ErrorUnableToOpenOfficeFile:
                    // 文件被占用会出现这个异常
                    break;

                case ConversionResult.ErrorUnableToAccessOfficeInterop:
                    // Office 未安装会出现这个异常
                    break;

                case ConversionResult.ErrorUnableToExportToXps:
                    // 微软 OFFICE Save As PDF 或 XPS  插件未安装异常
                    break;
                }

                if (UpLodeEnclouseFolder(filePath) == true)
                {
                    MessageBox.Show("文件上传成功!");
                    dgList.ItemsSource = null;
                    dgList.ItemsSource = currentFileList;
                    btnSave.IsEnabled  = true;
                }
                else
                {
                    MessageBox.Show("请选择附件!");
                }
            }
        }