C#读写word,文段及表格
生活随笔
收集整理的這篇文章主要介紹了
C#读写word,文段及表格
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Microsoft.Office.Interop.Word.dll程序集,對word文件進行讀寫操作。
1.添加Microsoft.Office.Interop.Word.dll引用
依據visualstudio IDE中是否能在已安裝目錄中找到該dll文件,分為兩種不同操作情況
有:https://blog.csdn.net/u010771437/article/details/41284859
無:使用Nuget添加,https://www.cnblogs.com/qianlifeng/archive/2011/04/22/2024920.html
2.得到word操作程序
Word.Application app = new Microsoft.Office.Interop.Word.Application();//Word.Document doc = null;//object unknow = Type.Missing;app.Visible = false;//打開word時,不彈出word文件顯示String filePath = getFile();//object file = filePath;doc = app.Documents.Open(ref file,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);//獲取文檔操作對象3.通過doc的方法、變量、屬性就可以讀寫word文檔
例:string line = doc.Paragraphs[1].Range.Text.Trim();int i = line.IndexOf("請");order.number = line.Substring(0, i).Trim();order.startTime = line.Substring(line.Length - 10, 10).Trim();Microsoft.Office.Interop.Word.Table nowTable = doc.Tables[1];string name = nowTable.Cell(5, 2).Range.Text.Trim();//投訴人姓名4.關掉通道,清空內存。
doc.Close(ref unknow, ref unknow, ref unknow);app.Quit(ref unknow, ref unknow, ref unknow);ps:寫到這,感覺我每天能做的還是好少啊,既想這,又想那,最后可能一無所獲。
所以,接下來,我最需要做的依然是明確目標。其次,好好準備公考;再其次,提升技術。
提升技術應該還是以需求量較大的java后端為重。
總結
以上是生活随笔為你收集整理的C#读写word,文段及表格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#中namespace、assembl
- 下一篇: c#简单记事本应用程序的快捷方式_Win