The following code demonstrates a way to generate an XLS file directly from a file structure that Excel can recognize, so that you don't reference troublesome OLE.
1.using System;
2.using System.Collections.Generic;
3.using System.Text;
4.namespace ConsoleApplication16
5.{
6. Class Program
7. {
8. static void Main (string [] args)
9. {
10.//method to generate Excel files without OLE
Excelwriter Excel = new Excelwriter (@ "C:/test.xls");
Excel. BeginWrite ();
Excel. WriteString (0, 0, "Name");
Excel. WriteString (0, 1, "Score");
Excel. WriteString (1, 0, "Jinjazz");
Excel. Writenumber (1, 1, 100);
Excel. WriteString (2, 0, "tourist");
Excel. Writenumber (2, 1, 0);
Excel. EndWrite ();
20.}
21.}
public class Excelwriter
23. {
System.IO.FileStream _wirter;
Public Excelwriter (String strpath)
26. {
_wirter = new System.IO.FileStream (strpath, System.IO.FileMode.OpenOrCreate);
28.}
///<summary>
30.///Write short array
///</summary>
///<param name= "values" ></param>
private void _writefile (short [] values)
34. {
foreach (short v in Values)
36. {
Panax Notoginseng. Byte [] b = System.BitConverter.GetBytes (v);
_wirter. Write (b, 0, b.length);
39.}
40.}
///<summary>
42.///Write File header
///</summary>
public void BeginWrite ()
45. {
_writefile (new short [] {0x809, 8, 0, 0x10, 0, 0});
47.}
///<summary>
49.///write the end of the file
///</summary>
The public void EndWrite ()
52. {
_writefile (new short [] {0xa, 0});
_wirter. Close ();
55.}
///<summary>
57.///write a number to cell x,y
///</summary>
///<param name= "x" ></param>
///<param name= "y" ></param>
///<param name= "value" ></param>
"public void Writenumber" (short x, short y, double value)
63. {
_writefile (new short [] {0x203, x, y, 0});
A. Byte [] b = System.BitConverter.GetBytes (value);
_wirter. Write (b, 0, b.length);
67.}
///<summary>
69.///write a character to cell x,y
///</summary>
///<param name= "x" ></param>
///<param name= "y" ></param>
///<param name= "value" ></param>
WriteString public void (short x, short y, String value)
75. {
A. Byte [] b = System.Text.Encoding.Default.GetBytes (value);
_writefile (new short [] {0x204, (short) (B.length + 8), X, y,0, (short) b.length});
_wirter. Write (b, 0, b.length);
79.}
80.}
81.}
82.