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!
  • 相关阅读:
    数组返回NULL绕过
    69.x的平方根
    1277.统计全为1的正方形子矩形
    221.最大正方形
    572.另一个树的子树
    983.最低票价
    98.验证二叉排序树
    53.最大子序和
    5386.检查一个字符串是否可以打破另一个字符串
    5385.改变一个整数能得到的最大差值
  • 原文地址:https://www.cnblogs.com/jsbrml/p/1915170.html
Copyright © 2011-2022 走看看