数据库下载word预览功能的研究
2024-08-21 01:01:53
本文参考了这里的一些方法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();
}
}
}
最新文章
- SQLite部署-无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块
- OO.A.D.P
- PhpSms 稳定可靠的php短信发送库
- HowTo Perform the spatial selection 'Share a line segment with' using ArcObjects
- Asp.Net Web API 2第四课——HttpClient消息处理器
- 一、Linux目录结构
- ***Java多线程发展简史
- Action<;>;和Func<;>;区别
- BZOJ 2300: [HAOI2011]防线修建( 动态凸包 )
- ABP分层设计
- ps-修复
- Java开发笔记(五十三)关键字final的用法
- tcpdump我的交叉编译(mips)
- ORA-20011 ORA-29913 KUP-11024 GATHER_TABLE_STATS
- shell脚本--CGI获取请求数据(GET / POST)
- BZOJ 4767 两双手
- 001 Spark的简介以及入门
- 关于lis的方案数
- [Proposal]Nano-Diary(纳日记)
- 尝试利用slmail的漏洞来getshell