最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 大數據導出word的假死報錯的處理方法

 更新時間:2013年03月11日 17:25:46   作者:  
C# 大數據導出word的假死報錯的處理方法,需要的朋友可以參考一下

最近一個項目是一個基于winform的報告系統(tǒng),根據一系列的查詢參數計算出結果,最終生成一個格式規(guī)范的word文檔,剛開始數據量不大(500行)數據以內,寫入速度還能接受,但最近遇到一個問題就是當出現大量的數據行的時候,寫入word的過程就變的非常慢,CPU直接拉到100%,本人機器配置已經算比較高的了,8G內存+i5CPU,依舊假死,該問題困擾了我?guī)滋?,也問了google很多次,基本上給出的答案都是word本身就比較慢這樣一類的答案,或者是非托管代碼的優(yōu)化,慢也就算了,至少可以通過進度條來交互,假死到報錯,這個絕對是零容忍的。嘗試了很多種方法,包括將非托管代碼強制進行回收,多線程等方式,最終都失敗了,當然,黃天不負有心人最終總算是解決了問題了,我的數據量不算特別巨大,就4000多行寫入表中,廢話少說,直接貼代碼:

常規(guī)寫入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表格中寫入數據,新的表格通過書簽復制后粘貼,然后繼續(xù)一行一行的寫入,數據量在500行以內的時候,速度還是可以接受的,當數據量增大之后,就很慢了

改進后的代碼,主要用到了分頁的思想,將集合或者datatable進行分頁,每頁處理50行數據,處理完了之后保存到臨時文件,釋放掉word資源,再打開臨時文件,接著上次的寫入位置繼續(xù)寫入。釋放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);

                //獲得分組,只有分組了的才統(tǒng)計,默認已經分好組,組可以調整
                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);
                                    }
                                    //添加行的同時填充單元格的值,減少一次全循環(huán)
                                    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);
            }
        }

相關文章

  • C#3.0中Lambda表達式詳解

    C#3.0中Lambda表達式詳解

    在C#3.0中,微軟給我?guī)淼囊恍┬绿匦钥赡苁且郧八虚_發(fā)語言都沒有的特性。這無疑大大的體現了C#3.0在開發(fā)語言中強大的優(yōu)勢
    2013-09-09
  • C#實現的鼠標鉤子

    C#實現的鼠標鉤子

    本文給大家分享的是使用C#實現鼠標鉤子功能,程序已能獲取鼠標坐標,其他就沒別的功能了,有需要的小伙伴參考下吧。
    2015-03-03
  • C#泛型方法在lua中表示的一種設計詳解

    C#泛型方法在lua中表示的一種設計詳解

    這篇文章主要給大家介紹了關于C#泛型方法在lua中表示的一種設計的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03
  • 深入理解C#中new、override、virtual關鍵字的區(qū)別

    深入理解C#中new、override、virtual關鍵字的區(qū)別

    下面小編就為大家?guī)硪黄钊肜斫釩#中new、override、virtual關鍵字的區(qū)別。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • C#實現條形碼識別的解決方案分享

    C#實現條形碼識別的解決方案分享

    主流的識別庫主要有ZXing.NET和ZBar,OpenCV 4.0后加入了QR碼檢測和解碼功能,所以本文主要和大家分享了使用ZBar進行條形碼識別的示例代碼,需要的可以參考一下
    2023-07-07
  • Unity 使用TexturePacker打包圖集的操作方法

    Unity 使用TexturePacker打包圖集的操作方法

    這篇文章主要介紹了Unity 使用TexturePacker打包圖集的操作方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • C#簡單獲取時間差的小例子

    C#簡單獲取時間差的小例子

    C#簡單獲取時間差的小例子,需要的朋友可以參考一下
    2013-04-04
  • C#三種方法獲取文件的Content-Type(MIME Type)

    C#三種方法獲取文件的Content-Type(MIME Type)

    這篇文章介紹了C#獲取文件Content-Type(MIME Type)的三種方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • 利用WPF窗口程序設計簡單計算器

    利用WPF窗口程序設計簡單計算器

    這篇文章主要為大家詳細介紹了利用WPF窗口程序設計簡單計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 基于C#中XmlReader讀取Xml的深入分析

    基于C#中XmlReader讀取Xml的深入分析

    本篇文章是對C#中XmlReader讀取Xml進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05

最新評論

开阳县| 千阳县| 太和县| 富川| 宜黄县| 大埔县| 双城市| 宜春市| 平度市| 荥经县| 湘潭县| 西峡县| 巫溪县| 高邑县| 滦平县| 兴和县| 凉山| 凤翔县| 嘉鱼县| 铜鼓县| 岢岚县| 巧家县| 高阳县| 玛多县| 长岛县| 山东| 鲁甸县| 南城县| 涿州市| 小金县| 萝北县| 岳池县| 调兵山市| 锡林浩特市| 专栏| 忻城县| 周宁县| 个旧市| 五原县| 浏阳市| 雷山县|