C#向Excel報表中插入圖片的2種方法
阿新 • • 發佈:2019-02-03
using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace MengXianhui.Utility.ExcelReport
{
class InsertPictureToExcel
{
///<summary>/// 開啟沒有模板的操作。
///</summary>publicvoid Open( )
{
this.Open(String.Empty);
}
///<summary>/// 功能:實現Excel應用程式的開啟
///</summary>///<param name="TemplateFilePath">模板檔案物理路徑</param>publicvoid 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);
}
privatevoid m_objExcel_WorkbookBeforeClose( Excel.Workbook m_objBooks, refbool _Cancel )
{
MessageBox.Show("儲存完畢!");
}
///<summary>/// 將圖片插入到指定的單元格位置。
/// 注意:圖片必須是絕對物理路徑
///</summary>///<param name="RangeName">單元格名稱,例如:B4</param>///<param name="PicturePath">要插入圖片的絕對路徑。</param>publicvoid 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>publicvoid 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>publicvoid 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>privatevoid Close( )
{
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
}
///<summary>/// 釋放所引用的COM物件。注意:這個過程一定要執行。
///</summary>publicvoid 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>privatevoid 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;
privateobject m_objOpt = System.Reflection.Missing.Value;
}
}
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace MengXianhui.Utility.ExcelReport
{
class InsertPictureToExcel
{
///<summary>/// 開啟沒有模板的操作。
///</summary>publicvoid Open( )
{
this.Open(String.Empty);
}
///<summary>/// 功能:實現Excel應用程式的開啟
{
//開啟物件 m_objExcel =new Excel.Application();
m_objExcel.Visible =false;
m_objExcel.DisplayAlerts =false;
if (m_objExcel.Version !="11.0")
{
MessageBox.Show(
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_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);
}
privatevoid m_objExcel_WorkbookBeforeClose( Excel.Workbook m_objBooks, refbool _Cancel )
{
MessageBox.Show("儲存完畢!");
}
///<summary>/// 將圖片插入到指定的單元格位置。
/// 注意:圖片必須是絕對物理路徑
///</summary>///<param name="RangeName">單元格名稱,例如:B4</param>///<param name="PicturePath">要插入圖片的絕對路徑。</param>publicvoid 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>publicvoid 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>publicvoid 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>privatevoid Close( )
{
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
}
///<summary>/// 釋放所引用的COM物件。注意:這個過程一定要執行。
///</summary>publicvoid 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>privatevoid 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;
privateobject m_objOpt = System.Reflection.Missing.Value;
}
}