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!
  • 相关阅读:
    Codeforces 765 E. Tree Folding
    Codeforces 617 E. XOR and Favorite Number
    2017.3.4[hihocoder#1403]后缀数组一·重复旋律
    2017.2.23[hdu1814]Peaceful Commission(2-SAT)
    2017.2.18Codeforces Round #398 (Div. 2)
    2017.2.18[codevs1170]NOIP2008提高组复赛T4双栈排序
    2017.2.18[codevs3319][bzoj3670]NOI2014D2T1动物园
    2017.2.18[codevs3311][bzoj3668]NOI2014D1T1起床困难综合症
    2017.2.10 Splay总结
    2017.2.10考试总结2017冬令营
  • 原文地址:https://www.cnblogs.com/jsbrml/p/1915170.html
Copyright © 2011-2022 走看看