Word报表生成

/// <summary>
        /// 生产报表
        /// </summary>
        /// <param name="strTemplate"></param>
        public void CreateWordFile(DataRow item)
        {
            try
            {
                MultiFormatWriter multiWriter = new MultiFormatWriter();
                QrCodeEncodingOptions options = new QrCodeEncodingOptions();
                BarcodeWriter bw = new BarcodeWriter();
                bw.Format = BarcodeFormat.CODE_128;
                bw.Options = options;

                object Nothing = System.Reflection.Missing.Value;
                //定义该插入图片是否为外部链接
                object linkToFile = true;
                //定义插入图片是否随word文档一起保存
                object saveWithDocument = true;

                WordApp = new Word.Application();
                WordDoc = new Word.Document();
                WordApp.Visible = false;
                WordApp.PrintPreview = false;

                string filename = System.Windows.Forms.Application.StartupPath + \\******\\Word.dot;
                try
                {
                    WordDoc = WordApp.Documents.Add(filename);
                    WordDoc = WordApp.ActiveDocument;
                }
                catch (Exception ex)
                {
                    throw (ex);
                }

                WordDoc.Tables[1].Cell(1, 2).Range.Text = HospitalName;
                int x = 1;
                //Table2  x = 1
                WordDoc.Tables[x].Cell(1, 1).Range.Text = item["CORP_DEPT_NAME"].ToString().Trim();
                WordDoc.Tables[x].Cell(2, 1).Range.Text = item["MEMO"].ToString().Trim();
                options.Width = 200;
                options.Height = 50;
                options.Margin = 0;
                options.PureBarcode = false;
                Image BarCode = bw.Write(item["REGISTER_NO"].ToString().Trim());
                Clipboard.SetDataObject(BarCode);
                WordDoc.Tables[x].Cell(1, 3).Range.Paste();
                BarCode.Dispose();
                WordDoc.Tables[x].Cell(4, 2).Range.Text = item["PATIENT_NAME"].ToString().Trim();
                WordDoc.Tables[x].Cell(4, 4).Range.Text = item["BIRTHDAY"].ToString().Trim();
                WordDoc.Tables[x].Cell(4, 6).Range.Text = item["CHART_NO"].ToString().Trim();

                if (item["CORPORATION_NAME"].ToString().Length > 26)
                {
                    WordDoc.Tables[x].Cell(5, 2).Range.Text = item["CORPORATION_NAME"].ToString().Trim().Substring(0, 26);
                }
                else
                {
                    WordDoc.Tables[x].Cell(5, 2).Range.Text = item["CORPORATION_NAME"].ToString().Trim();
                }
                WordDoc.Tables[x].Cell(5, 4).Range.Text = item["BOOKING_DATE"].ToString().Trim();

                if (item["ADDRESS"].ToString().Length > 26)
                {
                    WordDoc.Tables[x].Cell(6, 2).Range.Text = item["ADDRESS"].ToString().Trim().Substring(0, 26);
                }
                else
                {
                    WordDoc.Tables[x].Cell(6, 2).Range.Text = item["ADDRESS"].ToString().Trim();
                }
                WordDoc.Tables[x].Cell(6, 4).Range.Text = item["PHONE_NUMBER"].ToString().Trim();

                if (item["PHOTO"].ToString() != "")
                {
                    SaveFileDialog sflg = new SaveFileDialog();
                    MemoryStream ms = new MemoryStream((byte[])item["PHOTO"]);//把照片读到MemoryStream里     
                    Image imageBlob = Image.FromStream(ms, true);//用流创建Image                        
                    imageBlob.Save(System.Windows.Forms.Application.StartupPath + "\\" + item["REGISTER_NO"].ToString() + ".jpg");
                    ////插入图片
                    WordDoc.Tables[x].Cell(4, 7).Range.Text = "";
                    WordDoc.Tables[x].Cell(4, 7).Select();
                    object range = WordApp.Selection.Range;
                    Word.InlineShape shape = WordApp.ActiveDocument.InlineShapes.AddPicture(System.Windows.Forms.Application.StartupPath + "\\" + item["REGISTER_NO"].ToString() + ".jpg", ref linkToFile, ref saveWithDocument, ref range);

                    shape.Width = 73;//图片宽度
                    shape.Height = 91;//图片高度
                    System.IO.FileInfo file = new System.IO.FileInfo(System.Windows.Forms.Application.StartupPath + "\\" + item["REGISTER_NO"].ToString() + ".jpg");
                    if (file.Exists)
                    {
                        file.Delete();
                    }
                    imageBlob.Dispose();
                }
                DataTable dtHMS_REGISTER = hmsService.QueryHmsRequestDatafor0131(item["REGISTER_NO"].ToString().Trim(), strHavePrint);
                dtHMS_REGISTER.DefaultView.Sort = "STATION_ID";

                System.Data.DataTable dttemp = new System.Data.DataTable();
                DataView tempDv = dtHMS_REGISTER.DefaultView;
                dtHMS_REGISTER = tempDv.ToTable();

                dttemp = tempDv.ToTable(true, new[] { "STATION_NAME", "FLOW_SHEET_MEMO" });

                bool blMerge = false;
                Table table = null;
                int iRow = 11;
                WordDoc.Content.Tables[x].Rows.Add(ref Nothing);
                WordDoc.Tables[x].Cell(10, 1).Range.Text = "(1)登记报到";
                for (int i = 0; i < dttemp.Rows.Count; i++, iRow++)
                {
                    string strMemo = "";
                    DataRow[] dr = dtHMS_REGISTER.Select("STATION_NAME = '" + dttemp.Rows[i]["STATION_NAME"].ToString().Trim() + "'");

                    if (dr.Length > 0)
                    {
                        //创建新行
                        if (i > 0)
                        {
                            WordDoc.Content.Tables[x].Rows.Add(ref Nothing);
                            //拆分以合并行
                            if (blMerge)
                            {
                                WordDoc.Content.Tables[x].Cell(iRow, 1).Merge(WordDoc.Tables[x].Cell(iRow, 2));
                                WordDoc.Content.Tables[x].Cell(iRow, 1).Split(1, 3);
                                WordDoc.Content.Tables[x].Cell(iRow, 1).Width = 128;
                                WordDoc.Content.Tables[x].Cell(iRow, 2).Width = 70.5f;
                                WordDoc.Content.Tables[x].Cell(iRow, 3).Width = 333;
                                blMerge = false;
                            }
                        }
                        WordDoc.Tables[x].Cell(iRow, 1).Range.Text = dr[0]["STATION_NAME"].ToString().Trim();
                        int iNewRow = 1;
                        int iCol = 1;
                        int m = 0;

                        string strModType = "";
                        string strCollingNo = "";
                        for (int j = 0; j < dr.Length; j++)
                        {
                            #region 检查站类别(STATION_TYPE_CODE)=’1’(表示输入)
                            if (dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "1")
                            {
                                #region 体检项目基本档(HMS_EXAM_ITEM)中指引单打印标志(HMS_EXAM_ITEM.FLOW_SHEET_FLAG)=’Y’时
                                if (dr[j]["FLOW_SHEET_FLAG"].ToString().Trim() == "Y")
                                {
                                    //FLOW_SHEET_NAME
                                    if (j == 0)
                                    {
                                        WordDoc.Tables[x].Cell(iRow, 3).Select();
                                        table = CreatTable(3);
                                    }
                                    else
                                    {
                                        if (j % 3 == 0)
                                        {
                                            table.Rows.Add(ref Nothing);
                                            iNewRow++;
                                        }
                                    }
                                    iCol = j % 3 + 1;

                                    table.Cell(iNewRow, iCol).Range.Text = "" + dr[j]["FLOW_SHEET_NAME"].ToString().Trim();
                                    table.Cell(iNewRow, iCol).Range.Font.Name = "微软雅黑";
                                    table.Cell(iNewRow, iCol).Range.Font.Size = 10.5F;
                                }
                                else
                                {
                                    continue;
                                }
                                if (dr[j]["EXAM_MEMO"].ToString().Trim() != "" && strMemo.IndexOf(dr[j]["EXAM_MEMO"].ToString().Trim() + ",") < 0)
                                {
                                    strMemo += dr[j]["EXAM_MEMO"].ToString().Trim() + ",";
                                }
                                #endregion
                            }
                            else if (dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "2" || (dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "3"))
                            {
                                #region 检核报告主档(HMS_REPORT)中申请单号(OBSERVATION_NO)有值时
                                if (dr[j]["OBSERVATION_NO"].ToString().Trim() != "" && dr[j]["FLOW_FLAG"].ToString().Trim() == "Y")
                                {
                                    if (dr[j]["FLOW_TYPE_CODE"].ToString().Trim() == "2")
                                    {
                                        if (j == 0)
                                        {
                                            WordDoc.Tables[x].Cell(iRow, 3).Select();
                                            table = CreatTable(3);
                                        }
                                        else
                                        {
                                            if (j % 3 == 0)
                                            {
                                                table.Rows.Add(ref Nothing);
                                                iNewRow++;
                                            }
                                        }
                                        iCol = j % 3 + 1;

                                        table.Cell(iNewRow, iCol).Range.Text = "" + dr[j]["PRINT_TAG_NAME"].ToString().Trim();
                                        table.Cell(iNewRow, iCol).Range.Font.Name = "微软雅黑";
                                        table.Cell(iNewRow, iCol).Range.Font.Size = 10.5F;
                                    }
                                    else if (dr[j]["FLOW_TYPE_CODE"].ToString().Trim() == "1")
                                    {
                                        if (j == 0)
                                        {
                                            WordDoc.Tables[x].Cell(iRow, 3).Select();
                                            table = CreatTable(2);
                                            m = j;
                                        }
                                        else
                                        {
                                            m = j * 2;
                                            table.Rows.Add(ref Nothing);
                                        }

                                        if (Convert.ToInt32("0" + dr[j]["CALLING_NO"].ToString().Trim()) > 0)
                                        {
                                            if (dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "2" || dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "3")
                                            {
                                                table.Cell(m + 1, 1).Range.Text = "" + dr[j]["PRINT_TAG_NAME"].ToString().Trim();
                                                if (strModType != dr[j]["MODTYPE"].ToString().Trim())
                                                {
                                                    strModType = dr[j]["MODTYPE"].ToString().Trim();
                                                    if (strCollingNo.IndexOf(" 排号 " + dr[j]["CALLING_NO"].ToString()) < 0)
                                                    {
                                                        strCollingNo = strCollingNo + "\r\n" + dr[j]["MODTYPE"].ToString().Trim() + " 排号 " + dr[j]["CALLING_NO"].ToString();
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                table.Cell(m + 1, 1).Range.Text = "" + dr[j]["PRINT_TAG_NAME"].ToString().Trim() + " 排号 " + dr[j]["CALLING_NO"].ToString();
                                            }
                                            table.Cell(m + 1, 1).Range.Font.Name = "微软雅黑";
                                            table.Cell(m + 1, 1).Range.Font.Size = 10.5F;
                                        }
                                        else
                                        {
                                            table.Cell(m + 1, 1).Range.Text = "" + dr[j]["PRINT_TAG_NAME"].ToString().Trim();
                                            table.Cell(m + 1, 1).Range.Font.Name = "微软雅黑";
                                            table.Cell(m + 1, 1).Range.Font.Size = 10.5F;
                                        }
                                        options.Width = 130;
                                        options.Height = 15;
                                        options.Margin = 0;
                                        options.PureBarcode = true;
                                        Image BarCodeNew = bw.Write(dr[j]["OBSERVATION_NO"].ToString());
                                        Clipboard.SetDataObject(BarCodeNew);
                                        table.Cell(m + 1, 2).Range.Paste();
                                        table.Cell(m + 1, 2).Range.Text += dr[j]["OBSERVATION_NO"].ToString();
                                        //table.Rows.Add(ref Nothing);
                                        //table.Cell(m + 2, 2).Range.Text = dr[j]["OBSERVATION_NO"].ToString();
                                        BarCodeNew.Dispose();
                                    }
                                    if (strMemo != "" && dr[j]["SHEET_REMARKS"].ToString().Trim() != "" && strMemo.IndexOf(dr[j]["SHEET_REMARKS"].ToString().Trim() + ",") < 0)
                                    {
                                        strMemo += dr[j]["SHEET_REMARKS"].ToString().Trim() + ",";
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                                #endregion
                            }
                            #endregion
                        }
                        WordDoc.Tables[x].Cell(iRow, 1).Range.Text = dr[0]["STATION_NAME"].ToString().Trim() + strCollingNo;
                    }
                    if (dttemp.Rows[i]["FLOW_SHEET_MEMO"].ToString().Trim() != "" || strMemo != "")
                    {
                        WordDoc.Content.Tables[x].Rows.Add(ref Nothing);
                        iRow++;
                        WordDoc.Tables[x].Cell(iRow, 2).Merge(WordDoc.Tables[x].Cell(iRow, 3));
                        WordDoc.Tables[x].Cell(iRow, 1).Width = 128 + 70.5f;
                        WordDoc.Tables[x].Cell(iRow, 2).Width = 333;
                        WordDoc.Tables[x].Cell(iRow, 2).Range.Text = (dttemp.Rows[i]["FLOW_SHEET_MEMO"].ToString().Length > 0 ? dttemp.Rows[i]["FLOW_SHEET_MEMO"].ToString() + "," : "") + strMemo;
                        WordDoc.Tables[x].Cell(iRow, 2).Select();
                        WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                        blMerge = true;
                    }
                }
                WordDoc.Content.Tables[x].Rows.Add(ref Nothing);
                WordDoc.Tables[x].Cell(iRow + 1, 1).Range.Text = "(1)登记报到 指引单缴回";
                if (dtMome.Rows.Count > 0)
                {
                    WordDoc.Tables[x].Cell(7, 1).Range.Text = dtMome.Rows[0]["MEMO_HEAD"].ToString();
                }

                if (WordDoc.Bookmarks.Exists("MEMO_TAIL"))
                {
                    WordDoc.Bookmarks.get_Item("MEMO_TAIL").Range.Text = dtMome.Rows[0]["MEMO_TAIL"].ToString();
                }
                if (strPrintFlag != "Y")
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
                    sfd.FileName = strFileName + ".doc";
                    object oMissing = System.Reflection.Missing.Value;
                    object ofilename = sfd.FileName;
                    WordDoc.SaveAs(ref ofilename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    WordApp.Visible = false;
                    WordDoc.Close();
                    WordApp.Quit();
                }
                else
                {
                    string defaultPrinter = WordApp.ActivePrinter;
                    WordApp.PrintPreview = false;
                    WordApp.ActivePrinter = strWordPrint;
                    WordApp.PrintOut();
                    object oMissing = System.Reflection.Missing.Value;

                    WordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                    WordDoc.Saved = true;
                    WordApp.ActivePrinter = defaultPrinter;
                    WordApp.Quit();
                }
                BarCode.Dispose();
            }
            catch (Exception ex)
            {
                WordApp.Quit();
                throw ex;
            }
            finally
            {
                foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
                {
                    p.Kill();
                }
                GC.Collect();
            }
        }

        private Table CreatTable(int iColumn)
        {
            object Nothing = System.Reflection.Missing.Value;
            Range rng = WordApp.Selection.Range;
            rng.ParagraphFormat.LineSpacing = 15F;
            Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(rng, 1, iColumn, ref Nothing, ref Nothing);
            if (iColumn == 2)
            {
                table.Columns[1].Width = 180;
                table.Columns[2].Width = 160;
            }
            table.Rows.Alignment = WdRowAlignment.wdAlignRowLeft;
            table.Range.Font.Name = "微软雅黑";
            table.Range.Font.Size = 10.5F;
            return table;
        }

手动穿件Word.dot 模板文档格式如下,插入相应标签

  /// <summary>
        /// 生产报表
        /// </summary>
        /// <param name="strTemplate"></param>
        public void CreateWordFile(DataRow item)
        {
            try
            {
                MultiFormatWriter multiWriter = new MultiFormatWriter();
                QrCodeEncodingOptions options = new QrCodeEncodingOptions();
                BarcodeWriter bw = new BarcodeWriter();
                bw.Format = BarcodeFormat.CODE_128;
                bw.Options = options;

                object Nothing = System.Reflection.Missing.Value;
                //定义该插入图片是否为外部链接
                object linkToFile = true;
                //定义插入图片是否随word文档一起保存
                object saveWithDocument = true;

                WordApp = new Word.Application();
                WordDoc = new Word.Document();
                WordApp.Visible = false;
                WordApp.PrintPreview = false;

                string filename = System.Windows.Forms.Application.StartupPath + "\\RPT\\HMSRPT0131.dot";
                try
                {
                    WordDoc = WordApp.Documents.Add(filename);
                    WordDoc = WordApp.ActiveDocument;
                }
                catch (Exception ex)
                {
                    throw (ex);
                }

                WordDoc.Tables[1].Cell(1, 2).Range.Text = HospitalName;
                int x = 1;
                //Table2  x = 1
                WordDoc.Tables[x].Cell(1, 1).Range.Text = item["CORP_DEPT_NAME"].ToString().Trim();
                WordDoc.Tables[x].Cell(2, 1).Range.Text = item["MEMO"].ToString().Trim();
                options.Width = 200;
                options.Height = 50;
                options.Margin = 0;
                options.PureBarcode = false;
                Image BarCode = bw.Write(item["REGISTER_NO"].ToString().Trim());
                Clipboard.SetDataObject(BarCode);
                WordDoc.Tables[x].Cell(1, 3).Range.Paste();
                BarCode.Dispose();
                WordDoc.Tables[x].Cell(4, 2).Range.Text = item["PATIENT_NAME"].ToString().Trim();
                WordDoc.Tables[x].Cell(4, 4).Range.Text = item["BIRTHDAY"].ToString().Trim();
                WordDoc.Tables[x].Cell(4, 6).Range.Text = item["CHART_NO"].ToString().Trim();

                if (item["CORPORATION_NAME"].ToString().Length > 26)
                {
                    WordDoc.Tables[x].Cell(5, 2).Range.Text = item["CORPORATION_NAME"].ToString().Trim().Substring(0, 26);
                }
                else
                {
                    WordDoc.Tables[x].Cell(5, 2).Range.Text = item["CORPORATION_NAME"].ToString().Trim();
                }
                WordDoc.Tables[x].Cell(5, 4).Range.Text = item["BOOKING_DATE"].ToString().Trim();

                if (item["ADDRESS"].ToString().Length > 26)
                {
                    WordDoc.Tables[x].Cell(6, 2).Range.Text = item["ADDRESS"].ToString().Trim().Substring(0, 26);
                }
                else
                {
                    WordDoc.Tables[x].Cell(6, 2).Range.Text = item["ADDRESS"].ToString().Trim();
                }
                WordDoc.Tables[x].Cell(6, 4).Range.Text = item["PHONE_NUMBER"].ToString().Trim();

                if (item["PHOTO"].ToString() != "")
                {
                    SaveFileDialog sflg = new SaveFileDialog();
                    MemoryStream ms = new MemoryStream((byte[])item["PHOTO"]);//把照片读到MemoryStream里    
                    Image imageBlob = Image.FromStream(ms, true);//用流创建Image                       
                    imageBlob.Save(System.Windows.Forms.Application.StartupPath + "\\" + item["REGISTER_NO"].ToString() + ".jpg");
                    ////插入图片
                    WordDoc.Tables[x].Cell(4, 7).Range.Text = "";
                    WordDoc.Tables[x].Cell(4, 7).Select();
                    object range = WordApp.Selection.Range;
                    Word.InlineShape shape = WordApp.ActiveDocument.InlineShapes.AddPicture(System.Windows.Forms.Application.StartupPath + "\\" + item["REGISTER_NO"].ToString() + ".jpg", ref linkToFile, ref saveWithDocument, ref range);

                    shape.Width = 73;//图片宽度
                    shape.Height = 91;//图片高度
                    System.IO.FileInfo file = new System.IO.FileInfo(System.Windows.Forms.Application.StartupPath + "\\" + item["REGISTER_NO"].ToString() + ".jpg");
                    if (file.Exists)
                    {
                        file.Delete();
                    }
                    imageBlob.Dispose();
                }
                DataTable dtHMS_REGISTER = hmsService.QueryHmsRequestDatafor0131(item["REGISTER_NO"].ToString().Trim(), strHavePrint);
                dtHMS_REGISTER.DefaultView.Sort = "STATION_ID";

                System.Data.DataTable dttemp = new System.Data.DataTable();
                DataView tempDv = dtHMS_REGISTER.DefaultView;
                dtHMS_REGISTER = tempDv.ToTable();

                dttemp = tempDv.ToTable(true, new[] { "STATION_NAME", "FLOW_SHEET_MEMO" });

                bool blMerge = false;
                Table table = null;
                int iRow = 11;
                WordDoc.Content.Tables[x].Rows.Add(ref Nothing);
                WordDoc.Tables[x].Cell(10, 1).Range.Text = "(1)登记报到";
                for (int i = 0; i < dttemp.Rows.Count; i++, iRow++)
                {
                    string strMemo = "";
                    DataRow[] dr = dtHMS_REGISTER.Select("STATION_NAME = '" + dttemp.Rows[i]["STATION_NAME"].ToString().Trim() + "'");

                    if (dr.Length > 0)
                    {
                        //创建新行
                        if (i > 0)
                        {
                            WordDoc.Content.Tables[x].Rows.Add(ref Nothing);
                            //拆分以合并行
                            if (blMerge)
                            {
                                WordDoc.Content.Tables[x].Cell(iRow, 1).Merge(WordDoc.Tables[x].Cell(iRow, 2));
                                WordDoc.Content.Tables[x].Cell(iRow, 1).Split(1, 3);
                                WordDoc.Content.Tables[x].Cell(iRow, 1).Width = 128;
                                WordDoc.Content.Tables[x].Cell(iRow, 2).Width = 70.5f;
                                WordDoc.Content.Tables[x].Cell(iRow, 3).Width = 333;
                                blMerge = false;
                            }
                        }
                        WordDoc.Tables[x].Cell(iRow, 1).Range.Text = dr[0]["STATION_NAME"].ToString().Trim();
                        int iNewRow = 1;
                        int iCol = 1;
                        int m = 0;

                        string strModType = "";
                        string strCollingNo = "";
                        for (int j = 0; j < dr.Length; j++)
                        {
                            #region 检查站类别(STATION_TYPE_CODE)=’1’(表示输入)
                            if (dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "1")
                            {
                                #region 体检项目基本档(HMS_EXAM_ITEM)中指引单打印标志(HMS_EXAM_ITEM.FLOW_SHEET_FLAG)=’Y’时
                                if (dr[j]["FLOW_SHEET_FLAG"].ToString().Trim() == "Y")
                                {
                                    //FLOW_SHEET_NAME
                                    if (j == 0)
                                    {
                                        WordDoc.Tables[x].Cell(iRow, 3).Select();
                                        table = CreatTable(3);
                                    }
                                    else
                                    {
                                        if (j % 3 == 0)
                                        {
                                            table.Rows.Add(ref Nothing);
                                            iNewRow++;
                                        }
                                    }
                                    iCol = j % 3 + 1;

                                    table.Cell(iNewRow, iCol).Range.Text = "□ " + dr[j]["FLOW_SHEET_NAME"].ToString().Trim();
                                    table.Cell(iNewRow, iCol).Range.Font.Name = "微软雅黑";
                                    table.Cell(iNewRow, iCol).Range.Font.Size = 10.5F;
                                }
                                else
                                {
                                    continue;
                                }
                                if (dr[j]["EXAM_MEMO"].ToString().Trim() != "" && strMemo.IndexOf(dr[j]["EXAM_MEMO"].ToString().Trim() + ",") < 0)
                                {
                                    strMemo += dr[j]["EXAM_MEMO"].ToString().Trim() + ",";
                                }
                                #endregion
                            }
                            else if (dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "2" || (dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "3"))
                            {
                                #region 检核报告主档(HMS_REPORT)中申请单号(OBSERVATION_NO)有值时
                                if (dr[j]["OBSERVATION_NO"].ToString().Trim() != "" && dr[j]["FLOW_FLAG"].ToString().Trim() == "Y")
                                {
                                    if (dr[j]["FLOW_TYPE_CODE"].ToString().Trim() == "2")
                                    {
                                        if (j == 0)
                                        {
                                            WordDoc.Tables[x].Cell(iRow, 3).Select();
                                            table = CreatTable(3);
                                        }
                                        else
                                        {
                                            if (j % 3 == 0)
                                            {
                                                table.Rows.Add(ref Nothing);
                                                iNewRow++;
                                            }
                                        }
                                        iCol = j % 3 + 1;

                                        table.Cell(iNewRow, iCol).Range.Text = "□ " + dr[j]["PRINT_TAG_NAME"].ToString().Trim();
                                        table.Cell(iNewRow, iCol).Range.Font.Name = "微软雅黑";
                                        table.Cell(iNewRow, iCol).Range.Font.Size = 10.5F;
                                    }
                                    else if (dr[j]["FLOW_TYPE_CODE"].ToString().Trim() == "1")
                                    {
                                        if (j == 0)
                                        {
                                            WordDoc.Tables[x].Cell(iRow, 3).Select();
                                            table = CreatTable(2);
                                            m = j;
                                        }
                                        else
                                        {
                                            m = j * 2;
                                            table.Rows.Add(ref Nothing);
                                        }

                                        if (Convert.ToInt32("0" + dr[j]["CALLING_NO"].ToString().Trim()) > 0)
                                        {
                                            if (dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "2" || dr[j]["STATION_TYPE_CODE"].ToString().Trim() == "3")
                                            {
                                                table.Cell(m + 1, 1).Range.Text = "□ " + dr[j]["PRINT_TAG_NAME"].ToString().Trim();
                                                if (strModType != dr[j]["MODTYPE"].ToString().Trim())
                                                {
                                                    strModType = dr[j]["MODTYPE"].ToString().Trim();
                                                    if (strCollingNo.IndexOf(" 排号 " + dr[j]["CALLING_NO"].ToString()) < 0)
                                                    {
                                                        strCollingNo = strCollingNo + "\r\n" + dr[j]["MODTYPE"].ToString().Trim() + " 排号 " + dr[j]["CALLING_NO"].ToString();
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                table.Cell(m + 1, 1).Range.Text = "□ " + dr[j]["PRINT_TAG_NAME"].ToString().Trim() + " 排号 " + dr[j]["CALLING_NO"].ToString();
                                            }
                                            table.Cell(m + 1, 1).Range.Font.Name = "微软雅黑";
                                            table.Cell(m + 1, 1).Range.Font.Size = 10.5F;
                                        }
                                        else
                                        {
                                            table.Cell(m + 1, 1).Range.Text = "□ " + dr[j]["PRINT_TAG_NAME"].ToString().Trim();
                                            table.Cell(m + 1, 1).Range.Font.Name = "微软雅黑";
                                            table.Cell(m + 1, 1).Range.Font.Size = 10.5F;
                                        }
                                        options.Width = 130;
                                        options.Height = 15;
                                        options.Margin = 0;
                                        options.PureBarcode = true;
                                        Image BarCodeNew = bw.Write(dr[j]["OBSERVATION_NO"].ToString());
                                        Clipboard.SetDataObject(BarCodeNew);
                                        table.Cell(m + 1, 2).Range.Paste();
                                        table.Cell(m + 1, 2).Range.Text += dr[j]["OBSERVATION_NO"].ToString();
                                        //table.Rows.Add(ref Nothing);
                                        //table.Cell(m + 2, 2).Range.Text = dr[j]["OBSERVATION_NO"].ToString();
                                        BarCodeNew.Dispose();
                                    }
                                    if (strMemo != "" && dr[j]["SHEET_REMARKS"].ToString().Trim() != "" && strMemo.IndexOf(dr[j]["SHEET_REMARKS"].ToString().Trim() + ",") < 0)
                                    {
                                        strMemo += dr[j]["SHEET_REMARKS"].ToString().Trim() + ",";
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                                #endregion
                            }
                            #endregion
                        }
                        WordDoc.Tables[x].Cell(iRow, 1).Range.Text = dr[0]["STATION_NAME"].ToString().Trim() + strCollingNo;
                    }
                    if (dttemp.Rows[i]["FLOW_SHEET_MEMO"].ToString().Trim() != "" || strMemo != "")
                    {
                        WordDoc.Content.Tables[x].Rows.Add(ref Nothing);
                        iRow++;
                        WordDoc.Tables[x].Cell(iRow, 2).Merge(WordDoc.Tables[x].Cell(iRow, 3));
                        WordDoc.Tables[x].Cell(iRow, 1).Width = 128 + 70.5f;
                        WordDoc.Tables[x].Cell(iRow, 2).Width = 333;
                        WordDoc.Tables[x].Cell(iRow, 2).Range.Text = (dttemp.Rows[i]["FLOW_SHEET_MEMO"].ToString().Length > 0 ? dttemp.Rows[i]["FLOW_SHEET_MEMO"].ToString() + "," : "") + strMemo;
                        WordDoc.Tables[x].Cell(iRow, 2).Select();
                        WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                        blMerge = true;
                    }
                }
                WordDoc.Content.Tables[x].Rows.Add(ref Nothing);
                WordDoc.Tables[x].Cell(iRow + 1, 1).Range.Text = "(1)登记报到 指引单缴回";
                if (dtMome.Rows.Count > 0)
                {
                    WordDoc.Tables[x].Cell(7, 1).Range.Text = dtMome.Rows[0]["MEMO_HEAD"].ToString();
                }

                if (WordDoc.Bookmarks.Exists("MEMO_TAIL"))
                {
                    WordDoc.Bookmarks.get_Item("MEMO_TAIL").Range.Text = dtMome.Rows[0]["MEMO_TAIL"].ToString();
                }
                if (strPrintFlag != "Y")
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
                    sfd.FileName = strFileName + ".doc";
                    object oMissing = System.Reflection.Missing.Value;
                    object ofilename = sfd.FileName;
                    WordDoc.SaveAs(ref ofilename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    WordApp.Visible = false;
                    WordDoc.Close();
                    WordApp.Quit();
                }
                else
                {
                    string defaultPrinter = WordApp.ActivePrinter;
                    WordApp.PrintPreview = false;
                    WordApp.ActivePrinter = strWordPrint;
                    WordApp.PrintOut();
                    object oMissing = System.Reflection.Missing.Value;

                    WordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                    WordDoc.Saved = true;
                    WordApp.ActivePrinter = defaultPrinter;
                    WordApp.Quit();
                }
                BarCode.Dispose();
            }
            catch (Exception ex)
            {
                WordApp.Quit();
                throw ex;
            }
            finally
            {
                foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
                {
                    p.Kill();
                }
                GC.Collect();
            }
        }

        private Table CreatTable(int iColumn)
        {
            object Nothing = System.Reflection.Missing.Value;
            Range rng = WordApp.Selection.Range;
            rng.ParagraphFormat.LineSpacing = 15F;
            Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(rng, 1, iColumn, ref Nothing, ref Nothing);
            if (iColumn == 2)
            {
                table.Columns[1].Width = 180;
                table.Columns[2].Width = 160;
            }
            table.Rows.Alignment = WdRowAlignment.wdAlignRowLeft;
            table.Range.Font.Name = "微软雅黑";
            table.Range.Font.Size = 10.5F;
            return table;
        }

猜你喜欢

转载自www.cnblogs.com/6B23/p/12204986.html