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;

  • 相关阅读:
    责任链模式(Chain of Responsibility)
    模板模式(Template Method)
    组合模式(Composite Pattern)
    原型模式(Prototype Pattern)
    策略模式(Strategy Pattern)
    状态模式(State Pattern)
    增删改查
    安卓sql
    安卓第三次作业
    安卓第四周作业
  • 原文地址:https://www.cnblogs.com/mengxingxinqing/p/3121544.html
Copyright © 2011-2022 走看看