C\#빅 데이터 내 보 내기 워드 의 가사 오류 처리 방법

최근 프로젝트 는 winform 기반 보고 시스템 으로 일련의 조회 매개 변수 에 따라 결 과 를 계산 하여 최종 적 으로 형식 이 규범 화 된 워드 문 서 를 생 성 합 니 다.처음에 데이터 양 이 많 지 않 았 고(500 줄)데이터 이내 에 기록 속 도 는 받 아들 일 수 있 었 습 니 다.그러나 최근 에 대량의 데이터 줄 이 생 겼 을 때 워드 를 쓰 는 과정 이 매우 느 려 졌 습 니 다.CPU 는 100%까지 끌 어 올 렸 습 니 다.본인 의 기계 설정 은 이미 비교적 높 은 편 입 니 다.8G 메모리+i5CPU 는 여전히 가사 입 니 다.이 문 제 는 저 를 며칠 동안 괴 롭 혔 고 구 글 에 여러 번 물 었 습 니 다.기본적으로 제 시 된 답 은 워드 자체 가 비교적 느 린 것 입 니 다.또는 비 위탁 관리 코드 의 최적화,느 린 것 도 괜 찮 습 니 다.적어도 진도 표 를 통 해 상호작용 을 할 수 있 습 니 다.가사 에서 오 류 를 보고 할 때 까지.이 건 절대 용납 안 돼.여러 가지 방법 을 시 도 했 습 니 다.이 는 비 위탁 관리 코드 를 강제로 회수 하고 다 중 스 레 드 등 방식 을 포함 하여 모두 실 패 했 습 니 다.물론 황 천 은 마음 이 있 는 사람 을 저 버 리 지 않 고 결국은 문 제 를 해결 한 것 입 니 다.제 데 이 터 량 이 그리 크 지 않 습 니 다.4000 여 줄 을 표 에 기록 하고 쓸데없는 말 을 하지 않 으 며 코드 를 직접 붙 입 니 다.
일반적인 워드 표를 쓰 는 방법:

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 컨트롤 을 통 해 워드 템 플 릿 을 불 러 옵 니 다.한 줄 한 줄 워드 표 에 데 이 터 를 기록 합 니 다.새로운 표 는 책 갈 피 를 통 해 복사 한 후에 붙 여 넣 은 다음 에 한 줄 한 줄 씩 기록 합 니 다.데이터 양 이 500 줄 이내 일 때 속 도 는 받 아들 일 수 있 습 니 다.데이터 양 이 커지 면 느 립 니 다.
개 선 된 코드 는 주로 페이지 를 나 누 는 사상 을 사 용 했 습 니 다.집합 이나 datatable 을 페이지 별로 나 누고 페이지 당 50 줄 의 데 이 터 를 처리 한 후에 임시 파일 에 저장 하고 워드 자원 을 방출 한 다음 임시 파일 을 열 었 습 니 다.이 어 지난번 기록 위치 에 계속 기록 합 니 다.워드 자원 을 방출 하 는 것 은 데 이 터 를 너무 많이 쓰 는 가짜 사망 신고 의 문 제 를 해결 하기 위해 서 입 니 다.워드 를 처리 하 는 친구 에 게 도움 이 되 었 으 면 좋 겠 습 니 다.

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);
            }
        }

좋은 웹페이지 즐겨찾기