C# 大数据导出word的假死报错的处理方法

最近一个项目是一个基于winform的报告系统,根据一系列的查询参数计算出结果,最终生成一个格式规范的word文档,刚开始数据量不大(500行)数据以内,写入速度还能接受,但最近遇到一个问题就是当出现大量的数据行的时候,写入word的过程就变的非常慢,CPU直接拉到100%,本人机器配置已经算比较高的了,8G内存+i5CPU,依旧假死,该问题困扰了我几天,也问了google很多次,基本上给出的答案都是word本身就比较慢这样一类的答案,或者是非托管代码的优化,慢也就算了,至少可以通过进度条来交互,假死到报错,这个绝对是零容忍的。尝试了很多种方法,包括将非托管代码强制进行回收,多线程等方式,最终都失败了,当然,黄天不负有心人最终总算是解决了问题了,我的数据量不算特别巨大,就4000多行写入表中,废话少说,直接贴代码:

常规写入word表格的方法:


代码如下:

private void LoadSectionWord()
        {
           //临时目录
            string filePath = Tools.CreateWordFileDir() + Utils.GetTimeString() + ".doc";
          //将二进制文件写入到word文件
            sectionWordTools.ByteConvertWord(sectionWordTools.WordContentByTemplateIDAndTemplateTitle(templateId, sectionTitle), filePath);
            try
            {
              //wdc为winWordControl控件的实例
                wdC.LoadDocument(filePath);
                Microsoft.Office.Interop.Word.Document wd = (Microsoft.Office.Interop.Word.Document)wdC.document;
                Microsoft.Office.Interop.Word.Application wa = wd.Application;//
               //需要写入Word的集合
                if (dicList.Count > 0)
                {
                    dicList = dicList.OrderBy(d => d.Key.AnalyteID).ToDictionary(i => i.Key, ii => ii.Value);
                    sectionWordTools.GotoBookMark(wa, "RepeatAnalysisResult");
                    wa.Selection.Copy();//复制模板第一个table
                    sectionWordTools.WordReplace("special matrix", wdg.Species.ToLower().Trim() + " " + wdg.Matrix.ToLower().Trim(), true, wd);
                    string analyteTitles = string.Empty;
                    int index = 0;

#region Replace Title
                    foreach (KeyValuePair<AnalyteReNameEntity, DataTable> d in dicList)
                    {

AnalyteReNameEntity key = d.Key;
                        if (dicList.Count > 2)
                        {
                            if (index.Equals(dicList.Count - 2))
                            {
                                analyteTitles += key.NewAnalyteName + " and ";
                            }
                            else
                            {
                                analyteTitles += key.NewAnalyteName + " , ";
                            }
                        }
                        else if (dicList.Count == 2)
                        {
                            analyteTitles += key.NewAnalyteName + " and ";
                        }
                        else
                        {
                            analyteTitles += key.NewAnalyteName;
                        }
                        index++;
                    }
                    analyteTitles = analyteTitles.Trim().TrimEnd('d').TrimEnd('n').TrimEnd('a').Trim().Trim(',');
                    sectionWordTools.WordReplace("for Abc000", "for " + analyteTitles, true, wd);
                    #endregion

int wordTableCount = 0;
                    foreach (KeyValuePair<AnalyteReNameEntity, DataTable> d in dicList)
                    {
                        AnalyteReNameEntity key = d.Key;
                        DataView dv = d.Value.DefaultView;
                        dv.Sort = "Custom ID";
                        DataTable dt = dv.ToTable();
                        #region 处理dt
                        if (dt.Columns["analyteid"] != null)
                        {
                            dt.Columns.Remove("analyteid");
                        } if (dt.Columns["id"] != null)
                        {
                            dt.Columns.Remove("id");
                        }
                        if (dt.Columns["reportid"] != null)
                        {
                            dt.Columns.Remove("reportid");
                        }
                        if (dt.Columns["studyid"] != null)
                        {
                            dt.Columns.Remove("studyid");
                        }
                        if (dt.Columns["analyteid"] != null)
                        {
                            dt.Columns.Remove("analyteid");
                        }
                        #endregion
                        //第一个WordTable
                        Microsoft.Office.Interop.Word.Table tb = wd.Tables[wd.Tables.Count];

#region 填充值
                        if (dt.Rows.Count > 0)
                        {
                            object beforerow = tb.Rows[2];
                            //表头
                            for (int i = 1; i <= dt.Columns.Count; i++)
                            {
                                tb.Cell(1, i).Range.Text = dt.Columns[i - 1].ColumnName;
                            }
                            for (int k = 1; k <= dt.Rows.Count; k++)
                            {
                                //模板上默认有2行了,添加行数
                                if (k <= dt.Rows.Count - 2)
                                {
                                    tb.Rows.Add(ref beforerow);
                                }
                                for (int i = 1; i <= dt.Columns.Count; i++)
                                {
                                    tb.Cell(k + 1, i).Range.Text = dt.Rows[k - 1][dt.Columns[i - 1].ColumnName].ToString();
                                }
                            }
                        }
                        #endregion

sectionWordTools.WordReplace("Abc000", key.NewAnalyteName, true, wd);

#region 处理备注
                        string notStr = GetCurrentReassayReason(key.AnalyteID);
                        //notStr = "Reasons for Reassay:\r1). Confirmation Assay\r2). ALQ\rReasons for Reported Conc.:\r1). The only valid result is reported.\r20. Reassay results selected according to procedure defined in study protocol";

sectionWordTools.WordReplace("Repeat analysis Tipis", notStr, true, wd);
                        #endregion
                        //根据内容调整表格
                        //tb.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent);
                        //根据窗口调整表格
                        //tb.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitWindow);
                        if (wordTableCount < dicList.Count - 1)
                        {
                            // wa.Selection.TypeParagraph();//回车
                            wd.Paragraphs.Last.Range.Select();
                            wa.Selection.Paste();
                        }
                        wordTableCount++;

#region 处理整体格式
                        object lefttopstr = tb.Cell(2, 1).Range.Start;
                        object leftbottomend = tb.Cell(tb.Rows.Count, tb.Columns.Count).Range.End;
                        wd.Range(ref lefttopstr, ref leftbottomend).Select();
                        sectionWordTools.AddBorderNoneLineStyle(wa, false, false, true, true, true, true, true, true);
                        #endregion

}

}

}
            catch (Exception ex)
            {
                Tools.RecordErrorList(ex.Message, ex);
            }
        }

