Write Date value in ExcelWriter

People asking me how to insert the date value into Excel coulmn, but as a date, not just as a text in my C# ExcelWriter class.
I extended this class and added 2 additional methods:

  •  public void WriteFormat(string value)
  •  public void WriteCell(int row, int col, DateTime value, int formatIndex)

// C# ExcelWriter class v1.1
// by Serhiy Perevoznyk, 2008-2009

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XLSExportDemo
{
///
/// Produces Excel file without using Excel
///

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++)
writer.Write(value[i]);
}

///
/// Initializes a new instance of the class.
///

/// The stream.
public ExcelWriter(Stream stream)
{
this.stream = stream;
writer = new BinaryWriter(stream);
}

///
/// Writes the text cell value.
///

/// The row.
/// The col.
/// The string value.
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;
WriteUshortArray(clData);
writer.Write(plainText);
}

///
/// Writes the date format.
///

/// The date format value.
public void WriteFormat(string value)
{
ushort[] clData = { 0x001E, 0 };
byte[] plainText = Encoding.ASCII.GetBytes(value);
int iLen = plainText.Length;
clData[1] = (ushort)(1 + iLen);
WriteUshortArray(clData);
writer.Write((byte)iLen);
writer.Write(plainText);
}

///
/// Writes the integer cell value.
///

/// The row number.
/// The column number.
/// The value.
public void WriteCell(int row, int col, int value)
{
ushort[] clData = { 0x027E, 10, 0, 0, 0 };
clData[2] = (ushort)row;
clData[3] = (ushort)col;
WriteUshortArray(clData);
int iValue = (value << 2) | 2;
writer.Write(iValue);
}

///
/// Writes the cell.
///

/// The row.
/// The col.
/// The value.
/// Index of the date format.
public void WriteCell(int row, int col, DateTime value, int formatIndex)
{
DateTime baseDate = new DateTime(1899, 12, 31);
TimeSpan ts = value - baseDate;

ushort days = (ushort)(ts.Days + 1);
ushort[] clData = { 0x0002, 09, 0, 0};
clData[2] = (ushort)row;
clData[3] = (ushort)col;
WriteUshortArray(clData);
writer.Write((byte)0x0);
byte indexValue = (byte)(formatIndex & 0x3F);
writer.Write((byte)indexValue);
writer.Write((byte)0x0);
writer.Write(days);
}

///
/// Writes the double cell value.
///

/// The row number.
/// The column number.
/// The value.
public void WriteCell(int row, int col, double value)
{
ushort[] clData = { 0x0203, 14, 0, 0, 0 };
clData[2] = (ushort)row;
clData[3] = (ushort)col;
WriteUshortArray(clData);
writer.Write(value);
}

///
/// Writes the empty cell.
///

/// The row number.
/// The column number.
public void WriteCell(int row, int col)
{
ushort[] clData = { 0x0201, 6, 0, 0, 0x17 };
clData[2] = (ushort)row;
clData[3] = (ushort)col;
WriteUshortArray(clData);
}

///
/// Must be called once for creating XLS file header
///

public void BeginWrite()
{
WriteUshortArray(clBegin);
}

///
/// Ends the writing operation, but do not close the stream
///

public void EndWrite()
{
WriteUshortArray(clEnd);
writer.Flush();
}
}
}

and example of usage:

// This sample demonstrates how to create a file using the Microsoft Excel 
// Binary Interchange File Format (BIFF).
// If this program works, it was written by Serhiy Perevoznyk.
// If not, I don't know who wrote it.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XLSExportDemo
{
class Program
{
static void Main(string[] args)
{
FileStream stream = new FileStream("demo.xls", FileMode.OpenOrCreate);
ExcelWriter writer = new ExcelWriter(stream);
writer.BeginWrite();
writer.WriteFormat(@"dd\-mm\-yyyy"); //index 0
writer.WriteFormat(@"dd/mm/yyyy");
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, "date");
writer.WriteCell(3, 1, DateTime.Now, 1);
writer.EndWrite();
stream.Close();
}
}
}
Advertisements

5 thoughts on “Write Date value in ExcelWriter

  1. Hi, is there a way to get cells to autofit around text? So that all of the text written to the cells can be seen without having to make the cells manually?

    Like

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