예제 #1
0
        /// <summary>
        /// 将sFile描述的WORD文件写入到WORD模板文件中
        /// </summary>
        /// <param name="Key">打印模板参考位置</param>
        /// <param name="sFile">待写入到ORD模板文件中的WORD文档</param>
        /// <param name="mode">写入时的辅助参考值</param>
        /// <param name="offset">相对mode的偏移值</param>
        public override void WriteFile(string Key, string sFile, WriteMode mode, int offset)
        {
            try
            {
                if (File.Exists(sFile))
                {
                    KeyPosition kp = new KeyPosition();
                    for (int idx = 0; idx < m_arKeyPosition.Length; idx++)
                    {
                        if ((ArrayList)m_arKeyPosition.GetValue(idx) != null)
                        {
                            kp = GetPositionByKey(Key, (ArrayList)m_arKeyPosition.GetValue(idx));
                            if (kp.RowIndex != 0)
                            {
                                switch (mode)
                                {
                                    case WriteMode.Up:
                                        m_doc.Tables[idx + 1].Rows[kp.RowIndex - offset].Cells[kp.ColIndex].Range.Paragraphs.Last.Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                        SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex - offset].Cells[kp.ColIndex].Range);
                                        break;
                                    case WriteMode.Down:
                                        m_doc.Tables[idx + 1].Rows[kp.RowIndex + offset].Cells[kp.ColIndex].Range.Paragraphs.Last.Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                        SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex + offset].Cells[kp.ColIndex].Range);
                                        break;
                                    case WriteMode.Inner:
                                        m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex].Range.Paragraphs.Last.Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                        SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex].Range);
                                        //m_doc.Tables[1].Rows[kp.RowIndex].Cells[kp.ColIndex].Range.Text += value;
                                        break;
                                    case WriteMode.Right:
                                        m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex + offset].Range.Paragraphs.Last.Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                        SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex + offset].Range);
                                        //m_doc.Tables[1].Rows[kp.RowIndex].Cells[kp.ColIndex + 1].Range.Text += value;
                                        break;
                                    case WriteMode.Down_Append:
                                        m_doc.Tables[idx + 1].Rows[kp.RowIndex + offset].Cells[kp.ColIndex].Range.Paragraphs.Last.Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                        SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex + offset].Cells[kp.ColIndex].Range);
                                        break;
                                }
                                break;
                            }
                        }
                        else
                        {
                            for (int i = 1; i <= m_doc.Paragraphs.Count; i++)
                            {
                                if (this.HandleString(m_doc.Paragraphs[i].Range.Text) == Key)
                                {
                                    Word._Application WordApp = new Application();
                                    Word._Document doc = null;
                                    object oFile = (object)sFile;
                                    doc = WordApp.Documents.Open(ref oFile, 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);

                                    doc.Activate();
                                    object oFirst = doc.Paragraphs.First;
                                    object oLast = doc.Paragraphs.Last;
                                    SetTextFormat(doc.Range(ref oFirst, ref oLast));
                                    doc.Save();

                                    if (WordApp != null)
                                    {
                                        if (doc != null)
                                            doc.Close(ref oMissing, ref oMissing, ref oMissing);
                                        WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                                        doc = null;
                                        WordApp = null;
                                    }
                                    KillWordProcess(new TimeSpan(0, _INVALID_WORDPROC_TIMEOUT_, 0));

                                    switch (mode)
                                    {
                                        case WriteMode.Up:
                                            m_doc.Paragraphs[i - offset].Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                            //this.m_WordApp.Selection.Document.Paragraphs[i - offset].Range
                                            break;
                                        case WriteMode.Down:
                                            m_doc.Paragraphs[i + offset].Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                            break;
                                        case WriteMode.Inner:
                                            m_doc.Paragraphs[i].Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                            break;
                                        case WriteMode.Right:
                                            m_doc.Paragraphs[i + offset].Range.InsertFile(sFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                            break;
                                    }
                                    return;
                                }
                            }
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return;
            }
        }
