Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks,

right, so the following is a code snippet from someone else who is just using it without bothering how it does what it does. This code is a part of the class that can be used to generate Excel files using C# and NOT using interop. I must say it works, but, i m a bit brain-numbed by the ushort[] arrays and the hex-magic going on. Please have a look and see if you could help me demystify them:

C#
public sealed class ExcelWriter : IDisposable
      {
            // private Stream stream;
            private readonly BinaryWriter writer;

            private readonly ushort[] begin = { 0x0809, 8, 0, 0x10, 0, 0 };
            private readonly ushort[] end = { 0x0A, 00 };

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

            public void WriteCell(int row, int col, string value)
            {
                  ushort[] data = { 0x0204, 0, 0, 0, 0, 0 };
                  var len = value.Length;
                  var plainText = Encoding.ASCII.GetBytes(value);
                  data[1] = (ushort)(8 + len);
                  data[2] = (ushort)row;
                  data[3] = (ushort)col;
                  data[5] = (ushort)len;
                  WriteUshortArray(data);
                  writer.Write(plainText);
            }
...
}


I am interested in dissecting the WriteCell() method in particular, which is actually called by a main method, if u will, to write out values from a DataTable using BinaryWriter and FileStream (pretty much standard stuff). I tried something like this which is a heavily stripped down and de-fattened version of this code:

C#
BinaryWriter bw = new BinaryWriter(new FileStream(@"Z:\temp.xls", FileMode.OpenOrCreate));
            bw.Write(ASCIIEncoding.ASCII.GetBytes("HELLO\r\n"));
            bw.Write(ASCIIEncoding.ASCII.GetBytes("HELLO\r\n"));
            bw.Write(ASCIIEncoding.ASCII.GetBytes("HELLO\r\n"));
            bw.Flush();
            bw.Close();


When I open temp.xls, it opens with a warning about a different format but I can still see my data there. SO I imagine that those little "hexadecimal druids" are doing something mystical that opens the file without any warning.

Please don't point me to "Hexadecimal for Dummies" kinda websites, i know what they are I just want to know what the hell they are doing in an Excel generation code and how? :)

Many thanks...
Posted

1 solution

Someone has found (or reverse engineered) the file format and those ushort values are control codes that tell Excel to start a new cell etc. Since the guy that wrote that code didn't leave a reference to a header file, or a comment about each magic number, it's not really possible to infer more than that. I don't have a source for the Excel file format, but that's what you would need to go and look at to find out why those values are what they are.
 
Share this answer
 
Comments
I.explore.code 20-Oct-11 11:27am    
ah! i see, so wouldn't that mean that even though it explicitly doesn't use interop, it still is dependent on Excel being installed on the server? Coz there has to be something that recieves those control codes and interprets them..?
BobJanova 20-Oct-11 11:45am    
No – the file format doesn't depend on the application being present. If you don't have any software installed that knows how to read the file, you won't be able to do anything with it, but a file format is merely a specification for how to lay out bytes and what a reader should do with them. It doesn't require any particular software to make a file if you have the format (or read it, technically, but reading any legal file is often much harder than writing one, because writing only requires a tiny subset of the possible legal control codes).

The control codes are only interpreted when some application tries to load the file.
I.explore.code 20-Oct-11 11:49am    
Thanks very much mate! Appreciate the help...

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900