Click here to Skip to main content
13,862,971 members
Click here to Skip to main content
Add your own
alternative version


93 bookmarked
Posted 6 Jul 2007
Licenced CPOL

Write Data to Excel using C#

, 6 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Describes how to write data to an Excel sheet using C#


This article will help to write data to Excel using C# without playing with Excel.Application. With the help of the ExcelFileWriter class, it is very easy to write data to an Excel sheet.


Those who are lazy to read the help of Excel.Application in order to write to an Excel sheet can make use of this article and code base.

Using the Code

Firstly, add the reference "Microsoft Excel 11.0 Object Library" by right clicking the References from Visual Studio .NET and select the COM tab. If Excel 11.0 is not there, select Excel 5.0.

The ExcelFileWriter class is an abstract class.

In order to write, for example, a set of int values to an Excel sheet, the steps to follow are listed below:

  1. Add ExcelFileWriter.cs into your project.

  2. Let's assume you want to add numbers from 1 to 20 which are already populated in the List. You can have any data type inside the list.

  3. Create an object of ExcelFileWriter class. Since my collection class, List, is of type int, I create an object of ExcelFileWriter using int. You can use whatever data type you want as ExcelFileWriter is of generic type.

  4. Call the API, WriteDataToExcel and pass the name of the Excel file, the data, the starting column in Excel and the ending column.

    List<int> myList = new List<int>();
    for (int i = 0; i < 20; i++)
    ExcelFileWriter<int> myExcel = new ExcelWrite();
  5. Create a class which derives from ExcelFileWriter and make an object[] as a member variable.

  6. Override the functions and properties.

    1. Override "Headers" -> return the name of the Column Headers in the Excel sheet:

      public override object[] Headers
      object[] headerName = { "Header1", "Header2", "Header3", "Header4" };
      return headerName;
    2. Override RowCount and ColumnCount.

    3. Override FillRowData -> fill the exceldata object:

      public override void FillRowData(List<int> list)
      myRowCnt = list.Count;
      myExcelData = new object[RowCount + 1, 4];
      for (int row = 1; row <= myRowCnt; row++)
      for (int col = 0; col < 4; col++)
      myExcelData[row, col] = list[row - 1];
    4. Override ExcelData:

      public override object[,] ExcelData
      return myExcelData;

That's it. Build it and Run and you can see that the data is written to the Excel sheet. The important part is you didn't have the headache of understanding the Excel object.

Hope it helps.


  • 6th July, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThank a lot Pin
tinychi5-Jan-15 23:25
membertinychi5-Jan-15 23:25 
QuestionThank you Pin
khairosh25-Nov-13 22:32
memberkhairosh25-Nov-13 22:32 
QuestionWrite Data to Excel using C# Pin
Jaya0411-Sep-12 19:01
memberJaya0411-Sep-12 19:01 
Questionhelp plz Pin
persian264521-Apr-12 23:02
memberpersian264521-Apr-12 23:02 
GeneralMy vote of 4 Pin
coolqer31-Oct-11 5:25
membercoolqer31-Oct-11 5:25 
SuggestionSample on consuming the ExcelFileWriter Class Pin
irshadmohideen22-Aug-11 12:15
memberirshadmohideen22-Aug-11 12:15 
GeneralPopulating with a List of Entity type [modified] Pin
gopalganeriwal12-Jul-10 1:50
membergopalganeriwal12-Jul-10 1:50 
GeneralExcel & Force save Pin
Member 27794098-Jul-09 19:29
memberMember 27794098-Jul-09 19:29 
GeneralNice job Pin
Puhfista2-Dec-08 10:54
memberPuhfista2-Dec-08 10:54 
QuestionHow to export data from gridview to pdf? Pin
AtiqSuman25-Jun-08 19:12
memberAtiqSuman25-Jun-08 19:12 
AnswerRe: How to export data from gridview to pdf? Pin
Matthew Turner20-Aug-08 6:16
memberMatthew Turner20-Aug-08 6:16 
GeneralAlternative ways Pin
FilipKrnjic12-Jun-08 23:24
memberFilipKrnjic12-Jun-08 23:24 
QuestionHow to generate output from certain data in excel file in c# Pin
roslanabdul29-May-08 18:39
memberroslanabdul29-May-08 18:39 
GeneralNamespace Excel is not recognized Pin
AtiqSuman4-Nov-07 0:10
memberAtiqSuman4-Nov-07 0:10 
GeneralRe: Namespace Excel is not recognized Pin
Andrew Greatorex25-Jun-08 5:14
memberAndrew Greatorex25-Jun-08 5:14 
QuestionWhat to do when number of rows are greater than 65000? Pin
imranniaz27-Aug-07 2:58
memberimranniaz27-Aug-07 2:58 
QuestionHow to add ExcelFileWriter.cs in my project Pin
flamingo27-Aug-07 5:39
memberflamingo27-Aug-07 5:39 
AnswerRe: How to add ExcelFileWriter.cs in my project Pin
monalijaju1-Oct-07 3:15
membermonalijaju1-Oct-07 3:15 
General.csproj file is missing Pin
kbsaravana19-Jul-07 4:04
memberkbsaravana19-Jul-07 4:04 
Generaldoesnt work on my machine Pin
fguihen19-Jul-07 3:23
memberfguihen19-Jul-07 3:23 
GeneralFailure to cleanup unmanaged resoruces Pin
Jay L.10-Jul-07 2:39
memberJay L.10-Jul-07 2:39 
GeneralRe: Failure to cleanup unmanaged resoruces Pin
shinilkp10-Jul-07 8:42
membershinilkp10-Jul-07 8:42 
GeneralRe: Failure to cleanup unmanaged resoruces PinPopular
Jay L.10-Jul-07 9:23
memberJay L.10-Jul-07 9:23 
QuestionFormula? Pin
eraseunavez9-Jul-07 21:37
membereraseunavez9-Jul-07 21:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 6 Jul 2007
Article Copyright 2007 by shinilkp
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid