Posts Tagged ‘ Imagen ’

Crear un excel con una imagen en C#


En primer lugar deberíamos agregar la librería del Excel y luego el using Microsoft.Office.Interop.Excel ;

object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application apl = new Microsoft.Office.Interop.Excel.ApplicationClass();

//Creo mi workbook de Excel
Workbook libro = apl.Workbooks.Open(@»C:\Pruebas\Prueba.xls», oMissing, oMissing, oMissing, oMissing,oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

//Una hoja que asociaremos al excel anterior.
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)libro.Sheets[1];

//Imagen a agregar (notese el tamaño 500×450 configurable)
System.Drawing.Image logo1 = System.Drawing.Image.FromFile(@»C:\Pruebas\Prueba.JPG»);
sheet.Shapes.AddPicture(@»C:\Pruebas\Prueba.JPG», Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 1f, 1f, 500, 450);

//Guardamos el archivo
libro.SaveAs(@»C:\Pruebas\PruebaImg.xls», Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false,false,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, null, null, 0);