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" .............. %>

    至此问题解决。

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

  • 相关阅读:
    Dom对象和jQuery对象区别 jQuery对象转换为Dom对象、、Dom对象转换为jquery对象
    jquery 1,2,3三个版本的下载、区别/以及jquery使用步骤,jQuery入口函数
    2021年3月4日 第一周开课博客
    2021年3月3日
    2021年3月2日
    2021年2月24日 记账本开发07
    2021年2月23日 记账本开发06
    2021年2月22日 记账本开发05
    程序员修炼之道读书笔记03
    2021年2月21日 记账本开发04
  • 原文地址:https://www.cnblogs.com/aDust/p/1764822.html
Copyright © 2011-2022 走看看