Click here to Skip to main content
Licence CPOL
First Posted 6 Jul 2007
Views 128,236
Downloads 6,917
Bookmarked 79 times

Write Data to Excel using C#

By | 6 Jul 2007 | Article
Describes how to write data to an Excel sheet using C#

Introduction

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.

Background

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++)
    {
    myList.Add(i);
    }
    ExcelFileWriter<int> myExcel = new ExcelWrite();
    myExcel.WriteDateToExcel(@"C:\TEMP\myExcel.xls",myList,"A1","D1");
  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
      {
      get 
      {
      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
      {
      get 
      {
      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.

History

  • 6th July, 2007: Initial post

License

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

About the Author

shinilkp

Web Developer

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhelp plz Pinmemberpersian264522:02 21 Apr '12  
GeneralMy vote of 4 Pinmembercoolqer4:25 31 Oct '11  
SuggestionSample on consuming the ExcelFileWriter Class Pinmemberirshadmohideen11:15 22 Aug '11  
GeneralPopulating with a List of Entity type [modified] Pinmembergopalganeriwal0:50 12 Jul '10  
GeneralExcel & Force save PinmemberMember 277940918:29 8 Jul '09  
GeneralNice job PinmemberPuhfista9:54 2 Dec '08  
QuestionHow to export data from gridview to pdf? PinmemberAtiqSuman18:12 25 Jun '08  
AnswerRe: How to export data from gridview to pdf? PinmemberMatthew Turner5:16 20 Aug '08  
GeneralAlternative ways PinmemberBarbaMariolino22:24 12 Jun '08  
QuestionHow to generate output from certain data in excel file in c# Pinmemberroslanabdul17:39 29 May '08  
GeneralNamespace Excel is not recognized PinmemberAtiqSuman23:10 3 Nov '07  
GeneralRe: Namespace Excel is not recognized PinmemberAndrew Greatorex4:14 25 Jun '08  
QuestionWhat to do when number of rows are greater than 65000? Pinmemberimranniaz1:58 27 Aug '07  
QuestionHow to add ExcelFileWriter.cs in my project Pinmemberflamingo24:39 7 Aug '07  
AnswerRe: How to add ExcelFileWriter.cs in my project Pinmembermonalijaju2:15 1 Oct '07  
General.csproj file is missing Pinmemberkbsaravana3:04 19 Jul '07  
Generaldoesnt work on my machine Pinmemberfguihen2:23 19 Jul '07  
GeneralFailure to cleanup unmanaged resoruces PinmemberJay L.1:39 10 Jul '07  
GeneralRe: Failure to cleanup unmanaged resoruces Pinmembershinilkp7:42 10 Jul '07  
GeneralRe: Failure to cleanup unmanaged resoruces PinmemberJay L.8:23 10 Jul '07  
QuestionFormula? Pinmembername for display only20:37 9 Jul '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120517.1 | Last Updated 6 Jul 2007
Article Copyright 2007 by shinilkp
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid