zoukankan      html  css  js  c++  java
  • .net操作读取word中的图像并保存

    最近在编写一个在线考试系统,客户要求能导入word格式的题库,并且该题库还包含图像;

    由于以前一直没接触过office编程,在网上查找了c#操作word基本代码;

    其中读取word中的图像并保存的思路及代码主要包括两种:

    第一种:把word文档保存为html文档,然后再对提取出来的图片进行下一步处理,代码片段如下:

    Microsoft.Office.Interop.Word._Application oWord = new Word.Application();

    object oReadOnly = false;

    object oMissing = System.Reflection.Missing.Value;
     Word._Application oWord = new Word.Application();
    oWord.Visible = false;
    //打开word文档
    Word.Document oDoc = oWord.Documents.Open(ref oFileName, ref oMissing,

      ref oReadOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    oDoc.saveAs(ref osaveFileName, ref Word.WdSaveFormat.wdFormatHTML,

      ref oReadOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    /*以html格式保存该word文档后,对图像文件进行处理,此处省略*/

    oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
    Marshal.ReleaseComObject(oDoc);
    Marshal.FinalReleaseComObject(oWord);
    oWord = null;

    第二种:

    foreach(Word.InlineShape inlineShape in oDoc.InlineShapes) {

      inlineShape.Select();
      oWord.Selection.Copy();
      System.Drawing.Image image = System.Windows.Forms.Clipboard.GetImage();
      image.Save(savePath + user.UserName + ".jpg");

    }

    此处省略了异常处理机制,在具体使用时请自行加上

    在forms程序中使用正常,当在web中使用时出现异常,System.Windows.Forms.Clipboard.GetImage();方法获取到的数据为空

    原因是剪贴板只能在客户端使用,在服务器端使用时,必须锁定共享资源剪贴板;

    解决办法为:在使用此方法的aspx页面的page指令行中加入AspCompat="true"

    形式如:<%@ Page Language="C#" AspCompat="true" .............. %>

    至此问题解决。

    在此写下主要是为了防止自己忘记,有写的不对的大家指出,有什么不明白可以留言!

  • 相关阅读:
    SSH 免密登录配置
    Hadoop 2.7.2 集群安装配置
    将oracle数据库中数据写入excel文件
    PLSQL提交请求集
    oracle会计工作总结,EBS 创建会计科目 小结
    将oracle数据库中数据写入excel文件
    如何成为一名优秀的工程师(听讲座有感) Winema
    基于W5300的嵌入式以太网接口设计 Winema
    Java 基础入门随笔(2) JavaSE版——关键字、进制转换、类型转换
    VMware 11安装Mac OS X 10.10 及安装Mac Vmware Tools.
  • 原文地址:https://www.cnblogs.com/aDust/p/1764822.html
Copyright © 2011-2022 走看看