zoukankan      html  css  js  c++  java
  • c# 将word内容放到Excel的一个单元格中

            /// <summary>
            /// 将一个word文档中的所有文本存放到excel的指定单元格内
            /// using Excel = Microsoft.Office.Interop.Excel;
            /// using Word = Microsoft.Office.Interop.Word;
            /// </summary>
            /// <param name="wordPath">word文档的路径</param>
            /// <param name="excelPath">excel文档的路径</param>
            /// <param name="row"></param>
            /// <param name="col"></param>
            public void WordCopy2Excel(string wordPath, string excelPath, int row, int col)
            {
                string pos = "" + (char)('A' + row - 1) + col;
                object oMissing = System.Reflection.Missing.Value;
                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.Copy();
            //关掉document oDoc.Close();
            //关掉应用程序 oWord.Quit();
            //打开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;
            //选择粘贴模式(具体的模式有多少种 我也不太清楚 另外一种xlPasteFormats)还可以默认,但是是图片格式粘贴 sheet.Range[pos].PasteSpecial(Excel.XlPasteType.xlPasteValues); wb.Save(); app.Quit(); }

    想提升速度,就是不要关app,将app.Quit()去掉

    需要配置com组件,具体配置http://www.cnblogs.com/mengxingxinqing/archive/2013/06/07/3123344.html

    获取word的所有文本内容

    str = myWordDoc.Content.Text;

  • 相关阅读:
    单例模式-Singlleton
    C#中静态与非静态方法比较
    关于orcale的数据库脚本,记录下来,方便自己以后用到查找
    关于Oracle和SQLServer数据库在.net中拼接数据库语句的不同
    Oracle数据类型与.NET中的对应关系
    Got a packet bigger than 'max_allowed_packet' bytes
    .NET、C#和ASP.NET三者之间的区别(转)
    The use specified as definer('root'@'%') does not exist的解决办法
    app.config .exe.config .vshost.exe.config配置
    python学习:(3)自动化表单提交
  • 原文地址:https://www.cnblogs.com/mengxingxinqing/p/3121544.html
Copyright © 2011-2022 走看看