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