예제 #2
0
 /// <summary>
 /// 插入图片到WORD打印模板中
 /// </summary>
 /// <param name="rect">待插入图片设置的区域大小</param>
 /// <param name="sParams">待写入参数</param>
 /// <param name="sTimeStamp">生成图片时写入的时间戳</param>
 /// <param name="fLenRatio">图片长比例</param>
 /// <param name="fWidRatio">图片宽比例</param>
 public override void AddPicture(CRect rect, string[] sParams, string sTimeStamp,
     double fLenRatio, double fWidRatio, string sDisplay,
     string sUserID)
 {
     KeyPosition kp = new KeyPosition();
     for (int idx = 0; idx < m_arKeyPosition.Length; idx++)
     {
         if ((ArrayList)m_arKeyPosition.GetValue(idx) != null)
         {
             kp = GetPositionByKey(sParams[0], (ArrayList)m_arKeyPosition.GetValue(idx));
             if (kp.RowIndex != 0)
             {
                 switch (sParams[1].ToLower())
                 {
                     case "down":
                         InsertPicture(m_doc.Tables[kp.TblIndex].Rows[kp.RowIndex + 1].Cells[kp.ColIndex].Range,
                                       rect, sTimeStamp, fLenRatio, fWidRatio, sDisplay, sUserID);
                         return;
                     case "right":
                         InsertPicture(m_doc.Tables[kp.TblIndex].Rows[kp.RowIndex].Cells[kp.ColIndex + 1].Range,
                                       rect, sTimeStamp, fLenRatio, fWidRatio, sDisplay, sUserID);
                         return;
                     case "downs":
                         string[] arrStamp = sTimeStamp.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);
                         string[] arrsUserID = sUserID.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                         for (int i = 1; i <= m_doc.Tables[kp.TblIndex].Rows.Count; i++)
                         {
                             sTimeStamp = arrStamp[i-1]; sUserID = arrsUserID[i-1];
                             InsertPicture(m_doc.Tables[kp.TblIndex].Rows[kp.RowIndex + i].Cells[kp.ColIndex].Range,
                                           rect, sTimeStamp, fLenRatio, fWidRatio, sDisplay, sUserID);
                         }
                         break;
                 }
             }
         }
     }
 }
예제 #3
0
        /// <summary>
        /// 将VALUE值按照mode模式写入到key相关的位置处
        /// </summary>
        /// <param name="key">打印模板参考位置</param>
        /// <param name="value">待输出到WORD文档中的字符串</param>
        /// <param name="mode">写入时的辅助参考值</param>
        /// <param name="offset">相对mode的偏移值</param>
        public override void Write(string key, string value, WriteMode mode, int offset)
        {
            KeyPosition kp = new KeyPosition();
            for (int idx = 0; idx < m_arKeyPosition.Length; idx++)
            {
                if ((ArrayList)m_arKeyPosition.GetValue(idx) != null)
                {
                    kp = GetPositionByKey(key, (ArrayList)m_arKeyPosition.GetValue(idx));
                    if (kp.RowIndex != 0)
                    {
                        switch (mode)
                        {
                            case WriteMode.Up:
                                m_doc.Tables[idx + 1].Rows[kp.RowIndex - offset].Cells[kp.ColIndex].Range.InsertBefore(value);
                                //SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex - offset].Cells[kp.ColIndex].Range);
                                break;
                            case WriteMode.Down:
                                m_doc.Tables[idx + 1].Rows[kp.RowIndex + offset].Cells[kp.ColIndex].Range.InsertBefore(value);
                                //SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex + offset].Cells[kp.ColIndex].Range);
                                break;
                            case WriteMode.Inner:
                                m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex].Range.InsertAfter(value);
                                //m_doc.Tables[1].Rows[kp.RowIndex].Cells[kp.ColIndex].Range.Text += value;
                                //SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex].Range);
                                break;
                            case WriteMode.Right:
                                m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex + offset].Range.InsertBefore(value);
                                //m_doc.Tables[1].Rows[kp.RowIndex].Cells[kp.ColIndex + 1].Range.Text += value;
                                //SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex + offset].Range);
                                break;
                            case WriteMode.Shift:
                                m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex].Range.Text = value;
                                //SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex].Cells[kp.ColIndex].Range);
                                break;

                            case WriteMode.Down_Append:
                                m_doc.Tables[idx + 1].Rows[kp.RowIndex + 1].Cells[kp.ColIndex].Range.InsertAfter(value);
                                //SetTextFormat(m_doc.Tables[idx + 1].Rows[kp.RowIndex + 1].Cells[kp.ColIndex].Range);
                                break;
                        }
                        break;
                    }
                }
                else
                {
                    for (int i = 1; i <= m_doc.Paragraphs.Count; i++)
                    {
                        if (this.HandleString(m_doc.Paragraphs[i].Range.Text) == key)
                        {
                            switch (mode)
                            {
                                case WriteMode.Up:
                                    m_doc.Paragraphs[i - offset].Range.InsertBefore(value);
                                    //SetTextFormat(m_doc.Paragraphs[i - offset].Range);
                                    break;
                                case WriteMode.Down:
                                    m_doc.Paragraphs[i + offset].Range.InsertBefore(value);
                                    //SetTextFormat(m_doc.Paragraphs[i + offset].Range);
                                    break;
                                case WriteMode.Inner:
                                    m_doc.Paragraphs[i].Range.InsertAfter(value);
                                    //SetTextFormat(m_doc.Paragraphs[i].Range);
                                    break;
                                case WriteMode.Right:
                                    m_doc.Paragraphs[i + offset].Range.InsertBefore(value);
                                    //SetTextFormat(m_doc.Paragraphs[i + offset].Range);
                                    break;
                                case WriteMode.Shift:
                                    m_doc.Paragraphs[i].Range.Text = value;
                                    //SetTextFormat(m_doc.Paragraphs[i].Range);
                                    break;
                            }
                            return;
                        }
                    }
                    return;
                }
            }
        }
예제 #4
0
        /// <summary>
        /// 遍历模版文档表格数据,记录得到的KEY值所在的表格位置,以及Sentences位置
        /// </summary>
        /// <returns></returns>
        private ArrayList TraverTable(int TableIndex)
        {
            try
            {
                ArrayList arTmp = new ArrayList();

                if (m_doc.Tables.Count > 0)
                {
                    for (int iRow = 1; iRow <= m_doc.Tables[TableIndex].Rows.Count; iRow++)
                    {
                        for (int iCol = 1; iCol <= m_doc.Tables[TableIndex].Rows[iRow].Cells.Count; iCol++)
                        {
                            KeyPosition kp = new KeyPosition();
                            kp.TblIndex = TableIndex;
                            if (m_doc.Tables[TableIndex].Rows[iRow].Cells[iCol].Range.Paragraphs.Count > 1)
                            {
                                kp.CnKey = HandleString(m_doc.Tables[TableIndex].Rows[iRow].Cells[iCol].Range.Paragraphs[1].Range.Text);
                                kp.EnKey = HandleString(m_doc.Tables[TableIndex].Rows[iRow].Cells[iCol].Range.Paragraphs[2].Range.Text);
                            }
                            else
                            {
                                kp.CnKey = HandleString(m_doc.Tables[TableIndex].Rows[iRow].Cells[iCol].Range.Paragraphs[1].Range.Text);
                            }
                            kp.RowIndex = iRow;
                            kp.ColIndex = iCol;
                            //kp.SenIndex = TraverSentence(kp.CnKey);
                            arTmp.Add(kp);
                        }
                    }
                }
                return arTmp;
            }
            catch
            {
                return null;
            }
        }