上面的代码就是通过winwordControl控件加载word模板,一行一行向word表格中写入数据,新的表格通过书签复制后粘贴,然后继续一行一行的写入,数据量在500行以内的时候,速度还是可以接受的,当数据量增大之后,就很慢了

改进后的代码,主要用到了分页的思想,将集合或者datatable进行分页,每页处理50行数据,处理完了之后保存到临时文件,释放掉word资源,再打开临时文件,接着上次的写入位置继续写入。释放word资源就是为了解决写入数据量过大假死报错的问题。希望对处理word的朋友有帮助

代码如下:

private void LoadSectionWord()
        {
            string filePath = Tools.CreateWordFileDir() + Utils.GetTimeString() + ".doc";
            //读取模板
            sectionWordTools.ByteConvertWord(sectionWordTools.WordContentByTemplateIDAndTemplateTitle(templateId, sectionTitle), filePath);
            try
            {
                wdC.LoadDocument(filePath);
                Microsoft.Office.Interop.Word.Document wd = (Microsoft.Office.Interop.Word.Document)wdC.document;
                Microsoft.Office.Interop.Word.Application wa = wd.Application;
                sectionWordTools.GotoBookMark(wa, "TimeConcentrationData");
                wa.Selection.Copy();//复制模板第一个table
                string assayLLOQ = ComputerOfPostText.ComputerPostText.GetStudyAssayLLOQResults(this.studyId);
                sectionWordTools.WordReplace("0.0 ng/mL for Abc000", assayLLOQ, true, wd);

//获得分组,只有分组了的才统计,默认已经分好组,组可以调整
                List<string> groupList = commonBLL.GetAnalyteGroupList(this.reportId, COMMON_TYPE);
                List<List<AnalyteReNameEntity>> analyteGroupList = new List<List<AnalyteReNameEntity>>();

#region 确定Table分组的数量
                foreach (string group in groupList)
                {
                    List<AnalyteReNameEntity> currentGroupList = commonBLL.GetAnalyteReNameList(this.reportId, this.studyId, group);
                    if (currentGroupList.Count > 0)
                    {
                        analyteGroupList.Add(currentGroupList);
                    }
                }
                #endregion

if (analyteGroupList.Count > 0)
                {
                    int wordTableCount = 0;
                    foreach (List<AnalyteReNameEntity> currentGroupList in analyteGroupList)
                    {
                        //第一个WordTable
                        Microsoft.Office.Interop.Word.Table tb = wd.Tables[wd.Tables.Count];
                        string key = globalBLL.AnalyteAppendString(currentGroupList);
                        //需要写入word的结果集
                        DataTable dt = new DataTable();

#region  构造word中的列头
                        foreach (ReportColumnsEntity rc in rceList)
                        {
                            dt.Columns.Add(rc.ReportText, typeof(string));
                        }

foreach (AnalyteReNameEntity ar in currentGroupList)
                        {
                            dt.Columns.Add(ar.NewAnalyteName + " Concentration (ng/mL)", typeof(string));
                        }
                        dt.Columns.Add("Comments", typeof(string));
                        #endregion

#region 构造Word中的行的DataTable
                        for (int i = 0; i < currentGroupList.Count; i++)
                        {
                            DataRow[] rows = dtResult.Select(string.Format(" analyteid={0}", currentGroupList[i].AnalyteID));
                            if (i == 0)
                            {
                                #region i=0,填充包括列头
                                for (int k = 0; k < rows.Length; k++)
                                {
                                    string conc = Utils.EffectiveNumber(rows[k]["concentration"].ToString(), "3");
                                    DataRow dr = dt.NewRow();
                                    foreach (ReportColumnsEntity rc in rceList)
                                    {
                                        if (rc.WatsonText.Equals("concentration"))
                                        {
                                            dr[rc.ReportText] = Utils.EffectiveNumber(rows[k][rc.WatsonText].ToString(), "3");
                                        }
                                        else
                                        {
                                            dr[rc.ReportText] = rows[k][rc.WatsonText].ToString();
                                        }

}
                                    dr["Comments"] = "NA";
                                    dr[currentGroupList[i].NewAnalyteName + " Concentration (ng/mL)"] = conc;

dt.Rows.Add(dr);
                                }
                                #endregion
                            }
                            else
                            {
                                for (int k = 0; k < rows.Length; k++)
                                {
                                    string conc = Utils.EffectiveNumber(rows[k]["concentration"].ToString(), "3");
                                    //对分析物浓度列重新赋值
                                    dt.Rows[k][currentGroupList[i].NewAnalyteName + " Concentration (ng/mL)"] = conc;
                                }
                            }

}
                        DataTable dtTemp = dt.Copy();
                        DataView dv = dt.DefaultView;
                        dv.Sort = "Subject ID";
                        dtTemp = dv.ToTable();
                        dt = dtTemp;
                        #endregion

#region 填充值
                        if (dt.Rows.Count > 0)
                        {

#region 根据列合并拆分单元格
                            object wordColumnsCount = dt.Columns.Count;
                            object rownum = 1;
                            for (int k = 0; k < 5; k++)
                            {
                                for (int i = 1; i < tb.Columns.Count; i++)
                                {

tb.Cell(k, i).Range.Text = " ";
                                }
                            }
                            //先合并,再拆分  Selection.Cells.Merge
                            for (int i = 1; i <= 4; i++)
                            {
                                object start = tb.Cell(i, 1).Range.Start;
                                object end = tb.Cell(i, 6).Range.End;
                                wd.Range(ref start, ref end).Select();
                                wa.Selection.Cells.Merge();
                            }
                            for (int i = 1; i < 5; i++)
                            {
                                tb.Cell(i, 1).Split(ref rownum, ref wordColumnsCount);
                            }
                            #endregion

//关闭屏幕更新
                            wa.ScreenUpdating = false;
                            #region 表头的填充
                            //表头填充
                            for (int i = 1; i <= dt.Columns.Count; i++)
                            {
                                tb.Cell(1, i).Range.Text = dt.Columns[i - 1].ColumnName;
                            }
                            #endregion

#region 填充值

#region 分页
                            object missing = System.Reflection.Missing.Value;
                            object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; //解决normal.dot问题
                            object beforerow = tb.Rows[3];
                            int dtRowCounts = dt.Rows.Count;//10;// 
                            int columnCount = dt.Columns.Count;
                            int pageSize = 50;
                            //分页的总页数
                            int totalPages = DataUtils.GetPageCounts(dtRowCounts, pageSize);
                            for (int index = 1; index <= totalPages; index++)
                            {
                                tb = wd.Tables[wd.Tables.Count];
                                beforerow = tb.Rows[3 + (index - 1) * pageSize];
                                DataTable pageDt = DataUtils.GetPagedTable(dt, index, pageSize);

#region 添加行和数据
                                for (int k = 2; k < pageDt.Rows.Count + 2; k++)
                                {

//模板上已经有三行了,最后一页少添加3行
                                    if (index.Equals(totalPages))
                                    {
                                        if (k < pageDt.Rows.Count - 3)
                                        {
                                            tb.Rows.Add(ref beforerow);
                                        }

}
                                    else
                                    {
                                        tb.Rows.Add(ref beforerow);
                                    }
                                    //添加行的同时填充单元格的值,减少一次全循环
                                    for (int i = 1; i <= pageDt.Columns.Count; i++)
                                    {
                                        tb.Cell(k + (index - 1) * pageSize, i).Range.Text = pageDt.Rows[k - 2][pageDt.Columns[i - 1].ColumnName].ToString();
                                    }
                                }
                                #endregion
                                //填充完PageSize条数据,先保存再重新加载填充
                                object savePath = filePath;
                                wd.SaveAs(ref   savePath, ref   missing,
                ref   missing, ref   missing, ref   missing, ref   missing,
                ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                                wa.ScreenUpdating = true;
                                wd = null;
                                wa = null;
                                Thread.Sleep(10);
                                //重新加载
                                wdC.LoadDocument(filePath);
                                wd = (Microsoft.Office.Interop.Word.Document)wdC.document;
                                wa = wd.Application;
                                wa.ScreenUpdating = false;
                            }
                            #endregion

#endregion
                            //打开屏幕更新
                            //wa.ActiveDocument.ActiveWindow.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMaximize;
                            wa.ScreenUpdating = true;
                        }

#endregion

#region Paste Table
                        sectionWordTools.WordReplace("Abc000", key, true, wd);
                        tb = wd.Tables[wd.Tables.Count];
                        //根据内容调整表格
                        tb.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent);
                        //根据窗口调整表格
                        tb.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitWindow);
                        if (wordTableCount < analyteGroupList.Count - 1)
                        {
                            // wa.Selection.TypeParagraph();//回车
                            wd.Paragraphs.Last.Range.Select();
                            wa.Selection.Paste();
                        }
                        wordTableCount++;
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                Tools.RecordErrorList(ex.Message, ex);
            }
        }

(0)

相关推荐

  • VS2015使用scanf报错的解决方法

    本文实例为大家分享了VS2015使用scanf报错的解决方法,供大家参考,具体内容如下 1.在程序最前面加: #define_CRT_SECURE_NO_DEPRECATE 2.在程序最前面加: #pragma warning(disable:4996) 3.把scanf改为scanf_s: 4.无需在程序最前面加那行代码,只需在新建项目时取消勾选"SDL检查"即可: 5.若项目已建立好,在项目属性里关闭SDL也行:我用的VS是中文版的 (1).我将"是"改为&qu

  • jQuery在vs2008及js文件中的无智能提示的解决方法

    jQuery在vs2008中的智能提示 1  安装VS2008SP1补丁 要确保您的vs2008已经打了sp1补丁,在vs2008的帮助里的关于,要是安装了sp1,会出现"版本 3.5 sp1",没安装当然就只有"版本 3.5". 如果没有安装可以在这下载sp1. 2  安装VS 2008 Patch KB958502以支持"-vsdoc.js"Intellisense文件.该补丁会导致Visual Studio在一个JavaScript库被引用

  • 解决VS2015中没有报表项(ReportViewer)的方法

    没有报表,一般默认安装之后会出现这种情况,在安装的时候选择自定义安装,把Microsoft Office 开发人员工具.Microsoft SQL Server Data Tools勾选上,安装之后就可以找到了. 1.点击vs_entprise安装 2.安装过后的点击修改 3.额外勾选Microsoft Office 开发人员工具.Microsoft SQL Server Data Tools 两项.如下图 4.点击安装,等待安装之后就可以看到报表啦(因为我的有内容没有更新,所以显示的是更新,一

  • VS2010写的程序在自己电脑可以运行、其他电脑上不能运行的解决方案

    之后在网上搜得说是改为使用mfc静态链接,我也相应的改了,然后生成的release,可是还是在其他人电脑上没有反应. 无奈,只好在网上继续搜索结果,最后定在是不是.net framework版本的问题上,网上说改为2.0版本可以解决,然后我就打开项目属性,悲剧的发现目标Framework 直接显示的4.0,连下拉条都没有...而且我的通用属性的配置及平台选项均是不适用状态...我又新建一mfc工程,在新建菜单时上方有可以选择的.net 版本,我选择了2.0版本,但是新建之后打开属性它还是显示的4

  • c#使用process.start启动程序报错解决方法

    出错信息: 复制代码 代码如下: Unknown error (0xffffffff)at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)at System.Diagnostics.Process.Start()at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)at System.Diagnostics.Proc

  • vs2010无法打开项目文件的原因分析及解决方法

    使用 Visual Studio 2010 SP1 打开项目文件的时候出现以下提示信息: 报错的原因是 vs2010需要把mvc版本升到3,默认的vs2010的mvc是2 vs2010 mvc3下载地址:http://www.microsoft.com/web/handlers/webpi.ashx?command=getinstallerredirect&appid=MVC3

  • 卸载VS2011 Developer Preview后Sql Server2008 R2建立数据库关系图报“找不到指定的模块”错误的解决方法

    可怜我的C盘本来只有8.XG,所以不得不卸载掉它. 卸载掉本身没啥问题,只是昨晚突然发现 Sql Server 2008 R2 Management Studio 突然不能新建数据库关系图了,一建立就会报"找不到指定的模块(MS Visual Database Tools)"的错误: 为了这个错误我甚至认真评估了重新配置数据库的可能性.无奈这个R2的SQL2008打过SP1了,无论是修复还是重装都是很花时间的事情,所以不甘心,在Google上找了一圈.终于找到个类似的主题反应了这个问题

  • VS2008无法直接查看STL值的解决方法

    安装完visual studio之后你可能的调试时看到的STL的内容是下面这样的: 而你更希望看到下面这样的调试显示: 那么,如何让vs2008显示成下面的样子呢?其实就说一项配置的问题. 把下面蓝色的一项"在变量窗口中显示对象的原始结构" 取消 勾选. 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们.

  • VS2015在升级到Update2之后运行Cordova项目异常的解决方案

    VS2015在升级到Update2之后,有可能出现如下异常,在运行Cordova项目时提示: 查看输出面板会有乱码错误信息: 出现此问题的原因是在于npm程序损坏了.vs调用的npm程序并不是在node安装目录下的npm,而是在: C:\Users\用户名\AppData\Roaming\Microsoft\VisualStudio\MDA\vs-npm\版本号\ 升级完VS之后会发现npm.cmd已经无法启动运行了.所以Cordova项目在运行时,无法使用npm来解析包导致程序无法运行. 解决

  • IE8引发 VS2005/2008 MFC向导出错的解决方案

    第一时间测试成功.老外给出的解决方案,测试有效,大家试试. 请按照以下步骤做: - 启动注册表编辑器 regedit (在64位系统上也可以启动32位版本) - 找到"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones", 建立一个名为1000的新键(如果没有的话). - 在这个1000的键下,建立如下DWORD内容: 复制代码 代码如下: Name = 1207 Typ

随机推荐