Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#

Generate Excel files without using Microsoft Excel

Rate me:
Please Sign up or sign in to vote.
4.80/5 (93 votes)
22 Jun 2011CPOL2 min read 648.6K   24.3K   269  
A C# class to create Excel files without requiring Microsoft Excel.
Option Strict On
Option Explicit On

Imports System.IO
Imports System.Text

Module Module1
   Dim Stream1 As Stream
   Dim BinaryWriter1 As BinaryWriter
   Dim clBegin() As UShort = {CUShort(Convert.ToInt32("809", 16)), 8, 0, CUShort(Convert.ToInt32("10", 16)), 0, 0}
   Dim clEnd() As UShort = {CUShort(Convert.ToInt32("0A", 16)), 0}

   Private Sub WriteUShortArray(ByVal pValue As UShort())
      For DL As Integer = 0 To pValue.Length - 1
         BinaryWriter1.Write(pValue(DL))
      Next
   End Sub

   Public Sub ExcelWriter(ByVal pStream As Stream)
      Stream1 = pStream
      BinaryWriter1 = New BinaryWriter(Stream1)
   End Sub

   Public Sub WriteCell(ByVal pRow As Integer, ByVal pCol As Integer, ByVal pValue As String)
      Dim clData() As UShort = {CUShort(Convert.ToInt32("204", 16)), 0, 0, 0, 0, 0}
      Dim plainText() As Byte = Encoding.ASCII.GetBytes(pValue)
      clData(1) = CUShort(8 + pValue.Length)
      clData(2) = CUShort(pRow)
      clData(3) = CUShort(pCol)
      clData(5) = CUShort(pValue.Length)
      WriteUShortArray(clData)
      BinaryWriter1.Write(plainText)
   End Sub

   Public Sub WriteCell(ByVal pRow As Integer, ByVal pCol As Integer, ByVal pValue As Integer)
      Dim clData() As UShort = {CUShort(Convert.ToInt32("27E", 16)), 10, 0, 0, 0}
      clData(2) = CUShort(pRow)
      clData(3) = CUShort(pCol)
      WriteUShortArray(clData)
      Dim Value1 As Integer = (pValue << 2) Or 2
      BinaryWriter1.Write(Value1)
   End Sub

   Public Sub WriteCell(ByVal pRow As Integer, ByVal pCol As Integer, ByVal pValue As Double)
      Dim clData() As UShort = {CUShort(Convert.ToInt32("203", 16)), 14, 0, 0, 0}
      clData(2) = CUShort(pRow)
      clData(3) = CUShort(pCol)
      WriteUShortArray(clData)
      BinaryWriter1.Write(pValue)
   End Sub

   Public Sub WriteCell(ByVal pRow As Integer, ByVal pCol As Integer)
      Dim clData() As UShort = {CUShort(Convert.ToInt32("201", 16)), 6, 0, 0, CUShort(Convert.ToInt16("17"))}
      clData(2) = CUShort(pRow)
      clData(3) = CUShort(pCol)
      WriteUShortArray(clData)
   End Sub

   Public Sub BeginWrite()
      WriteUShortArray(clBegin)
   End Sub

   Public Sub EndWrite()
      WriteUShortArray(clEnd)
      BinaryWriter1.Flush()
   End Sub

   Sub Main()
      Try
         Dim FileStream1 As FileStream = New FileStream("demo.xls", FileMode.OpenOrCreate)
         ExcelWriter(FileStream1)

         BeginWrite()
         WriteCell(0, 0, "ExcelWriter Demo")
         WriteCell(1, 0, "int")
         WriteCell(1, 1, 10)
         WriteCell(2, 0, "double")
         WriteCell(2, 1, 1.5)
         WriteCell(3, 0, "empty")
         WriteCell(3, 1)
         EndWrite()

         FileStream1.Close()

      Catch ex As Exception
         MsgBox(Err.Description)
      End Try
   End Sub


End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions