• 读写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!
  • 相关阅读:
    宿主机无法访问CentOS7上Jenkins服务的解决办法
    415. Add Strings
    367. Valid Perfect Square
    326. Power of Three
    258. Add Digits
    231. Power of Two
    204. Count Primes
    202. Happy Number
    172. Factorial Trailing Zeroes
    171. Excel Sheet Column Number
  • 原文地址:https://www.cnblogs.com/jsbrml/p/1915170.html
Copyright © 2020-2023  润新知