my code stock.com

Fmarchena

Inserta datos en un excel en c#
by Fmarchena

Permite inserta en un excel datos

Snippet options

Download: Download snippet as inserta-datos-en-un-excel-en-c.c.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

//se utiliza la libreria using Excel = Microsoft.Office.Interop.Excel;


              Excel.Application xlApp = default(Excel.Application);
            Excel.Workbook xlWorkBook = default(Excel.Workbook);
            Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);

            const string fName = @"D:\Pinturas\csharp-Excel.xls";

            try
            {
                object misValue = System.Reflection.Missing.Value;

                xlApp = new Excel.ApplicationClass();
                xlWorkBook = xlApp.Workbooks.Add(misValue);

                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";

                xlWorkBook.SaveAs(fName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);
            }
            finally
            {
                if (xlApp != null)
                    releaseObject(xlApp);
                if (xlWorkBook != null)
                    releaseObject(xlWorkBook);
                if (xlWorkSheet != null)
                    releaseObject(xlWorkSheet);
            }

            if (System.IO.File.Exists(fName))
            {
                if (MessageBox.Show("Usted Desearia Abrir el archivo Excel?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(fName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error opening the excel file." + Environment.NewLine +
                        ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            
            
            /* metodo para   recojer basura */
            
            
               private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.