How to create Excel spreadsheet on the fly using C#?

In this code snippet, we will see how we can create Microsoft Excel spreadsheet / worksheet using C#. In this code we will generate the sheet on the fly. There are lot of freeware/opensources available to do this work but in our code snippet we will use spire.xls

We have the requirements to generate/create Excel Spreadsheet using dynamic data on the fly. There are lot of open sources and freewares available including ICEBlue, EPPS, Excelgenerator. In our example, we will use free spire.xls from ICEBlue to create/meet our requirements.

What we need to do?
No, any special activities required you just need to include a dll (Dynamic Lnk Library) in your project and you're ready to start the work. Here is the code, which will create spreadsheet, there are lot of feature, I am not going to discuss here.

using Spire.Xls;
namespace ExcelOnTheFly.ExceleHelper
public class SampleExcel
private string _xlsFilePath = HttpContext.Current.Server.MapPath("~/Sample/XLSFiles/");
private string _xlsFileName;
private Workbook workBook;
private Worksheet workSheet;
public SpireExcel()
: this("myExcel.xls")
{ }

public SampleExcel(string xlsFileName)
_xlsFileName = xlsFileName;
workBook = new Workbook();

public void CreateSampleExcel()
workSheet = workBook.Worksheets[0];
workSheet.Range["A1"].Text = "This is a sample Excel dcouemnt and created on the fly using C#";
workBook.SaveToFile(_xlsFilePath + _xlsFileName);



In this code-snippet, we discussed about
how we can create Microsoft Excel sheet or spreadsheet on the fly using dynamic data.
Copy/paste above code and enjoy the taste of this great tool.

Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 149 articles authored by Gaurav Aroraa


Author: Shuby Arora28 Apr 2015 Member Level: Gold   Points : 0

Good explanation

Author: Gaurav Aroraa29 Apr 2015 Member Level: Gold   Points : 0

Thanks. Glad to help

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: