您的位置:首页 > 编程语言 > C#

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# Excel