close

1.前言:
某些生產設備會將分析資料照片存入EXCEL檔案中,因為需要從大量檔案中轉出照片影像,可利用程式將圖片轉出存入磁碟機或是資料庫中。

2.說明:
加入參考:
版本:12.0.0.0
Microsoft.Office.Interop.Excel
Office

加入命名空間:

using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

程式碼:

//使用Application類別,開啟Excel應用程式
Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
//Excel文件開啟不可見
app.Visible = false;
//開啟Workbook
Excel.Workbook wb = null;
wb = app.Workbooks.Open(@"d:\tmp\picture.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet ws = (Excel.Worksheet)app.ActiveSheet;

for (int i = 1; i <= ws.Shapes.Count; i++)
{
	if (((Excel.Shape)ws.Shapes.Item(i)).Type == Office.MsoShapeType.msoPicture)
	{
		string name = ((Excel.Shape)ws.Shapes.Item(i)).Name;

		Excel.Picture pic = (Excel.Picture)ws.Pictures(name);
		pic.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap);

		if (Clipboard.ContainsImage())
		{
			Image img = Clipboard.GetImage();
			pictureBox1.Image = img;

			Application.DoEvents();
			System.Threading.Thread.Sleep(2000);
		}
	}
}

//停用警告訊息
app.DisplayAlerts = false;

if (app != null)
{
	//關閉活頁簿
	wb.Close(Type.Missing, @"D:\tmp\picture.xlsx", Type.Missing);
	//關閉Excel
	app.Quit();
	//釋放資源
	System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
	System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
	System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
	ws = null;
	wb = null;
	app = null;
}

GC.Collect();

圖例:

getPictureFromExcel    

arrow
arrow
    文章標籤
    C# EXCEL Picture Image
    全站熱搜

    西夏普 發表在 痞客邦 留言(0) 人氣()