Create and Bind Data in Excel File
In this article, I will explain how to create and Bind Data in Excel File using C#. With the help of Microsoft.Office.Interop.Excel dll we can create and bind Excel file. It helps to automatically create excel data without user interference.
Create and Bind Data in Excel File
Microsoft Excel is a spreadsheet tool capable of manage and organize data. With the help of Microsoft.Office.Interop.Excel dll we can create Excel file in C# by selecting Microsoft.Office.Interop.Excel dll as referece or else we can give reference from com components and Select Microsoft Excel 12.0 Object Library .
Below code shows assigning of Microsoft.Office.Interop.Excel dll to Excel variable.
using Excel = Microsoft.Office.Interop.Excel;
Full Sample Code as Follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Declare Excel variable and assign Microsoft.Office.Interop.Excel dll name file
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//creating excel file
//Below Declaring Excel.Application as xlApp, Excel.Workbook as xlWorkBook, Excel.Worksheet as xlWorkSheet.
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);
// Assigning values to the first row which we assume as columns of the Excel
// xlWorkSheet.Cells passing Rowindex and Columnindex as parameter, For example xlWorkSheet.Cells[Rowindex, Columnindex]
// Modify the value in the First row of the first cell.
xlWorkSheet.Cells[1, 1] = "Name";
// Modify the value in the First row of the second cell.
xlWorkSheet.Cells[1, 2] = "Age";
// Modify the value in the First row of the Third cell.
xlWorkSheet.Cells[1, 3] = "Gender";
// Modify the value in the First row of the Fourth cell.
xlWorkSheet.Cells[1, 4] = "Address";
// Modify the value in the First row of the Fifth cell.
xlWorkSheet.Cells[1, 5] = "Mobile";
//Assigning values to the second row which we assume as first data row of the Excel
// Modify the value in the Second row of the first cell.
xlWorkSheet.Cells[2, 1] = "Raj";
// Modify the value in the Second row of the second cell.
xlWorkSheet.Cells[2, 2] = "28";
// Modify the value in the Second row of the Third cell.
xlWorkSheet.Cells[2, 3] = "Male";
// Modify the value in the Second row of the Fourth cell.
xlWorkSheet.Cells[2, 4] = "Sydney";
// Modify the value in the Second row of the Fifth cell.
xlWorkSheet.Cells[2, 5] = "9111111111";
//Assigning values to the third row which we assume as second data row of the Excel
// Modify the value in the Third row of the first cell.
xlWorkSheet.Cells[3, 1] = "David";
// Modify the value in the Third row of the second cell.
xlWorkSheet.Cells[3, 2] = "29";
// Modify the value in the Third row of the Third cell.
xlWorkSheet.Cells[3, 3] = "Male";
// Modify the value in the Third row of the Fourth cell.
xlWorkSheet.Cells[3, 4] = "Canada";
// Modify the value in the Third row of the Fifth cell.
xlWorkSheet.Cells[3, 5] = "9122222222";
//Assign path and filename for the Excel file to filename string variable
string Filename = @"C:\sample.xls";
//xlWorkBook.SaveAs Method contains following parameters object Filename,object FileFormat, object Password, object WriteResPassword, object ReadOnlyRecommended, object CreateBackup, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode, object ConflictResolution, object AddToMru, object TextCodepage, object TextVisualLayout, object Local.
xlWorkBook.SaveAs(Filename, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlNoChange, misValue, misValue, misValue, misValue, misValue);
//Following xlWorkBook.Close Method contains following parameters Object SaveChanges, Object Filename, object RouteWorkbook
//passing true as SaveChange parameter inside Close method which saves changes in Excel.
xlWorkBook.Close(true, misValue, misValue);
// call the Quit-method of the excel application object:
xlApp.Quit();
//Releasing resource object allocated from Excel.Application, Excel.Workbook, Excel.Worksheet
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
//Calling that ReleaseComObject method will release the object for garbage collection
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
// Collect all generations of memory.
GC.Collect();
}
}
}
}
Note: I had attached Microsoft.Office.Interop.Excel dll which is helps to create excel and also Full Sample code.