Creating an Excel Sheet using .Net

Posted: October 5, 2008 in .Net
Tags: , , ,
In this entry I will show how you can create a Microsoft Excel file using .Net.
// Create the Excel Application object.
ApplicationClass ExcelApp = new ApplicationClass();
// Set the visibility of the application.
ExcelApp.Visible = true;
// Create a new Excel Workbook.
Workbook ExcelWorkbook = ExcelApp.Workbooks.Add(Type.Missing);
// Create a new Excel Sheet.
Worksheet ExcelSheet = (Worksheet)ExcelWorkbook.Sheets.Add(ExcelWorkbook.Sheets.get_Item(1), Type.Missing, 1, XlSheetType.xlWorksheet);
try
{
   // Loop for 10 rows.
   for (int rwCount = 1; rwCount <= 10; rwCount++)
   {
      // Loop for 3 columns.
      for (int clmCount = 1; clmCount <= 3; clmCount++)
      {
         ExcelSheet.Cells[rwCount, clmCount] = "This is Row – " + rwCount + " Column – " + clmCount;
      }
   }
// Save the Excel sheet.
// The @ symbol makes the string to contain any special characters inside the string without breaking the string.
ExcelApp.Save(@"C:\Projects\Ex.xls");
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s