Click here to Skip to main content
11,635,098 members (76,049 online)
Click here to Skip to main content
Add your own
alternative version

Generate Excel files without using Microsoft Excel

, 22 Jun 2011 CPOL 310.5K 17.3K 252
A C# class to create Excel files without requiring Microsoft Excel.
ConsoleApplication1.zip
ConsoleApplication1
ConsoleApplication1
bin
Debug
ConsoleApplication1.exe
ConsoleApplication1.pdb
ConsoleApplication1.vshost.exe
demo.xls
ConsoleApplication1.vbproj.user
My Project
Application.myapp
Settings.settings
obj
Debug
ConsoleApplication1.exe
ConsoleApplication1.pdb
ConsoleApplication1.Resources.resources
ConsoleApplication1.vbproj.GenerateResource.Cache
ConsoleApplication1.vbproj.ResolveComReference.cache
TempPE
ConsoleApplication1.suo
XLSExportDemo.zip
XLSExportDemo
XLSExportDemo
Properties
XLSExportDemo_Update.zip
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)

Share

About the Author

Serhiy Perevoznyk
Architect
Belgium Belgium
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 22 Jun 2011
Article Copyright 2009 by Serhiy Perevoznyk
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid