本文参考了这里的一些方法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();
}
}
}

最新文章

  1. SQLite部署-无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块
  2. OO.A.D.P
  3. PhpSms 稳定可靠的php短信发送库
  4. HowTo Perform the spatial selection 'Share a line segment with' using ArcObjects
  5. Asp.Net Web API 2第四课——HttpClient消息处理器
  6. 一、Linux目录结构
  7. ***Java多线程发展简史
  8. Action<>和Func<>区别
  9. BZOJ 2300: [HAOI2011]防线修建( 动态凸包 )
  10. ABP分层设计
  11. ps-修复
  12. Java开发笔记(五十三)关键字final的用法
  13. tcpdump我的交叉编译(mips)
  14. ORA-20011 ORA-29913 KUP-11024 GATHER_TABLE_STATS
  15. shell脚本--CGI获取请求数据(GET / POST)
  16. BZOJ 4767 两双手
  17. 001 Spark的简介以及入门
  18. 关于lis的方案数
  19. [Proposal]Nano-Diary(纳日记)
  20. 尝试利用slmail的漏洞来getshell

热门文章

  1. sql server数据建表
  2. Unix/Linux环境C编程入门教程(42) 终端控制篇
  3. SNMP配置和常用命令OID(转)
  4. Search in Rotated Sorted Array (I, II) 解答
  5. 修改Fedora 20 启动项
  6. HDOJ-1013 Digital Roots
  7. 【Cocos2d-X游戏实战开发】捕鱼达人之游戏场景的创建(六)
  8. ListView之ArrayAdapter
  9. root密码忘记后如何修改
  10. PC--CSS常识