Click here to Skip to main content
11,930,788 members (49,543 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


257 bookmarked

Generate Excel files without using Microsoft Excel

, 2 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A C# class to create Excel files without requiring Microsoft Excel.
This is an old version of the currently published article.


Generating Excel files from web pages or other applications is a major subject in many articles. I'd like to present here a solution that does not require MS Excel to be installed on the target computer. This article shows how to create Microsoft Excel Binary Interchange File Format (BIFF) without using Microsoft Excel. BIFF is the native file format for Excel data, and can be viewed and modified in Microsoft Excel 97 or later.

Approaches and problems

The technique that is most frequently used to transfer data to an Excel workbooks is Automation. With Automation, you can call methods and properties that are specific to Excel tasks, but this solution has many drawbacks. Some of them are described in the Microsoft Knowledge Base. Additionally, you have to manage the lifetime of the temporary XLS files created on the server. Also, it is slow, because Excel runs in a separate process.

ExcelWriter class

The solution presented here is to write directly to a stream in Excel binary file format. For this purpose, I created very small C# class called ExcelWriter.

ExcelWriter class diagram
/// <summary>
/// Produces Excel file without using Excel
/// </summary>
public class ExcelWriter
    private Stream stream;
    private BinaryWriter writer;

    private ushort[] clBegin = { 0x0809, 8, 0, 0x10, 0, 0 };
    private ushort[] clEnd = { 0x0A, 00 };

    private void WriteUshortArray(ushort[] value)
        for (int i = 0; i < value.Length; i++)

    /// <summary>
    /// Initializes a new instance of the <see cref="ExcelWriter"/> class.
    /// </summary>
    /// <param name="stream">The stream.</param>
    public ExcelWriter(Stream stream)
    { = stream;
        writer = new BinaryWriter(stream);

    /// <summary>
    /// Writes the text cell value.
    /// </summary>
    /// <param name="row">The row.</param>
    /// <param name="col">The col.</param>
    /// <param name="value">The string value.</param>
    public void WriteCell(int row, int col, string value)
        ushort[] clData = { 0x0204, 0, 0, 0, 0, 0 };
        int iLen = value.Length;
        byte[] plainText = Encoding.ASCII.GetBytes(value);
        clData[1] = (ushort)(8 + iLen);
        clData[2] = (ushort)row;
        clData[3] = (ushort)col;
        clData[5] = (ushort)iLen;

    /// <summary>
    /// Writes the integer cell value.
    /// </summary>
    /// <param name="row">The row number.</param>
    /// <param name="col">The column number.</param>
    /// <param name="value">The value.</param>
    public void WriteCell(int row, int col, int value)
        ushort[] clData = { 0x027E, 10, 0, 0, 0 };
        clData[2] = (ushort)row;
        clData[3] = (ushort)col;
        int iValue = (value << 2) | 2;

    /// <summary>
    /// Writes the double cell value.
    /// </summary>
    /// <param name="row">The row number.</param>
    /// <param name="col">The column number.</param>
    /// <param name="value">The value.</param>
    public void WriteCell(int row, int col, double value)
        ushort[] clData = { 0x0203, 14, 0, 0, 0 };
        clData[2] = (ushort)row;
        clData[3] = (ushort)col;

    /// <summary>
    /// Writes the empty cell.
    /// </summary>
    /// <param name="row">The row number.</param>
    /// <param name="col">The column number.</param>
    public void WriteCell(int row, int col)
        ushort[] clData = { 0x0201, 6, 0, 0, 0x17 };
        clData[2] = (ushort)row;
        clData[3] = (ushort)col;

    /// <summary>
    /// Must be called once for creating XLS file header
    /// </summary>
    public void BeginWrite()

    /// <summary>
    /// Ends the writing operation, but do not close the stream
    /// </summary>
    public void EndWrite()

Using the code

The ExcelWriter sample is a console application that writes an XLS file to the name and location specified. The XLS file that is created is simply an empty spreadsheet. Then, you can start to write cell values by calling the WriteCell method with the row number, column number, and a cell value as parameters:

namespace XLSExportDemo
    class Program
        static void Main(string[] args)
            FileStream stream = new FileStream("demo.xls", FileMode.OpenOrCreate);
            ExcelWriter writer = new ExcelWriter(stream);
            writer.WriteCell(0, 0, "ExcelWriter Demo");
            writer.WriteCell(1, 0, "int");
            writer.WriteCell(1, 1, 10);
            writer.WriteCell(2, 0, "double");
            writer.WriteCell(2, 1, 1.5);
            writer.WriteCell(3, 0, "empty");
            writer.WriteCell(3, 1);

The picture below shows the final result of this sample:

The ExcelWriter class supports numeric and text values for the cells, and can be used for exporting data from the database to an XLS file, or for producing an XLS stream from a website on the fly.

I hope this article gives you a head start in working with Excel files from .NET and C#.


  • 03 March 2009 - Initial submission.


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


About the Author

Serhiy Perevoznyk
Belgium Belgium
No Biography provided

You may also be interested in...

Comments and Discussions

Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
QuestionHow can i open it for edit cell value? it just overwrite the whole xls file. Pin
Member 1093195520-Nov-15 21:25
memberMember 1093195520-Nov-15 21:25 
GeneralColor Pin
Member 112053805-Nov-15 19:48
memberMember 112053805-Nov-15 19:48 
GeneralMy vote of 5 Pin
SomnathDas23-Oct-15 7:00
memberSomnathDas23-Oct-15 7:00 
QuestionMaking this Excel file Password Protected Pin
ariez887-Aug-15 3:01
memberariez887-Aug-15 3:01 
QuestionHow we can avoid Protected view and also I want to merge cells rowwise and columnwise Pin
Nisha Alias16-Jun-15 19:59
memberNisha Alias16-Jun-15 19:59 
QuestionRemove Protected View Pin
Alen Toma7-May-15 3:22
memberAlen Toma7-May-15 3:22 
Questionhow to change the worksheet name Pin
thanikasalam5-May-15 22:53
memberthanikasalam5-May-15 22:53 
GeneralGenerate Excel files without using Microsoft Excel Pin
vishnus79016-Mar-15 8:26
membervishnus79016-Mar-15 8:26 
QuestionHow to Parse excel Formula or What is mean by "Parsed expression" in Formula Cell Pin
Member 114262235-Feb-15 22:35
memberMember 114262235-Feb-15 22:35 
QuestionWriting Unicode Pin
Member 1107227110-Sep-14 3:43
memberMember 1107227110-Sep-14 3:43 
Question[My vote of 1] Completely Worthless Pin
MSDEVTECHNIK28-Aug-14 10:56
memberMSDEVTECHNIK28-Aug-14 10:56 
QuestionSee also this: Pin
Dietmar Schoder29-Jul-14 6:26
professionalDietmar Schoder29-Jul-14 6:26 
Questionis anyone still develop this way? Pin
bonjot5-Jul-14 21:41
memberbonjot5-Jul-14 21:41 
QuestionExcel row Limit Pin
csharpi16-May-14 10:58
membercsharpi16-May-14 10:58 
QuestionAttach ExcelDocument to Email Pin
vaughantrebilco10-Apr-14 13:46
membervaughantrebilco10-Apr-14 13:46 
GeneralPerfect Class Pin
William Costa Rodrigues19-Feb-14 13:22
memberWilliam Costa Rodrigues19-Feb-14 13:22 
Questionexcel cannot open the file .xlsx because the file format Pin
bellona88810-Feb-14 22:43
memberbellona88810-Feb-14 22:43 
QuestionSpecial Characters Problem Pin
Emre Can Serteli7-Dec-13 6:14
memberEmre Can Serteli7-Dec-13 6:14 
AnswerRe: Special Characters Problem Pin
Member 1058524714-Jul-14 0:33
memberMember 1058524714-Jul-14 0:33 
QuestionHow to avoid protected view? Pin
Amit Ghorai24-Aug-13 6:04
memberAmit Ghorai24-Aug-13 6:04 
Questionhow to create a new worksheet in the same document Pin
Member 1022196720-Aug-13 20:55
memberMember 1022196720-Aug-13 20:55 
AnswerRe: how to create a new worksheet in the same document Pin
Javier Chan26-Feb-15 19:48
memberJavier Chan26-Feb-15 19:48 
QuestionMy Vote of 5! Pin
jiaxing yee6-Jun-13 15:45
memberjiaxing yee6-Jun-13 15:45 
GeneralMy vote of 5 Pin
hygoh30-May-13 17:28
memberhygoh30-May-13 17:28 
Generalvery cool stuff Pin
Jerry139-May-13 3:31
memberJerry139-May-13 3:31 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 2 Nov 2010
Article Copyright 2009 by Serhiy Perevoznyk
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid