注:轉載請標明文章原始出處及作者信息
aspose.word 插件下載
鏈接:https://pan.baidu.com/s/1aAAE8LRULH-uZXS_TaN9Bw 提取碼:z9wf
使用原因:無需安裝office,無兼容性問題,破解版有一定限制
代碼:
public ParagraphCollection WordParagraphs(string fileName) { Document doc = new Document(fileName); if (doc.FirstSection.Body.Paragraphs.Count > 0) { return doc.FirstSection.Body.Paragraphs;//word中的所有段落 } return null; }
public string GetWordParagraphs(int index) { var phs = _word.WordParagraphs(Server.MapPath(model.WordFpath)); return phs[i].GetText(); }
附上使用Microsoft.Office.Interop.Word 讀取段落(操作靈活但兼容性未解決)
public class WordHelper { private _Document doc = null; public WordHelper() { objApp = new Application(); } public void Open(string fileName) { object objDoc = fileName; object objMissing = Missing.Value; doc = objApp.Documents.Open(ref objDoc); doc.Activate(); } public Paragraphs GetParagraphs() { return objApp.ActiveDocument.Paragraphs; } public string GetParagraphs(int index) { return objApp.ActiveDocument.Paragraphs[index].Range.Text; } public void Close() { objApp.ActiveDocument.Close(); objApp.Quit(); } }
