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

首先要添加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;



publicpartialclassMore:System.Web.UI.Page
{
   
protectedvoidPage_Load(object sender,EventArgs e)
   
{

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

       
//针对当前目录建立目录引用对象
       
DirectoryInfo dirInfo =newDirectoryInfo(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>
   
publicvoidCopyWordToOther(string tempath,string filepath)
   
{
       
Document doc1 =newDocument(tempath);
       
DocumentBuilder builder1 =newDocumentBuilder(doc1);
       
Document doc2 =newDocument(filepath);
       
DocumentBuilder builder2 =newDocumentBuilder(doc2);
       
Section sec1 = builder1.CurrentSection;
        builder2
.CurrentSection.AppendContent(sec1);
        doc2
.Save(filepath);
   
}

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

   
/// <summary>
   
/// 读取word内容
   
/// </summary>
   
/// <param name="docpath">word文档路径</param>
   
/// <returns></returns>
   
publicstringDoc2Text(string docpath)
   
{
       
//实例化COM
       
Microsoft.Office.Interop.Word.ApplicationClass wordApp =newMicrosoft.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;
   
}
}

 

转载于:https://www.cnblogs.com/lema/archive/2012/10/10/2718590.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值