Visual C# 操作 Excel 文件(三) 對圖片的存取

来源:互联网 发布:mac机appstore是英文 编辑:程序博客网 时间:2024/05/17 10:38
要插入圖片到 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);
 
       Rectangle aBox2 = new Rectangle((int)aShape.Left,(int)aShape.Top,
 
                                       (int)aShape.Width, (int)aShape.Height);
 
       if (aBox1.IntersectsWith(aBox2))
 
       {
 
           aShape.Copy();
 
           //判断剪贴板中是否存在图片。
 
           if (Clipboard.ContainsImage())
 
           {
 
              image = Clipboard.GetImage();
 
           }
 
           break;
 
       }
 
   }
 
   return image;
}

調用的方式:
MyExcel aXLS = new MyExcel();
string szFile = @"D:\temp\test.jpg";
aXLS.SetPicture(3, 5, 12, 12, szFile);
pictureBox1.Image = aXLS.GetPicture("C5");
aXLS.Dispose();
原创粉丝点击