C# 通过文件结构直接生成xls(Excel)文件

本文介绍了一种使用C#纯代码方式生成Excel文件的方法,无需依赖OLE,通过手动构造Excel文件的二进制流实现。文章提供了具体实现的源代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下代码演示了 直接通过excel可以识别的文件结构生成xls文件的方法,这样就可以不引用麻烦的ole了。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication16
{
    class Program
     {
        static void Main(string[] args)
         {
            //不通过OLE生成excel文件的方法
             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, "游客");
             excel.WriteNumber(2, 1, 0);
             excel.EndWrite();
         }
     }
    public class ExcelWriter
     {
         System.IO.FileStream _wirter;
        public ExcelWriter(string strPath)
         {
             _wirter = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate);
         }
        /// <summary>
        /// 写入short数组
        /// </summary>
        /// <param name="values"></param>
        private void _writeFile(short[] values)
         {
            foreach (short v in values)
             {
                byte[] b = System.BitConverter.GetBytes(v);
                 _wirter.Write(b, 0, b.Length);
             }
         }
        /// <summary>
        /// 写文件头
        /// </summary>
        public void BeginWrite()
         {
             _writeFile(new short[] { 0x809, 8, 0, 0x10, 0, 0 });
         }
        /// <summary>
        /// 写文件尾
        /// </summary>
        public void EndWrite()
         {
             _writeFile(new short[] { 0xa, 0 });
             _wirter.Close();
         }
        /// <summary>
        /// 写一个数字到单元格x,y
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        public void WriteNumber(short x, short y, double value)
         {
             _writeFile(new short[] { 0x203, 14, x, y, 0 });
            byte[] b = System.BitConverter.GetBytes(value);
             _wirter.Write(b, 0, b.Length);
         }
        /// <summary>
        /// 写一个字符到单元格x,y
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        public void WriteString(short x, short y, string value)
         {
            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);
         }
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷月宫主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值