Visual C# 操作 Excel 文件(三) 對圖片的存取
2013-04-25 10:28
836 查看
要插入圖片到 Excel 中,就要用到以下的方法:
Sheet.Shapes.AddPicture(FileName,LinkToFile,SaveWithDocument,Left,
Top, Width,Height)
LinkToFile : 是否要鏈結到文件。
SaveWithDocument : 圖片是否隨文檔一起保存。
Left, Top : 圖片在文檔中的左上角座標,以 points 為單位。
Width, Height : 圖片顯示的寬度與高度,以 points 為單位。
詳細說明見http://msdn.microsoft.com/zh-cn/library/aa221765(office.11).aspx。
在使用上,不會知道 Left, Top,而是用類似 "A1" 或 [3, 5] 之類的方式,用那一格的左上角座標當作圖檔的作標。所以在MyExcel 中增加了以下兩個方法:
SetPicture(col, row, width, height,filename);
SetPicture(RangeName, width, height, filename);
這兩個方法在取得 Range 後,就呼叫 InsertPicture() 來完成任務。看看以下的程式碼吧!
public void SetPicture(int col, int row, double width, doubleheight, string szFile)
{
Excel.Range
aRange = (Excel.Range)m_aSheet.Cells[row, col];
InsertPicture(aRange, width, height, szFile);
}
public void SetPicture(string szRangeName, double width, doubleheight, string szFile)
{
Excel.Range aRange = m_aSheet.get_Range(szRangeName, missing);
InsertPicture(aRange, width, height, szFile);
}
private void InsertPicture(Excel.Range aRange, double width, doubleheight, string szFile)
{
float x =System.Convert.ToSingle(aRange.Left.ToString())
+ 1;
float y =System.Convert.ToSingle(aRange.Top.ToString())
+ 1;
float w =(float)width;
float h =(float)height;
Bitmap aPic = newBitmap(szFile);
//載入圖檔,然後依顯示區域,作等比例縮放。
if(((float)aPic.Height / (float)aPic.Width) <((float)h
/ (float)w))
{
h
= w * aPic.Height / aPic.Width;
}
else
{
w
= h * aPic.Width / aPic.Height;
}
try
{
m_aSheet.Shapes.AddPicture(szFile,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue,
x,
y, w, h);
}
catch
(Exception e)
{
MessageBox.Show(e.Message,
szFile);
}
}
要從 Excel 中讀取圖片比較麻煩,雖然圖片全部存在 Shapes集合中,但並不知道圖片在那一格上,唯一的方式是判斷那一格的位置與圖片的位置是否有重疊,若是,才是真正要取得的圖片。GetImage()才是真正取得圖片的方法,找到該 Shape 後,將其複製到剪貼簿中,然後再從剪貼簿中讀取 Image。
public Image GetPicture(int col, int row)
{
Excel.Range aRange = (Excel.Range)m_aSheet.Cells[row, col];
returnGetImage(aRange);
}
public Image GetPicture(string szRangeName)
{
Excel.Range aRange = m_aSheet.get_Range(szRangeName, missing);
returnGetImage(aRange);
}
private Image GetImage(Excel.Range aRange)
{
Image image = null;
Int32 x =System.Convert.ToInt32(aRange.Left.ToString()); //
pt
Int32 y =System.Convert.ToInt32(aRange.Top.ToString()); //
pt
Int32 w =System.Convert.ToInt32(aRange.MergeArea.Width); //
pt
Int32 h =System.Convert.ToInt32(aRange.MergeArea.Height);
// pt
Rectangle
aBox1 = new Rectangle(x, y, w, h);
int Count =m_aSheet.Shapes.Count;
for (int i =1; i <= Count; i++)
{
Excel.Shape
aShape = (Excel.Shape)m_aSheet.Shapes.Item(i);
Sheet.Shapes.AddPicture(FileName,LinkToFile,SaveWithDocument,Left,
Top, Width,Height)
LinkToFile : 是否要鏈結到文件。
SaveWithDocument : 圖片是否隨文檔一起保存。
Left, Top : 圖片在文檔中的左上角座標,以 points 為單位。
Width, Height : 圖片顯示的寬度與高度,以 points 為單位。
詳細說明見http://msdn.microsoft.com/zh-cn/library/aa221765(office.11).aspx。
在使用上,不會知道 Left, Top,而是用類似 "A1" 或 [3, 5] 之類的方式,用那一格的左上角座標當作圖檔的作標。所以在MyExcel 中增加了以下兩個方法:
SetPicture(col, row, width, height,filename);
SetPicture(RangeName, width, height, filename);
這兩個方法在取得 Range 後,就呼叫 InsertPicture() 來完成任務。看看以下的程式碼吧!
public void SetPicture(int col, int row, double width, doubleheight, string szFile)
{
Excel.Range
aRange = (Excel.Range)m_aSheet.Cells[row, col];
InsertPicture(aRange, width, height, szFile);
}
public void SetPicture(string szRangeName, double width, doubleheight, string szFile)
{
Excel.Range aRange = m_aSheet.get_Range(szRangeName, missing);
InsertPicture(aRange, width, height, szFile);
}
private void InsertPicture(Excel.Range aRange, double width, doubleheight, string szFile)
{
float x =System.Convert.ToSingle(aRange.Left.ToString())
+ 1;
float y =System.Convert.ToSingle(aRange.Top.ToString())
+ 1;
float w =(float)width;
float h =(float)height;
Bitmap aPic = newBitmap(szFile);
//載入圖檔,然後依顯示區域,作等比例縮放。
if(((float)aPic.Height / (float)aPic.Width) <((float)h
/ (float)w))
{
h
= w * aPic.Height / aPic.Width;
}
else
{
w
= h * aPic.Width / aPic.Height;
}
try
{
m_aSheet.Shapes.AddPicture(szFile,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue,
x,
y, w, h);
}
catch
(Exception e)
{
MessageBox.Show(e.Message,
szFile);
}
}
要從 Excel 中讀取圖片比較麻煩,雖然圖片全部存在 Shapes集合中,但並不知道圖片在那一格上,唯一的方式是判斷那一格的位置與圖片的位置是否有重疊,若是,才是真正要取得的圖片。GetImage()才是真正取得圖片的方法,找到該 Shape 後,將其複製到剪貼簿中,然後再從剪貼簿中讀取 Image。
public Image GetPicture(int col, int row)
{
Excel.Range aRange = (Excel.Range)m_aSheet.Cells[row, col];
returnGetImage(aRange);
}
public Image GetPicture(string szRangeName)
{
Excel.Range aRange = m_aSheet.get_Range(szRangeName, missing);
returnGetImage(aRange);
}
private Image GetImage(Excel.Range aRange)
{
Image image = null;
Int32 x =System.Convert.ToInt32(aRange.Left.ToString()); //
pt
Int32 y =System.Convert.ToInt32(aRange.Top.ToString()); //
pt
Int32 w =System.Convert.ToInt32(aRange.MergeArea.Width); //
pt
Int32 h =System.Convert.ToInt32(aRange.MergeArea.Height);
// pt
Rectangle
aBox1 = new Rectangle(x, y, w, h);
int Count =m_aSheet.Shapes.Count;
for (int i =1; i <= Count; i++)
{
Excel.Shape
aShape = (Excel.Shape)m_aSheet.Shapes.Item(i);
相关文章推荐
- Visual C# 操作 Excel 文件(二) 封裝成 MyExcel 類
- Visual C# 操作 Excel 文件(一) 基本操作
- Visual C# 操作 Excel 文件(四) 存檔及其他操作
- JAVA-Excel文件操作
- Excel-VBA操作文件四大方法
- Excel 关于新建xls文件 新建sheet 合并sheet的VBA操作代码
- java操作office和pdf文件java读取word,excel和pdf文档内容
- 案例1:“操作中停电导致Excel文件受损”的数据恢复
- python第三方库——xlrd和xlwt操作Excel文件学习
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- VB 通过指定Excel模板文件进行另存为新文件操作
- 针对Excel表格文件操作的编程实现
- ★★Excel-VBA操作文件四大方法之二
- 利用 JAVA 操作 EXCEL 文件
- excel 2003 ,wps office 2007,excel 2007 xls xlsx文件操作效率比较
- C++ ofstream和ifstream详细用法 及文件操作 导入到EXCEL
- ArcGIS操作Excel文件没有注册类解决办法
- excel文件操作
- java操作Excel、PDF文件
- MongoDB学习笔记(五) MongoDB文件存取操作