zoukankan      html  css  js  c++  java
  • 数据库下载word预览功能的研究

    本文参考了这里的一些方法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();
            } 
        }
    }
  • 相关阅读:
    思考:如何保证服务稳定性?
    svn:Item is out of date解决办法
    MAC OS 10.15 Lucene 源码分析环境搭建
    防止数据重复提交的6种方法(超简单)!
    6种快速统计代码执行时间的方法,真香!
    漫画:Integer 竟然有 6 种比较方式?
    IDEA 不为人知的 5 个骚技巧!真香!
    自由职业半年之后,我又滚回职场了...
    为什么建议你使用枚举?
    ESP8266
  • 原文地址:https://www.cnblogs.com/lgmbk/p/5057535.html
Copyright © 2011-2022 走看看