远光软件 发表于 2015-5-13 11:03:00

表格单元格,applicationFeild 背景色替换,性能问题

现在200-600页文档,是面包含了很多带背景颜色的表格单元格和applicationFeild,现在需要把所有颜色替换为文档的的背景色,现在的实现性能较低,96页要1分多钟,有没有更好的方式?
主要代码:      
       Color backColor = wordControl.BackColor;
          foreach (ApplicationField appField in tempWordControl.ApplicationFields)         // 去掉取数公式的背景色
            {
                tempWordControl.SetApplicationFieldBackColor(appField, backColor);
            }

            foreach (Table table in tempWordControl.Tables)         // 去掉表格中取数公式的背景色
            {
                foreach (TableCell cell in table.Cells)
                {
                  cell.CellFormat.BackColor = backColor;
                }
            }
-------------------------------------------------------------------------

      /// <summary>
      /// 设置ApplicationField的前景颜色.
      /// </summary>
      /// <param name="field">字段.</param>
      /// <param name="color">颜色.</param>
      public void SetApplicationFieldForeColor(ApplicationField field, Color color)
      {
            if (field != null)
            {
                Selection backSelection = this.Selection;
                try
                {
                  int start = field.Start - 1;
                  int length = field.Length;
                  this.Selection = new Selection(start, length);
                  this.Selection.ForeColor = color;
                }
                finally
                {
                  this.Selection = backSelection;
                }

iceman 发表于 2015-5-13 18:50:00

回复 1楼远光软件的帖子

鉴于您提到的文档格式可能比较复杂,能否把这个模板保存成 tx 文件上传到论坛,我这边通过 1# 代码调试用于重现问题?谢谢

远光软件 发表于 2015-5-18 14:33:00

导出方式:
TXTextControl.SaveSettings SaveSettings = new TXTextControl.SaveSettings();
this.wordControl.Save(saveFileDialog.FileName, StreamType.InternalUnicodeFormat, SaveSettings);

iceman 发表于 2015-5-18 17:19:00

回复 3楼远光软件的帖子

抱歉,您提供的 word 文件我们无法打开,请问您的 word 文档的office版本是什么?

远光软件 发表于 2015-5-19 14:19:00

这个是用StreamType.InternalUnicodeFormat 内部版本格式导出,直接用控件load进来不行吗?

iceman 发表于 2015-5-20 18:03:00

回复 5楼远光软件的帖子

可以正常加载了,问题已经重现,我会和厂商沟通看是否有优化的可能。

iceman 发表于 2015-5-21 12:26:00

回复 5楼远光软件的帖子

从测试情况看,并非是AppField 设置样式耗费时间,时间主要还是耗费在 cell 的背景色设置上。

你可以通过附件 Demo 测试看下效果。我已经把当前进展反馈给厂商。


private void Form1_Load(object sender, EventArgs e)
      {
            string filename=System.IO.Path.Combine(Application.StartupPath,&quot;..\\..\\会计报告附注2014(一般企业).doc&quot;);
            this.wordControl.Load(filename, TXTextControl.StreamType.InternalUnicodeFormat);

            Stopwatch st = new Stopwatch();
            st.Start();
            Color backColor = wordControl.BackColor;
            foreach (ApplicationField appField in wordControl.ApplicationFields)         // 去掉取数公式的背景色
            {
                SetApplicationFieldForeColor(appField, backColor);
            }
            st.Stop();
            MessageBox.Show(st.ElapsedMilliseconds.ToString());


            int tablecount = 0;
            int cellcount = 0;

            st.Reset();
            st.Start();
            this.wordControl.SuspendLayout();
            foreach (Table table in wordControl.Tables)         // 去掉表格中取数公式的背景色
            {
                tablecount++;
                foreach (TableCell cell in table.Cells)
                {
                  cellcount++;
                  cell.CellFormat.BackColor = backColor;
                }
            }
            this.wordControl.ResumeLayout();
            st.Stop();
            MessageBox.Show(st.ElapsedMilliseconds.ToString());
      }

iceman 发表于 2015-5-29 09:10:00

回复 6楼iceman的帖子

为了给你提供更优质的服务,请对本次服务进行评分。我们会认真对待你提出的宝贵意见,谢谢   
http://gcdn.gcpowertools.com.cn/attachment.aspx?attachmentid=10062
页: [1]
查看完整版本: 表格单元格,applicationFeild 背景色替换,性能问题