¿Cómo insertar una imagen en Excel desde la aplicación C #?
Estoy tratando de insertar una imagen en la hoja de cálculo de Excel usando mi aplicación C #.
He usado lo siguiente como mi fuente.http://csharp.net-informations.com/excel/csharp-insert-picture-excel.htm
Toda esta línea está subrayada en azul.
xlWorkSheet.Shapes.AddPicture("C:\\pic.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45);
Mi código:
private void btnWriteSpreedSheet_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//xlWorkSheet.SetBackgroundPicture("C:/Users/Shaun/Documents/Visual Studio 2010/Projects/TestXMLToEXCEL/TestXMLToEXCEL/bin/Debugpic.JPG"); //
//add some text
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkSheet.Cells[2, 1] = "Adding picture in Excel File";
xlWorkSheet.Shapes.AddPicture("C:\\pic.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45); //C:\\csharp-xl-picture.JPG
xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
MessageBox.Show ("File created !");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Error de mensajes:
La mejor coincidencia del método sobrecargado para 'Microsoft.Office.Interop.Excel.Shapes.AddPicture (string, Microsoft.Office.Core.MsoTriState, Microsoft.Office.Core.MsoTriState, float, float, float, float)' tiene algunos argumentos inválidos
El tipo 'Microsoft.Office.Core.MsoTriState' se define en un ensamblaje al que no se hace referencia. Debe agregar una referencia al ensamblado 'office, Version = 12.0.0.0, Culture = neutral, PublicKeyToken = 71e9bce111e9429c'.
Argumento 2: no se puede convertir de 'Microsoft.Office.Core.MsoTriState [c: \ users \ shaun \ documents \ visual studio 2010 \ Projects \ TestXMLToEXCEL \ TestXMLToEXCEL \ CreateSpreadSheet.cs]' a 'Microsoft.Office.Core.MsoTriState'
Argumento 3: no se puede convertir de 'Microsoft.Office.Core.MsoTriState [c: \ users \ shaun \ documents \ visual studio 2010 \ Projects \ TestXMLToEXCEL \ TestXMLToEXCEL \ CreateSpreadSheet.cs]' a 'Microsoft.Office.Core.MsoTriState'
Mis Referencias:
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using Microsoft.Office;
using System.Xml;