zoukankan      html  css  js  c++  java
  • 将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();
                
            }
  • 相关阅读:
    ADO.NET基础学习 二(Command对象)
    如何使ElementUI中的el-dropdown传入多参数
    Vue router Element 重复点击导航路由报错解决方法
    vue-svgicon基本使用
    js 两个相等的数组,修改其中一个怎么做到不改变另外一个
    LocalStorage存储JSON对象
    js取整数、取余数的方法
    JS正则截取两个字符串之间及字符串前后内容的方法
    Vuetify 表单规则验证
    JS正则表达式
  • 原文地址:https://www.cnblogs.com/mengxingxinqing/p/3133764.html
Copyright © 2011-2022 走看看