您的位置:首页 > 大数据

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

2013-03-11 17:25 1666 查看
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);            }        }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  大数据 导出 word