程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#向Excel報表中插入圖片的2種方法

C#向Excel報表中插入圖片的2種方法

編輯:關於C語言

這幾天做向Excel插入數據,其中有插入圖片的需求,經試驗,下面2種方法都可以插入圖片,但各有不同的用處。現將這2種方法共享出來,希望需要的朋友進行參考,代碼中已經有詳細注釋了。

注意:使用之前需要引用COM:Microsoft Office 11.0 Object Library

如果引用列表中沒有,需要自行添加 C:\Program Files\Microsoft Office\Office11\Excel.EXE

調用方法:

MengXianhui.Utility.ExcelReport.InsertPictureToExcel ipt = new MengXianhui.Utility.ExcelReport.InsertPictureToExcel();
ipt.Open();
ipt.InsertPicture("B2", @"C:\Excellogo.gif");
ipt.InsertPicture("B8", @"C:\Excellogo.gif",120,80);
ipt.SaveFile(@"C:\ExcelTest.xls");
ipt.Dispose();

簡單包裝的類:

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace MengXianhui.Utility.ExcelReport
{
class InsertPictureToExcel
{
/// <summary>
/// 打開沒有模板的操作。
/// </summary>
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_obJSheets = (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.msoTrue, 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( )
{
ReleaSEObj(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.InteropServices.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;
}
}

運行結果:

-
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved