Opening a Word document in C # is not too difficult, and there are more ways.
A. Open Word document method in C #
Copy Code code as follows:
Add a reference to the Microsoft Word 11.0 Object Library in the project reference
private void Button1_Click (object sender, System.EventArgs e)
{
Call Open File dialog box to get open file Word file, RTF file, text file path name
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 a word class, disadvantage: The display of tables, pictures, etc. can not 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 missing, ref missing, ref missing,
Ref missing, ref missing, ref missing, ref isVisible, ref missing,
Ref missing, ref missing, ref missing);
Doc. ActiveWindow.Selection.WholeStory ();
Doc. ActiveWindow.Selection.Copy ();
Getting 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 what if we use JavaScript to open it? In fact, it is not difficult.
two. Open a Word document in JavaScript
We create a new HTML file and write a FileUpload and button control.
Copy Code code as follows:
<input id= "flupload" type= "file"/>flupload
<input id= "btnopenfile" type= "button" value= "button" onclick= "OpenFile" () "/>"
Then, write a JavaScript OpenFile method.
Copy Code code as follows:
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 in IE, you can select a Doc document, then click Open, it can be opened.
I hope it will help you.
Oh! ~