none
como insertar una imagen a un excel

Answers

  • Thank you.

    Please try some solutions at http://www.eggheadcafe.com/community/aspnet/2/10502/here-is-the-solution.aspx (How to Insert a picture into excel using c#) and see if they address your issue.

    Please feel free to follow up here if they don't.

    Thank you.


    WHAT'S NEW IN THE .NET FRAMEWORK 4:
    Article: Comparison of parallel and sequential computing in .NET Framework
    Thursday, May 27, 2010 8:53 AM
  • Hi,

    Try this code.

     

    using System;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Excel;
    using System.Reflection;
    
    void button1_Click(object sender, EventArgs e)
    {
    
     ApplicationClass objExcel = null;
     Workbooks objBooks = null;
     _Workbook objBook = null;
     Sheets objSheets = null;
     _Worksheet objSheet = null;
     Range objRange = null;
     int row = 1, col = 1;
     try
     {
    
      objExcel = new ApplicationClass();
      objBooks = objExcel.Workbooks;
      objBook = objBooks.Add(XlWBATemplate.xlWBATWorksheet);
      objSheets = objBook.Worksheets;
      objSheet = (_Worksheet)objSheets.get_Item(1);
      objRange = (Range)objSheet.Cells[row, col];
      //Insert picture into Excel Shee
      Picture picture = ((Pictures)objSheet.Pictures(Type.Missing)).Insert(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg", Type.Missing);
    
      //Save Excel document
      objSheet.Name = "Sample Sheet";
      object objOpt = Missing.Value;
      objBook.SaveAs(@"C:\TestSheet1.xls", objOpt, objOpt, objOpt, objOpt, objOpt, XlSaveAsAccessMode.xlNoChange, objOpt, objOpt, objOpt, objOpt, objOpt);
      objBook.Close(false, objOpt, objOpt);
    
     }
     catch
     {
      MessageBox.Show("Error Inserting Picture in Excel Document");
     }
     finally
     {
      objExcel = null;
      objBooks = null;
      objBook = null;
      objSheets = null;
      objSheet = null;
      objRange = null;
      ReleaseComObject(objExcel);
      ReleaseComObject(objBooks);
      ReleaseComObject(objBook);
      ReleaseComObject(objSheets);
      ReleaseComObject(objSheet);
      ReleaseComObject(objRange);
     }
    }
    //Release COM objects from memory
    public void ReleaseComObject(object reference)
    {
     try
     {
      while (System.Runtime.InteropServices.Marshal.ReleaseComObject(reference) <= 0)
      {
      }
     }
     catch
     {
     }
    }  

     

    Thursday, May 27, 2010 9:50 AM
  • Hi,

    You need to add reference to 'Microsoft.Office.Interop.Excel'  Version 11 or 12. For that, Right click on References in the Solution Explorer->Add References->.NET Tab->Select Microsoft.Office.Interop.Excel Version 11. The path to this assembly is C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11.

    You can download Office Primary Interop Assemblies from here.

    2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies

    Office 2003 Update: Redistributable Primary Interop Assemblies

     

    Friday, May 28, 2010 5:46 AM

All replies

  • PLEASE REPOST IN ENGLISH


    WHAT'S NEW IN THE .NET FRAMEWORK 4:
    Article: Comparison of parallel and sequential computing in .NET Framework
    Thursday, May 27, 2010 8:29 AM
  • i wich i know how to insert an image .jpg into an excel usign c# code
    Thursday, May 27, 2010 8:42 AM
  • i want  to know how to inser an image .jpg into an excel using c # code :)

    Thursday, May 27, 2010 8:44 AM
  • Thank you.

    Please try some solutions at http://www.eggheadcafe.com/community/aspnet/2/10502/here-is-the-solution.aspx (How to Insert a picture into excel using c#) and see if they address your issue.

    Please feel free to follow up here if they don't.

    Thank you.


    WHAT'S NEW IN THE .NET FRAMEWORK 4:
    Article: Comparison of parallel and sequential computing in .NET Framework
    Thursday, May 27, 2010 8:53 AM
  • Hi,

    Try this code.

     

    using System;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Excel;
    using System.Reflection;
    
    void button1_Click(object sender, EventArgs e)
    {
    
     ApplicationClass objExcel = null;
     Workbooks objBooks = null;
     _Workbook objBook = null;
     Sheets objSheets = null;
     _Worksheet objSheet = null;
     Range objRange = null;
     int row = 1, col = 1;
     try
     {
    
      objExcel = new ApplicationClass();
      objBooks = objExcel.Workbooks;
      objBook = objBooks.Add(XlWBATemplate.xlWBATWorksheet);
      objSheets = objBook.Worksheets;
      objSheet = (_Worksheet)objSheets.get_Item(1);
      objRange = (Range)objSheet.Cells[row, col];
      //Insert picture into Excel Shee
      Picture picture = ((Pictures)objSheet.Pictures(Type.Missing)).Insert(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg", Type.Missing);
    
      //Save Excel document
      objSheet.Name = "Sample Sheet";
      object objOpt = Missing.Value;
      objBook.SaveAs(@"C:\TestSheet1.xls", objOpt, objOpt, objOpt, objOpt, objOpt, XlSaveAsAccessMode.xlNoChange, objOpt, objOpt, objOpt, objOpt, objOpt);
      objBook.Close(false, objOpt, objOpt);
    
     }
     catch
     {
      MessageBox.Show("Error Inserting Picture in Excel Document");
     }
     finally
     {
      objExcel = null;
      objBooks = null;
      objBook = null;
      objSheets = null;
      objSheet = null;
      objRange = null;
      ReleaseComObject(objExcel);
      ReleaseComObject(objBooks);
      ReleaseComObject(objBook);
      ReleaseComObject(objSheets);
      ReleaseComObject(objSheet);
      ReleaseComObject(objRange);
     }
    }
    //Release COM objects from memory
    public void ReleaseComObject(object reference)
    {
     try
     {
      while (System.Runtime.InteropServices.Marshal.ReleaseComObject(reference) <= 0)
      {
      }
     }
     catch
     {
     }
    }  

     

    Thursday, May 27, 2010 9:50 AM
  • it seems perfect the problem is that when i try to use it.  It appears an error.  it said:  "interop doesn exist in the namespaces"

    i've read about it and it saids that i should get a reference  before use "using Microsoft.Office.Interop.Excel;" line

    i fisrst go to the com libraries and use excell 11.0 but still apears the error and it underline in red the interop part of the using ¿? :(

    well i hope you understand

     

    Thursday, May 27, 2010 2:22 PM
  • Hi,

    You need to add reference to 'Microsoft.Office.Interop.Excel'  Version 11 or 12. For that, Right click on References in the Solution Explorer->Add References->.NET Tab->Select Microsoft.Office.Interop.Excel Version 11. The path to this assembly is C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11.

    You can download Office Primary Interop Assemblies from here.

    2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies

    Office 2003 Update: Redistributable Primary Interop Assemblies

     

    Friday, May 28, 2010 5:46 AM