Use data streams to read Word documents online

Source: Internet
Author: User
Friends who often write blogs or space diaries are familiar with the network Editor (1, The csdn blog Editor) and are easy to make brilliant typographical la S. However, in this BS project, the customer has been using the word in the office software for editing, and has never used this tool. It is a stranger. "How to set the line spacing here ?" -- I can't touch my mind instantly. In the original version of the system, when the customer publishes information, simply use the screen tool from the word, and then stick it to the editor, the effect can be imagined. Later I thought about it. As I used to use Word, why not upload the word and preview it online?
Figure 1I checked the information and found that it was not difficult. I mainly converted word into HTML format, and then read and displayed it through the data stream!
Note:Add a reference to "Microsoft. Office. InterOP. Word"

The code implementation is as follows:
Using system; using system. collections. generic; using system. io; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using word = Microsoft. office. interOP. word; namespace testwordonline {public partial class Demo: system. web. UI. page {protected void page_load (Object sender, eventargs e) {string filename = wordtohtml ("F: \ test .doc "); // read the character streamreader fread = new streamreader (filename, system. text. encoding. getencoding ("gb2312"); string Ss = fread. readtoend (); // print the data response. write (SS); fread. close (); fread. dispose ();} /// <summary> /// convert Word to HTML /// </Summary> /// <Param name = "wordfilename"> </param> private string wordtohtml (Object wordfilename) {// put the user code here to initialize the page word. application word = new word. application (); Type wordtype = word. getType (); word. required ents docs = word. documents; // open the file type docstype = docs. getType (); word. document Doc = (word. document) docstype. invokemember ("open", system. reflection. bindingflags. invokemethod, null, Docs, new object [] {wordfilename, true, true}); // convert the format and save it as type doctype = Doc. getType (); string wordsavefilename = wordfilename. tostring (); string strsavefilename = wordsavefilename. substring (0, wordsavefilename. length-3) + "html"; object savefilename = (object) strsavefilename; doctype. invokemember ("saveas", system. reflection. bindingflags. invokemethod, null, Doc, new object [] {savefilename, word. wdsaveformat. wdformatfilteredhtml}); doctype. invokemember ("close", system. reflection. bindingflags. invokemethod, null, Doc, null); // exit word wordtype. invokemember ("quit", system. reflection. bindingflags. invokemethod, null, word, null); Return savefilename. tostring ();}}}
Let's look at the effect:

Figure 3-word Edition


Figure 4-webpage version


Is the function implementation quite simple?We are used to this kind of network programmer and impose this kind of habit on users. The program is intended for users. It is the starting point of every IT personnel only to make users feel comfortable.

Use data streams to read Word documents online

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.