Opening a Word document in C # is not too difficult, and there are many methods.
Method for opening a Word document in I. C #
// Add a reference to Microsoft Word 11.0 object library to the project reference
Private void button#click (object sender, System. EventArgs e)
{
// Call the open file dialog box to obtain the WORD file, RTF file, and text file path name of the file to be opened.
OpenFileDialog opd = new OpenFileDialog ();
Opd. InitialDirectory = \ "c :\\\\\";
Opd. filter = \ "Word Document (*. doc) | *. doc | Text Document (*. txt) | *. txt | RTF document (*. rtf) | *. rtf | all documents (*. *) | *. *\";
Opd. FilterIndex = 1;
If (opd. ShowDialog () = DialogResult. OK & opd. FileName. Length> 0)
{
// Create an instance of the Word class. disadvantage: the display of tables, images, and so on cannot be read correctly.
Word. ApplicationClass app = new Word. ApplicationClass ();
Word. Document doc = null;
Object missing = System. Reflection. Missing. Value;
Object FileName = opd. FileName;
Object readOnly = false;
Object isVisible = true;
Object index = 0;
Try
{
Doc = app. Documents. Open (ref FileName, ref missing, ref readOnly,
Ref missing,
Ref missing, ref isVisible, ref missing,
Ref missing, ref missing, ref missing );
Doc. ActiveWindow. Selection. WholeStory ();
Doc. ActiveWindow. Selection. Copy ();
// Obtain data from the clipboard
IDataObject data = Clipboard. GetDataObject ();
This. richTextBox1.Text = data. GetData (DataFormats. Text). ToString ();
}
Finally
{
If (doc! = Null)
{
Doc. Close (ref missing, ref missing, ref missing );
Doc = null;
}
If (app! = Null)
{
App. Quit (ref missing, ref missing, ref missing );
App = null; [Page]
}
}
}
}
But how can we use javascript to open it? In fact, it is not difficult.
2. Open the Word document www.2cto.com in javascript.
Create an html file and write a FileUpLoad and button control.
<Input id = "flUpload" type = "file"/> flUpload
<Input id = "btnOpenFile" type = "button" value = "button" onclick = "OpenFile ()"/>
Then, write a javascript OpenFile method.
Function OpenFile ()
{
If (document. getElementById ("flUpload"). value. toUpperCase (). indexOf (". XLS ")! =-1)
{
Var objExcel;
ObjExcel = new ActiveXObject ("Excel. Application ");
ObjExcel. Visible = true;
ObjExcel. Workbooks. Open (document. getElementById ("flUpload"). value );
}
Else if (document. getElementById ("flUpload"). value. toUpperCase (). indexOf (". DOC ")! =-1)
{
Var objDoc;
ObjDoc = new ActiveXObject ("Word. Application ");
ObjDoc. Visible = true;
ObjDoc. Documents. Open (document. getElementById ("flUpload"). value );
}
Else
{
Alert ("Please select Word/Excel file only ");
Return false;
}
}
OK. Then you can select a doc file in IE and click open to open the file.
Hope to help you.
Haha !~.
Author: Lanny ☆landong Cai