首页 .NET/Web ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名

ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名

作者:胡同里的砖头 围观群众:1745 更新于:2013-12-19

首先要添加COM引用
Microsoft word 11.0 Object Library.


然后添加.NET引用
Microsoft.Office.Interop.Word.dll

下载Aspose.Words引用
Aspose.Words.dll

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
//复制到另一个doc
using Aspose.Words;
//读取引用
using Microsoft.Office.Core;
using Microsoft.Office.Interop;
//写入引用
using System.Reflection;
using MSWord = Microsoft.Office.Interop.Word;



public partial class More : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //批量改文件名
    protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        string path=string.Format("File/");
        String strField = Server.MapPath(string.Format("~/{0}",path));
        

        //针对当前目录建立目录引用对象
        DirectoryInfo dirInfo = new DirectoryInfo(strField);
        int count=1;
        foreach (FileInfo fi in dirInfo.GetFiles())
        {
            string ext=fi.Name.Substring(fi.Name.LastIndexOf('.'));
            File.Move(MapPath("~/File/" + fi.Name), MapPath("~/File/" + count.ToString() + ext));
            count++;
        }
    }
    /// <summary>
    /// 复制doc文档里面的内容到另一个doc文件之中,需要下载Aspose.Words.dll控件
    /// </summary>
    /// <param name="tempath">复制的doc文档路径</param>
    /// <param name="filepath">要复制到的doc文档路径</param>
    public void CopyWordToOther(string tempath,string filepath)
    {
        Document doc1 = new Document(tempath);
        DocumentBuilder builder1 = new DocumentBuilder(doc1);
        Document doc2 = new Document(filepath);
        DocumentBuilder builder2 = new DocumentBuilder(doc2);
        Section sec1 = builder1.CurrentSection;
        builder2.CurrentSection.AppendContent(sec1);
        doc2.Save(filepath); 
    }

    /// <summary>
    /// 写入word
    /// </summary>
    public void WordWrite()
    {
        object path;//文件路径变量
        string strContent;//文本内容变量
        MSWord.Application wordApp;//Word应用程序变量
        MSWord.Document wordDoc;//Word文档变量
        path = MapPath("~/File/test.doc");//保存路径
        wordApp = new MSWord.ApplicationClass(); //初始化
        //如果已存在,则删除
        if (File.Exists((string)path))
        {
            File.Delete((string)path);
        }
        //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
        Object Nothing = Missing.Value;
        wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
        strContent = "doc内容";//内容
        wordDoc.Paragraphs.Last.Range.Text = strContent;
        //WdSaveFormat为Word 2007文档的保存格式
        object format = MSWord.WdSaveFormat.wdFormatDocument;
        //将wordDoc文档对象的内容保存为DOCX文档
        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);
        //关闭wordDoc文档对象
        wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
        //关闭wordApp组件对象
        wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        Console.WriteLine(path + " 创建完毕!");
    }

    /// <summary>
    /// 读取word内容
    /// </summary>
    /// <param name="docpath">word文档路径</param>
    /// <returns></returns>
    public string Doc2Text(string docpath)
    {
        //实例化COM
        Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
        object fileobj = docpath;
        object nullobj = System.Reflection.Missing.Value;
        //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
        Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
        );
        //取得doc文件中的文本
        string outText = doc.Content.Text;
        //关闭文件
        doc.Close(ref nullobj, ref nullobj, ref nullobj);
        //关闭COM
        wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
        //返回
        return outText;
    }
}

 

  • 本文标题: ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8