使用C#在Excel中的特定单元格上添加图片

使用C#在Excel中的特定单元格上添加图片

问题描述:

我正尝试将图像添加到Excel单元格的第3行第1列,如下所示.编译器给我一个错误. 我在这里做错了吗?预先感谢您的建议.

I am trying to add an image into Excel cell at row 3 column 1 as specified below. The compiler gave me an error. Did I do something wrong here? Thanks in advance for your suggestions.

Excel.Application xlApp; 
Excel.Workbook wb; 
Excel.Worksheet ws; 
object misValue = System.Reflection.Missing.Value; 
xlApp = new Excel.Application(); 
wb = xlApp.Workbooks.Add(misValue); 
ws = (Excel.Worksheet)wb.Worksheets.get_Item(1); 
ws.Cells[3, 1] = ws.Shapes.AddPicture("C:\\photos\\4a.png", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 75, 75, 350, 50);

您必须添加如下图片

Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)ws.Cells[3, 1];
float Left = (float)((double)oRange.Left);
float Top = (float)((double)oRange.Top);
const float ImageSize = 32;
ws.Shapes.AddPicture("C:\\pic.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);