我想用c#实现对word的操作。我对流的概念比较模糊,那位大侠能帖出一段对word保存以及读取的代码,让我研究以下,不胜感激

解决方案 »

  1.   

    自己看看,并实验一下吧。追加文件(文件不存在创建)public void WriteFile(string users,string content)
    {
    try
    {
    string path=@"bin\leaveword.log";
    string mDate=DateTime.Now.ToShortDateString();
    if(!File.Exists(Server.MapPath(path)))
    {
    StreamWriter SW=File.CreateText(Server.MapPath(path));
    SW.WriteLine(users+"\t"+mDate+"\t"+content);
    SW.Close(); }
    else
    {
    StreamWriter SW=File.AppendText(Server.MapPath(path));
    SW.WriteLine(users+"\t"+mDate+"\t"+content);
    SW.Close();
    }
    }
    catch(Exception ee)
    {
    throw new Exception(ee.Message);
    }
    }创建文件
    public void CreateFile()
    {
    string path="";
    try
    {
    if(!File.Exists(path))
    {
    FileStream FS=File.Create(path);
    Byte[] info={0,0,0,0,0,0,};
    FS.Write(info,0,info.Length);
    FS.Close();
    }
    }
    catch(Exception ee)
    { }
    }
    拷贝文件
    public void CopyFile()
    {
    string path="";
    try
    {
    if(!File.Exists(orgin_path))
    {
    return;
    }
    if(File.Exists(object_path))
    {
    return;
    }
                    File.Copy(orgin_path,object_path);
    }
    catch(Exception ee)
    {
    }
    }移动文件
    public void MoveFile()
    {
    string path="";
    try
    {
    if(!File.Exists(orgin_path))
    {
    return;
    }
    if(File.Exists(object_path))
    {
    return;
    }
                    File.Move(orgin_path,object_path);
    }
    catch(Exception ee)
    {
    }
    }删除文件public void DeleteFile()
    {
    string path="";
    try
    {
    if(!File.Exists(path))
    {
    return;
    }

                    File.Delete(orgin_path,object_path);
    }
    catch(Exception ee)
    {
    }
    }读取文件信息public void FileInfo()
    {
    string path="";
    FileInfo FI=new FileInfo(path);
    try
    {
    if(!FI.Exists)
    {
    return;
    }
        FI.DirectoryName;//文件路径
    FI.Length;//文件大小
    FI.CreationTime;//创建时间
                    FI.LastWriteTime;//最后修改时间
    FI.Attributes;//文件属性
    }
    catch(Exception ee)
    {
    }
    }
    创建文件夹
    public void CreateDirectory()
    {
    string path="";
    try
    {
    if(Directory.Exists(path))
    {
    return;
    }
    DirectoryInfo DirInfo=Directory.CreateDirectory(path);
    Directory.GetCreationTime(path);//创建时间
    }
    catch(Exception ee)
    {}
    }移动文件夹
    public void MoveDirectory()
    {
    string path="";
    try
    {
    if(!Directory.Exists(source_path))
    {
    return;
    }
    if(Directory.Exists(object_path))
    {
    return;
    }
    Directory.Move(source_path,object_path); }
    catch(Exception ee)
    {}
    }删除文件夹
    public void MoveDirectory()
    {
    string path="";
    try
    {
    if(!Directory.Exists(path))
    {
    return;
    }

    Directory.Delete(path); }
    catch(Exception ee)
    {}
    }
      

  2.   

    Process
    FileInfo
    DirectoryInfo
      

  3.   


                FileInfo fifOut;
               // FileStream fsOut = new FileStream();
                ApplicationClass acsOut = new ApplicationClass();
                object strPash = @"d:\my.doc";            object Nothing=System.Reflection.Missing.Value;
                Word.Document dctOut;            dctOut =acsOut.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);            dctOut.Paragraphs .Last .Range .Text ="aaaaaa";
                dctOut.Paragraphs.First.Range.Text = "bbbb";
                //bool blFlag = File.Exists(strPash .ToString ());
                //if (blFlag)
                //{
                //    File.Delete(strPash.ToString());
                //} 
              
                dctOut.SaveAs(ref strPash ,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 ,ref Nothing );
                  
                dctOut .Close (ref Nothing ,ref Nothing ,ref Nothing );
                acsOut .Quit (ref Nothing ,ref Nothing ,ref Nothing );