码迷,mamicode.com
首页 > 数据库 > 详细

数据库下载word预览功能的研究

时间:2015-12-18 18:39:47      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

本文参考了这里的一些方法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[1].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[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
            System.Threading.Thread.Sleep(100);
            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(500);
            System.IO.File.Delete(str);//测试的时候要注意 这里会把源文件删除!!
            GC.Collect();
        } 
    }
}

 

数据库下载word预览功能的研究

标签:

原文地址:http://www.cnblogs.com/lgmbk/p/5057535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!