本文参考了这里的一些方法http://tobetobe.blog.51cto.com/1392243/354420

一直想通过缓存来实现,奈何技术不够,走了曲线救国的思路,先下载,然后预览,删除下载文件。好吧主要给自己做个备忘。

注意

1.关闭前清空剪切板,否则会提示删除文件失败或者打开word预览是弹提示说已占用。

2.打开新预览之前要sleep一段时间确保word完全退出

3.粘贴时需要sleep一段时间确保粘贴完成

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word; namespace WordTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{ if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) {
return;
}
string str = openFileDialog1.FileName;//这里用选择文件的形式模拟下载的文件
object sorceDocPath = str;//下载文件的路径 #region 打开word 复制内容
object readOnly = false;
object isVisible = false;
Object Nothing = System.Reflection.Missing.Value;
object objDocType = WdDocumentType.wdTypeDocument;
object type = WdBreakType.wdSectionBreakContinuous;
Microsoft.Office.Interop.Word.Application wordApp1 = new Microsoft.Office.Interop.Word.ApplicationClass();
Document openWord;
openWord = wordApp1.Documents.Open(ref sorceDocPath, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
openWord.Select();
openWord.Sections[].Range.Copy();
#endregion #region 打开word 粘贴内容 显示
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document newWordDoc = wordApp.Documents.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
newWordDoc.Sections[].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
System.Threading.Thread.Sleep();
Clipboard.Clear();
openWord.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp1.Quit(ref Nothing, ref Nothing, ref Nothing);
wordApp.Visible = true;
#endregion
System.Threading.Thread.Sleep();
System.IO.File.Delete(str);//测试的时候要注意 这里会把源文件删除!!
GC.Collect();
}
}
}

数据库下载word预览功能的研究的更多相关文章

  1. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  2. 分离与继承的思想实现图片上传后的预览功能:ImageUploadView

    本文要介绍的是网页中常见的图片上传后直接在页面生成小图预览的实现思路,考虑到该功能有一定的适用性,于是把相关的逻辑封装成了一个ImageUploadView组件,实际使用效果可查看下一段的git效果图 ...

  3. 用原生JS实现多张图片上传及预览功能(兼容IE8)

    最近需要做一个图片上传预览的功能(兼容IE8-11.chrome.firefox等浏览器),网上现有的文件上传组件(如webuploader)总是会遇到一些兼容性问题.于是我参考了一些博文(链接找不到 ...

  4. JavaScript实现本地图片上传预览功能(兼容IE、chrome、FF)

    需要解决的问题有:本地图片如何在上传前预览.编辑:最近发现这个功能很多是基于flash实现的,很多JavaScript实现的代码兼容性都很差,特别是在IE和firefox和chrome三个浏览器上不兼 ...

  5. C#实现打印与打印预览功能

    C#实现打印与打印预览功能的思路及代码. 在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的 ...

  6. nodejs实现本地上传图片并预览功能(express4.0+)

    Express为:4.13.1  multyparty: 4.1.2 代码主要实现本地图片上传到nodejs服务器的文件下,通过取图片路径进行图片预览 写在前面:计划实现图片上传预览功能,但是本地图片 ...

  7. word预览

    word+excle表格在线浏览 word.ppt.xls文件实现在线预览的方式比较简单可以直接通过调用微软的在线预览功能实现 (预览前提:资源必须是公共可访问的) 通过iframe直接引用微软提供的 ...

  8. Java实现在线预览功能

    java实现在线预览功能,需要用到  jacob.dll jacob.jar   预览pdf所需js  pdfobject.min.js 将上传文件转为pdf保存. <div class=&qu ...

  9. JS通过使用PDFJS实现基于文件流的预览功能

    需求: 使用JS实现PDF文件预览功能 备选方案: 使用ViewerJS,官网  http://viewerjs.org/ 使用PDFJS,官网  https://mozilla.github.io/ ...

随机推荐

  1. shell编程while

    脚本编程:    顺序结构    选择结构        if        case    循环结构        for        while        until        whil ...

  2. iOS 按住Home键的代理

    1.按住home键怎么保存程序的运行状态: 监听按home键的时候程序挂起的状态(通知),然后让其重新进入程序[[NSNotificationCenter defaultCenter] addObse ...

  3. ERROR 1130: Host is not allowed to connect to this MySQL server

    解决远程连接mysql错误1130代码的方法 今天在用远程连接Mysql服务器的数据库,不管怎么弄都是连接不到,错误代码是1130,ERROR 1130: Host 192.168.2.159 is ...

  4. css-下拉菜单案例

    <!DOCTYPE html>CSS4-布局2-display下拉菜单案例 <style>.xiala{width:200px;background:#ddd;}.xiala ...

  5. jquery 底部导航透明度变化

    如果滚动条到达底部,footer-nav 的透明度为1,否则为0.8: html <div class="footer-nav" id="footer"& ...

  6. Linux中Firefox——Firebug插件安装及使用

    Firebug的安装方法即打开方式同httpfox Firebug使用指南: Firebug可以随时编辑页面:在HTML标签中,点击窗口上方的"inspect"命令,然后再选择页面 ...

  7. uva 10935 throwing cards away <queue>

    Given is an ordered deck of    n    cards numbered 1 to    n    with card 1 at the top and card    n ...

  8. (转)ubuntu 文件目录结构

    文件系统的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 1. 普通文件:如文本文件.C语言元代码.SHELL脚本.二进制的可执行文件等,可用 ...

  9. 飘逸的python - 多条件排序及itemgetter的应用

    曾经客户端的同事用as写一大堆代码来排序,在得知python排序往往只需要一行,惊讶无比,遂对python产生浓厚的兴趣. 之前在做足球的积分榜的时候需要用到多条件排序,如果积分相同,则按净胜球,再相 ...

  10. 实习小白笔记一(鼠标悬停、获取多选、提交修改、layer页面、单元格文字长度、json、分页、左连接)

    ①easyui 当鼠标悬停显示单元格信息: $(this).datagrid('doCellTip',{'max-width':'600px','delay':300}); ②jquery 获取che ...