Create Excel 2007 file using Interop services.

Step 1

First of all we have to create a new Console Application ; let us see the description with images of how to create it.

  • Open Visual Studio
  • File>New>Project
  • Choose Visual C#> Windows > Select Console Application

Step 2

Select >> Project Menu >> Click on Add References >> Select COM Tab >>

Add “Microsoft Excel 12.0 Object Library”

Image1.png

Step 3

Write below code to generate Excel File.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

using System.Runtime.InteropServices;

using Microsoft.Office.Interop;

using Microsoft.Office.Interop.Excel;

namespace ConsoleApplication1

{

class Class1

{

public static void Main(string[] ar)

{

Application ExcelApp = new Application();

Workbook ExcelWorkBook = null;

Worksheet ExcelWorkSheet = null;

ExcelApp.Visible = true;

ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

// ExcelWorkBook.Worksheets.Add(); //Adding New Sheet in Excel Workbook

try

{

ExcelWorkSheet = ExcelWorkBook.Worksheets[1]; // Compulsory Line in which sheet you want to write data

//Writing data into excel of 100 rows with 10 column

for (int r = 1; r < 101; r++) //r stands for ExcelRow and c for ExcelColumn

{

// Excel row and column start positions for writing Row=1 and Col=1

for (int c = 1; c < 11; c++)

ExcelWorkSheet.Cells[r, c] = "R" + r + "C" + c;

}

ExcelWorkBook.Worksheets[1].Name = "MySheet";//Renaming the Sheet1 to MySheet

ExcelWorkBook.SaveAs("d:\\Testing.xlsx");

ExcelWorkBook.Close();

ExcelApp.Quit();

Marshal.ReleaseComObject(ExcelWorkSheet);

Marshal.ReleaseComObject(ExcelWorkBook);

Marshal.ReleaseComObject(ExcelApp);

}

catch (Exception exHandle)

{

Console.WriteLine("Exception: " + exHandle.Message);

Console.ReadLine();

}

finally

{

foreach (Process process in Process.GetProcessesByName("Excel"))

process.Kill();

}

}
}
}