我想使用C#Interop讀取Word文檔並將其填充到DataGrid中。
我試圖通過簡單的控制檯程序在C#中讀取Word文檔。使用C#Interop讀取文檔並將其填充到DataGrid中?
但我是新來的Asp.net,並想知道如何執行該程序。
我可以創建Word文檔,但希望將其填充到DATA網格。
string filePath = null;
OpenFileDialog file = new OpenFileDialog();
file.Title = "Word File";
file.InitialDirectory = "C:\\";
file.RestoreDirectory = true;
// execute if block when dialog result box click ok button
if (file.ShowDialog() == DialogResult.OK) {
// store selected file path
filePath = file.FileName.ToString();
}
try
{
// create word application
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
// create object of missing value
object miss = System.Reflection.Missing.Value;
// create object of selected file path
object path = filePath;
// set file path mode
object readOnly = false;
// open document
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss,ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
docs.ActiveWindow.Selection.WholeStory();
docs.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
//populate data into Datagridview?
docs.Close(ref miss, ref miss, ref miss);
你嘗試過什麼到目前爲止?如果是這樣,請將其納入您的問題中。 –
@TomFenech我已經包含上面的代碼。現在我想單詞文檔內容顯示在DATAGRID.How要完成? – user3383270