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”

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

Viky TripathiPosted Sep 4, 2020, 3:37 AM
XlWorkSheet = (Worksheet)xWb.Worksheets[1]; worksheet is null, i a trying to add fresh new workbook with single worksheet,tell me what is the process?
Jack BregPosted Jun 24, 2020, 11:24 AM
Hello, I have tried to find same solutions that you are searching for. I have tried different websites and software. You can visit zetexcel.com. They develop high performance applications to Create, Edit, Convert or Print Excel spreadsheet file formats without requiring Microsoft Excel.