首先添加引用,解決方案資源管理器-》引用-》添加-》Com-》瀏覽-》C:\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB 我使用的是office 2003其他版本我不太清楚,.net會(huì)自動(dòng)把OLB控件轉(zhuǎn)換成DLL文件
使用方法:
object oMissing = System.Reflection.Missing.Value;
Word.Application oWord =new Word.Application();
oWord.Visible = false;//設(shè)置Word應(yīng)用程序?yàn)椴豢梢?jiàn)
//新建一個(gè)Word文檔
Word.Document oDoc=oWord.Documents.Add(ref oMissing,ref oMissing ,ref oMissing,ref oMissing);
//文檔內(nèi)容的復(fù)制與粘貼
oDoc.Content.Copy();
oDoc.Content.Paste()
//文檔的另存為
oDoc.SaveAs(ref fileName,ref saveFormat,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing);
//其他設(shè)置
oDoc.PageSetup.PaperSize=Word.WdPaperSize.wdPaperA3;//頁(yè)面設(shè)置
oDoc.PageSetup.Orientation=Word.WdOrientation.wdOrientLandscape;//橫板還是豎板
oDoc.PageSetup.TextColumns.SetCount(2);//分欄
//關(guān)閉Word
oWord.Application.Quit(ref b,ref oMissing,ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
通過(guò)oDoc對(duì)象對(duì)Word文檔進(jìn)行操作(word能做的它都能做)進(jìn)行操作里面有很多函數(shù),有興趣的自己研究
另一方法:
1:對(duì)項(xiàng)目添加引用,Microsoft Word 11.0 Object Library
2:在程序中添加 using Word = Microsoft.Office.Interop.Word;
3:程序中添加
Word.Application app = new Microsoft.Office.Interop.Word.Application(); //可以打開(kāi)word程序
Word.Document doc = null; //一會(huì)要記錄word打開(kāi)的文檔
word文檔和word程序可不是一回事奧!
4:一般來(lái)說(shuō),對(duì)于抽取word內(nèi)容,用的方法很少
public override void openFile(object fileName){} //打開(kāi)文檔
public override object readPar(int i){} //讀取word文檔的第i段
public override int getParCount(){} //返回word文檔一共幾段
public override void closeFile(){} //關(guān)閉文檔
public override void quit(){} //關(guān)閉word程序
//從網(wǎng)頁(yè)上拷貝的目錄有時(shí)候會(huì)出現(xiàn)手動(dòng)換行符^l,,先將其換成回車段落標(biāo)記,才能正確讀取
public void replaceChar(){}
5:代碼
public override void openFile(object fileName)
...{
try
...{
if (app.Documents.Count > 0)
...{
if (MessageBox.Show("已經(jīng)打開(kāi)了一個(gè)word文檔,你想關(guān)閉重新打開(kāi)該文檔嗎?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
...{
object unknow = Type.Missing;
doc = app.ActiveDocument;
if (MessageBox.Show("你想保存嗎?", "保存", MessageBoxButtons.YesNo) == DialogResult.Yes)
...{
app.ActiveDocument.Save();
}
app.ActiveDocument.Close(ref unknow, ref unknow, ref unknow);
app.Visible = false;
}
else
...{
return;
}
}
}
catch (Exception)
...{
//MessageBox.Show("您可能關(guān)閉了文檔");
app = new Microsoft.Office.Interop.Word.Application();
}
try
...{
object unknow = Type.Missing;
app.Visible = true;
doc = app.Documents.Open(ref fileName,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
}
catch (Exception ex)
...{
MessageBox.Show("出現(xiàn)錯(cuò)誤:" + ex.ToString());
}
}
public override object readPar(int i)
...{
try
...{
string temp = doc.Paragraphs[i].Range.Text.Trim();
return temp;
}
catch (Exception e) ...{
MessageBox.Show("Error:"+e.ToString());
return null;
}
}
public override int getParCount()
...{
return doc.Paragraphs.Count;
}
public override void closeFile()
...{
try
...{
object unknow = Type.Missing;
object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
app.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
}
catch (Exception ex)
...{
MessageBox.Show("Error:" + ex.ToString());
}
}
public override void quit()
...{
try
...{
object unknow = Type.Missing;
object saveChanges = Word.WdSaveOptions.wdSaveChanges;
app.Quit(ref saveChanges, ref unknow, ref unknow);
}
catch (Exception)
...{
}
}
public void replaceChar() ...{
try
...{
object replaceAll = Word.WdReplace.wdReplaceAll;
object missing = Type.Missing;
app.Selection.Find.ClearFormatting();
app.Selection.Find.Text = "^l";
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.Replacement.Text = "^p";
app.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}
catch (Exception e)
...{
MessageBox.Show("文檔出現(xiàn)錯(cuò)誤,請(qǐng)重新操作");
}
}
6:剛才是用讀取一段做的例子,如果要讀取一句或一篇只需要把doc.Paragraphs[i](readPar中)改成doc.Sentences[i]或doc.content即可,因?yàn)槎际俏④浀臇|東,所以用起來(lái)沒(méi)有一點(diǎn)的障礙,再加上現(xiàn)在的vs2005做的很智能,所以先從java轉(zhuǎn)到了c#上
7:實(shí)際上,c#中讀取word是不用那么麻煩的,但是如果考慮到可能還要抽取txt,ppt等多種格式,所以就寫了一個(gè)抽象類,調(diào)用起來(lái)也方便,這就是為什么我的程序方法開(kāi)頭會(huì)有override的原因,總要考慮到通用,所以多了一些代碼。