JavaScript open Word documents

Source: Internet
Author: User

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

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.