zoukankan      html  css  js  c++  java
  • 读写word

    确保添加 microsoft word 11.0 object libiary (word2003)

    1.读word

    代码
    1 public string readDoc(string fileName)
    2 {
    3 Word.Application wordApp = new Word.Application();
    4 // Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    5   object fileobj = fileName;
    6 object nullobj = System.Reflection.Missing.Value;
    7 //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
    8   Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
    9 ref nullobj, ref nullobj, ref nullobj,
    10 ref nullobj, ref nullobj, ref nullobj,
    11 ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
    12 );
    13 //取得doc文件中的文本
    14   string outText = doc.Content.Text;
    15 //关闭文件
    16   doc.Close(ref nullobj, ref nullobj, ref nullobj);
    17 //关闭COM
    18   wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
    19 //返回
    20 return outText;
    21
    22 }

    2.写word

    代码
    1 public string write(object path,string content)
    2 {
    3 //object path;
    4 //path = @"e:\mf\data\" + fileName;
    5 if (File.Exists((string)path))
    6 {
    7 File.Delete((string)path);
    8 }
    9 object Nothing = System.Reflection.Missing.Value;
    10 Word.Application wordApp = new Word.Application();
    11 Word.Document wordDoc = new Word.Document();
    12 wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    13 //string strContent;
    14 //strContent = "你好";
    15 wordDoc.Paragraphs.Last.Range.Text = content;
    16 object format = Word.WdSaveFormat.wdFormatDocument;
    17 wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    18 wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
    19 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
    20 return (string)path;
    21 }
    Right! is "Fuck GIS",but don't think too much; It means reach a high during playing GIS. Come on!
  • 相关阅读:
    PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]
    PAT Advanced 1029 Median (25) [two pointers]
    PAT Advanced 1010 Radix(25) [⼆分法]
    PAT Basic 1070 结绳(25) [排序,贪⼼]
    PAT Basic 1023 组个最⼩数 (20) [贪⼼算法]
    PAT Basic 1020 ⽉饼 (25) [贪⼼算法]
    PAT Advanced 1070 Mooncake (25) [贪⼼算法]
    PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]
    PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]
    PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]
  • 原文地址:https://www.cnblogs.com/jsbrml/p/1915170.html
Copyright © 2011-2022 走看看