1. 程式人生 > >ASP.NET C#如何讀取word,寫入word,複製內容到另一個word文件,批量修改檔名

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){//例項化COMMicrosoft.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;}}