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();
            } 
        }
    }
  • 相关阅读:
    Android Gradle使用总结
    Jenkins实现Android自动化打包
    RxJava 2.x 使用最佳实践
    Android 路由框架ARouter最佳实践
    Android 加载GIF图最佳实践
    Java 锁机制 synchronized
    Fiddler抓包使用教程-断点调试
    Fiddler抓包使用教程-Android应用抓包
    Fiddler抓包使用教程-QuickExec
    Fiddler抓包使用教程-模拟低速网络环境
  • 原文地址:https://www.cnblogs.com/lgmbk/p/5057535.html
Copyright © 2011-2022 走看看