一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net c 把图片导入到excel代码

时间:2022-06-25 05:05:34 编辑:袖梨 来源:一聚教程网

asp教程.net c 把图片导入到excel代码
下面是一款利用c#把图片导入到excel实例代码,方便简单哦,如果你正在要把一些图片导入到excel的话正好,管用。
*/

using system;
using system.collections.generic;
using system.text;
using microsoft.office.tools.excel;
using system.windows.forms;
using excel;

namespace serial_port
{
  class insertpicturetoexcel
  {
  public void open()
  {
  this.open(string.empty);
  }

  ///


  /// 功能:实现excel应用程序的打开
  ///

  /// 模板文件物理路径
  public void open(string templatefilepath)
  {
  //打开对象
  m_objexcel = new excel.application();
  m_objexcel.visible = false;
  m_objexcel.displayalerts = false;

  if (m_objexcel.version != "11.0")
  {
   
  messagebox.show("您的 excel 版本不是 11.0 (office 2003),操作可能会出现问题。");
  m_objexcel.quit();
  return;
  }

  m_objbooks = (excel.workbooks)m_objexcel.workbooks;
  if (templatefilepath.equals(string.empty))
  {
  m_objbook = (excel._workbook)(m_objbooks.add(m_objopt));
  }
  else
  {
   
  m_objbook=m_objbooks.open(templatefilepath, m_objopt, m_objopt, m_objopt, m_objopt,m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt);

  }
  m_ob网页特效heets = (excel.sheets)m_objbook.worksheets;
  m_objsheet = (excel._worksheet)(m_objsheets.get_item(1));
   
  m_objexcel.workbookbeforeclose += new excel.appevents_workbookbeforecloseeventhandler(m_objexcel_workbookbeforeclose);
  }

  private void m_objexcel_workbookbeforeclose(excel.workbook m_objbooks, ref bool _cancel)
  {
  messagebox.show("保存完毕!");
  }

  ///


  /// 将图片插入到指定的单元格位置。
  /// 注意:图片必须是绝对物理路径
  ///

  /// 单元格名称,例如:b4
  /// 要插入图片的绝对路径。
  public void insertpicture(string rangename, string picturepath)
  {
  m_objrange = m_objsheet.get_range(rangename, m_objopt);
  m_objrange.select();
  excel.pictures pics = (excel.pictures)m_objsheet.pictures(m_objopt);
  pics.insert(picturepath, m_objopt);
  }

  ///


  /// 将图片插入到指定的单元格位置,并设置图片的宽度和高度。
  /// 注意:图片必须是绝对物理路径
  ///

  /// 单元格名称,例如:b4
  /// 要插入图片的绝对路径。
  /// 插入后,图片在excel中显示的宽度。
  /// 插入后,图片在excel中显示的高度。
  public void insertpicture(string rangename, string picturepath, float pictutewidth, float pictureheight)
  {
   
  m_objrange = m_objsheet.get_range(rangename, m_objopt);
  m_objrange.select();
  float picleft, pictop;
  picleft = convert.tosingle(m_objrange.left);
  pictop = convert.tosingle(m_objrange.top);
  //参数含义:
  //图片路径
  //是否链接到文件
  //图片插入时是否随文档一起保存
  //图片在文档中的坐标位置(单位:points)
  //图片显示的宽度和高度(单位:points)
  //参数详细信息参见:http://msdn2.microsoft.com/zh-cn/library/aa221765(office.11).aspx

  m_objsheet.shapes.addpicture(picturepath,microsoft.office.core.msotristate.msofalse, microsoft.office.core.msotristate.msoctrue, picleft, pictop, pictutewidth, pictureheight);

   
  }

  ///


  /// 将excel文件保存到指定的目录,目录必须事先存在,文件名称不一定要存在。
  ///

  /// 要保存成的文件的全路径。
  public void savefile(string outputfilepath)
  {
   
  m_objbook.saveas(outputfilepath, m_objopt, m_objopt,m_objopt, m_objopt, m_objopt, excel.xlsaveasaccessmode.xlnochange,m_objopt, m_objopt, m_objopt, m_objopt, m_objopt);


  this.close();
  }
  ///


  /// 关闭应用程序
  ///

  private void close()
  {
  m_objbook.close(false, m_objopt, m_objopt);
  m_objexcel.quit();
  }

  ///


  /// 释放所引用的com对象。注意:这个过程一定要执行。
  ///

  public void dispose()
  {
  releaseo教程bj(m_objsheets);
  releaseobj(m_objbook);
  releaseobj(m_objbooks);
  releaseobj(m_objexcel);
  system.gc.collect();
  system.gc.waitforpendingfinalizers();
  }
  ///
  /// 释放对象,内部调用
  ///

  ///
  private void releaseobj(object o)
  {
  try
  {
  system.runtime.interops教程ervices.marshal.releasecomobject(o);
  }
  catch { }
  finally { o = null; }
  }

  private excel.application m_objexcel = null;
  private excel.workbooks m_objbooks = null;
  private excel._workbook m_objbook = null;
  private excel.sheets m_objsheets = null;
  private excel._worksheet m_objsheet = null;
  private excel.range m_objrange = null;
  private object m_objopt = system.reflection.missing.value;

  }
}

热门栏目