将excel单元格中的内容放到word中

     /// <summary>
        /// 将一个word文档中的所有文本存放到excel的指定单元格内
        /// using Excel = Microsoft.Office.Interop.Excel;
        /// using Word = Microsoft.Office.Interop.Word;
        /// </summary>
        /// <param name="excelPath">excel文档的路径</param>
        /// <param name="wordPath">word文档的路径</param>
        /// <param name="row">行 从1开始</param>
        /// <param name="col">列 从1开始</param>
        public void ExcelCopy2Word(string excelPath, string wordPath, int row, int col)
        {
            string pos = "" + (char)('A' + col - 1) + row;
            object oMissing = System.Reflection.Missing.Value;

            //打开excel应用程序
            Excel.Application app = new Excel.Application();
            string filename = excelPath;
            object optional = System.Reflection.Missing.Value;
            //打开一个excel文档
            Excel.Workbook wb = app.Workbooks.Open(
                        filename,
                        optional,
                        optional,
                        optional,
                        optional,
                        optional,
                        optional,
                        optional,
                        optional,
                        optional,
                        optional,
                        optional
                );
            app.Visible = true;
            //选中当前活动的sheet
            Excel._Worksheet sheet = (Excel.Worksheet)wb.ActiveSheet;
            sheet.Range[pos].Copy();
            app.Quit();

            Word._Application oWord;
            Word._Document oDoc;
            //打开应用 
            oWord = new Word.Application();
            oWord.Visible = true;
            object fileName = wordPath;
            //打开文档
            oDoc = oWord.Documents.Open(ref fileName,
           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);
            //选中整个document
            oDoc.ActiveWindow.Selection.WholeStory();
            //粘贴
            oDoc.ActiveWindow.Selection.Paste();
            oDoc.Save();
            //关掉document
            oDoc.Close();
            //关掉应用程序
            oWord.Quit();
            
        }

 

posted @ 2013-06-13 11:31  梦醒心晴  Views(401)  Comments(0Edit  收藏  举报