JavaScript to open the implementation code for Word documents (C #) _javascript tips

Source: Internet
Author: User
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! ~
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.