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!
  • 相关阅读:
    原生js大总结十一
    jQuery快速入门知识重点
    原生js大总结九
    原生js大总结十
    原生js大总结八
    原生js大总结六
    原生js大总结七
    原生js大总结四
    原生js大总结五
    移动端适配
  • 原文地址:https://www.cnblogs.com/jsbrml/p/1915170.html
Copyright © 2011-2022 走看看