.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

CREATE EXCEL SHEET AND DISPLAY

Posted By:Sreeraj Nair Mungath       Posted Date: July 11, 2012    Points: 40    Category: File Operation    URL: http://www.dotnetspark.com  

CREATE EXCEL SHEET AND DISPLAY
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using aExcel = Microsoft.Office.Interop.Excel;

namespace excel2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            aExcel.Application xlApp;
            aExcel.Workbook xlWorkBook;
            aExcel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new aExcel.Application();
            xlApp.Visible = true;
      
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (aExcel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[1, 1] = "http://google.com";

            xlWorkBook.SaveAs("csharp-Excel.xls", aExcel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, aExcel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
           
            MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
        }

        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();
            }
        }

        

    }
}


     

Further Readings:

Responses
Author: Ravi Ranjan Kumar         Company URL: http://www.dotnetspark.com
Posted Date: July 11, 2012

Will you please elaborate your codes.
Author: abhays         Company URL: http://www.dotnetspark.com
Posted Date: March 31, 2014

PLease Explain the code

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend