找回密码
 立即注册

QQ登录

只需一步,快速开始

远光软件

初级会员

47

主题

119

帖子

378

积分

初级会员

积分
378

活字格认证

远光软件
初级会员   /  发表于:2015-5-13 11:03  /   查看:5602  /  回复:7
现在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;
                }

7 个回复

倒序浏览
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);

会计报告附注2014(一般企业).doc

1.69 MB, 下载次数: 168

回复 使用道具 举报
iceman
社区贡献组   /  发表于:2015-5-18 17:19:00
地板
回复 3楼远光软件的帖子

抱歉,您提供的 word 文件我们无法打开,请问您的 word 文档的office版本是什么?
Untitled.png
回复 使用道具 举报
远光软件
初级会员   /  发表于:2015-5-19 14:19:00
5#
这个是用StreamType.InternalUnicodeFormat 内部版本格式导出,直接用控件load进来不行吗?
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2015-5-20 18:03:00
6#
回复 5楼远光软件的帖子

可以正常加载了,问题已经重现,我会和厂商沟通看是否有优化的可能。
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2015-5-21 12:26:00
7#
回复 5楼远光软件的帖子

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

你可以通过附件 Demo 测试看下效果。我已经把当前进展反馈给厂商。
  1. private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             string filename=System.IO.Path.Combine(Application.StartupPath,&quot;..\\..\\会计报告附注2014(一般企业).doc&quot;);
  4.             this.wordControl.Load(filename, TXTextControl.StreamType.InternalUnicodeFormat);
  5.             Stopwatch st = new Stopwatch();
  6.             st.Start();
  7.             Color backColor = wordControl.BackColor;
  8.             foreach (ApplicationField appField in wordControl.ApplicationFields)         // 去掉取数公式的背景色
  9.             {
  10.                 SetApplicationFieldForeColor(appField, backColor);
  11.             }
  12.             st.Stop();
  13.             MessageBox.Show(st.ElapsedMilliseconds.ToString());
  14.             int tablecount = 0;
  15.             int cellcount = 0;
  16.             st.Reset();
  17.             st.Start();
  18.             this.wordControl.SuspendLayout();
  19.             foreach (Table table in wordControl.Tables)         // 去掉表格中取数公式的背景色
  20.             {
  21.                 tablecount++;
  22.                 foreach (TableCell cell in table.Cells)
  23.                 {
  24.                     cellcount++;
  25.                     cell.CellFormat.BackColor = backColor;
  26.                 }
  27.             }
  28.             this.wordControl.ResumeLayout();
  29.             st.Stop();
  30.             MessageBox.Show(st.ElapsedMilliseconds.ToString());
  31.         }
复制代码
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2015-5-29 09:10:00
8#
回复 6楼iceman的帖子

为了给你提供更优质的服务,请对本次服务进行评分。我们会认真对待你提出的宝贵意见,谢谢   
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部