Generate XLS files directly from file structure

Source: Internet
Author: User
Tags excelwriter ole

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.