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);
}
/// <summary>
/// 功能:实现excel应用程序的打开
/// </summary>
/// <param name="templatefilepath">模板文件物理路径</param>
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("保存完毕!");
}
/// <summary>
/// 将图片插入到指定的单元格位置。
/// 注意:图片必须是绝对物理路径
/// </summary>
/// <param name="rangename">单元格名称,例如:b4</param>
/// <param name="picturepath">要插入图片的绝对路径。</param>
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);
}
/// <summary>
/// 将图片插入到指定的单元格位置,并设置图片的宽度和高度。
/// 注意:图片必须是绝对物理路径
/// </summary>
/// <param name="rangename">单元格名称,例如:b4</param>
/// <param name="picturepath">要插入图片的绝对路径。</param>
/// <param name="pictutewidth">插入后,图片在excel中显示的宽度。</param>
/// <param name="pictureheight">插入后,图片在excel中显示的高度。</param>
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);
}
/// <summary>
/// 将excel文件保存到指定的目录,目录必须事先存在,文件名称不一定要存在。
/// </summary>
/// <param name="outputfilepath">要保存成的文件的全路径。</param>
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();
}
/// <summary>
/// 关闭应用程序
/// </summary>
private void close()
{
m_objbook.close(false, m_objopt, m_objopt);
m_objexcel.quit();
}
/// <summary>
/// 释放所引用的com对象。注意:这个过程一定要执行。
/// </summary>
public void dispose()
{
releaseo教程bj(m_objsheets);
releaseobj(m_objbook);
releaseobj(m_objbooks);
releaseobj(m_objexcel);
system.gc.collect();
system.gc.waitforpendingfinalizers();
}
/// <summary>
/// 释放对象,内部调用
/// </summary>
/// <param name="o"></param>
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;
}
}