Research on the database download word preview function

Source: Internet
Author: User

This article refers to some of the methods here http://tobetobe.blog.51cto.com/1392243/354420

Always want to achieve through the cache, but the technology is not enough, walked the idea of saving the curve, first download, then preview, delete the download file. All right, make a memo for yourself.

Attention

1. Empty the Clipboard before closing, or you will be prompted to delete the file or open Word Preview is a pop-up saying that it is occupied.

2. Sleep for a while before opening a new preview to make sure word exits completely

3. Need sleep for some time to paste to ensure that the paste is complete

Code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Drawing;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMicrosoft.Office.Interop.Word;namespacewordtest{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            if(Openfiledialog1.showdialog ()! =System.Windows.Forms.DialogResult.OK) {return; }            stringstr = openfiledialog1.filename;//this simulates the downloaded file in the form of a select file.            ObjectSorcedocpath = str;//path to the download file            #regionOpen Word to copy contentObjectReadOnly =false; ObjectIsVisible =false; Object Nothing=System.Reflection.Missing.Value; ObjectObjdoctype =wddocumenttype.wdtypedocument; ObjectType =wdbreaktype.wdsectionbreakcontinuous; Microsoft.Office.Interop.Word.Application WordApp1=NewMicrosoft.Office.Interop.Word.ApplicationClass ();            Document Openword; Openword= WordApp1.Documents.Open (refSorcedocpath,refNothing,refReadOnly,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refIsVisible,refNothing,refNothing,refNothing,refNothing );            Openword.select (); openword.sections[1].            Range.Copy (); #endregion            #regionOpen Word paste content displayMicrosoft.Office.Interop.Word.Application WordApp=NewMicrosoft.Office.Interop.Word.ApplicationClass (); Microsoft.Office.Interop.Word.Document Newworddoc=wordApp.Documents.Add (Missing.Value, Missing.Value, Missing.Value, Missing.Value); newworddoc.sections[1].            Range.pasteandformat (Wdrecoverytype.wdpastedefault); System.Threading.Thread.Sleep ( -);            Clipboard.clear (); Openword.close (refNothing,refNothing,refNothing ); Wordapp1.quit (refNothing,refNothing,refNothing ); Wordapp.visible=true; #endregionSystem.Threading.Thread.Sleep ( -); System.IO.File.Delete (str);//when testing the time to note that the source files will be deleted!! GC.        Collect (); }     }}

Research on the database download word preview function

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.