zoukankan      html  css  js  c++  java
  • 获取WOED和EXCEL的公用方法

           1. 需要传入word地址

    /// <summary> /// 获取WORD内容 /// </summary> /// <param name="docFileName"></param> /// <returns></returns> public string Doc2Text(string docFileName) { Word.Application app = new Microsoft.Office.Interop.Word.Application(); object fileobj = docFileName; object nullobj = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref fileobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj ); string outText = doc.Content.Text; doc.Close(ref nullobj, ref nullobj, ref nullobj); app.Quit(ref nullobj, ref nullobj, ref nullobj); return outText; }


    2. 传入Excel的路径即可

     1         /// <summary>
     2         /// 读取Excel
     3         /// </summary>
     4         /// <param name="strFileName"></param>
     5         public string ResumeExcel(string path)
     6         {
     7             string str = string.Empty;
     8             //创建Application对象
     9             Excel.Application xApp = new Excel.ApplicationClass();
    10             xApp.Visible = false;
    11 
    12             object Missing = System.Reflection.Missing.Value;
    13             //得到WorkBook对象,
    14             Excel.Workbook xBook = xApp.Workbooks.Open(path, Missing, Missing, Missing, Missing,
    15                   Missing, Missing, Missing, Missing,
    16                   Missing, Missing, Missing, Missing);
    17 
    18             //指定要操作的Sheet:
    19             Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
    20 
    21             //读取,通过Range对象,但使用不同的接口得到Range
    22             for (int i = 1; i <= 100; i++)
    23             {
    24                 for (int j = 1; j <= 100; j++)
    25                 {
    26                     Excel.Range rng = (Excel.Range)xSheet.Cells[i, j];
    27                     if (rng.Value2 != null)
    28                     {
    29                         str += rng.Value2.ToString();
    30                     }
    31                 }
    32             }
    33             xApp.Quit();
    34             return str;
    35         }
        3. 打开word或者Excel后要杀死进程,以免下次打开报错
    public void KillProcess() { System.Diagnostics.Process[] myPs; myPs = System.Diagnostics.Process.GetProcesses(); string myS = "EXCEL.EXE"; foreach (System.Diagnostics.Process p in myPs) { try { if (p.Modules != null) if (p.Modules.Count > 0) { System.Diagnostics.ProcessModule pm = p.Modules[0]; if (pm.ModuleName.ToLower() == "excel.exe") p.Kill(); } } catch { } finally { } } }
    欢迎交流,一起进步
  • 相关阅读:
    jQuery选择器
    jQuery属性
    复选框的全选+全不选+ajax传递复选框的value值+后台接受复选框默认值
    [BZOJ1085][SCOI2005]骑士精神 搜索
    [BZOJ1004][HNOI2008]Cards 群论+置换群+DP
    [BZOJ1046][HAOI2007]上升序列 DP+贪心
    [BZOJ1016][JSOI2008]最小生成树计数 最小生成树 搜索
    [BZOJ1031][JSOI2007]字符加密Cipher 后缀数组
    后缀数组学习笔记
    [SPOJ8222]NSUBSTR
  • 原文地址:https://www.cnblogs.com/yunangel/p/6112446.html
Copyright © 2011-2022 走看看