好文章保存"直接通过excel可以识别的文件结构生成xls文件的方法,这样就可以不引用麻烦的ole了"...

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication16
ExpandedBlockStart.gifContractedBlock.gif
{
    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//不通过OLE生成excel文件的方法
            ExcelWriter excel = new ExcelWriter(@"c:\test.xls");
            excel.BeginWrite();
            excel.WriteString(
00"Name");
            excel.WriteString(
01"Score");
            excel.WriteString(
10"jinjazz"); 
            excel.WriteNumber(
11100);
            excel.WriteString(
20"游客");
            excel.WriteNumber(
210);
            excel.EndWrite();
        }

    }

    
public class ExcelWriter
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        System.IO.FileStream _wirter;
        
public ExcelWriter(string strPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            _wirter 
= new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate);
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 写入short数组
        
/// </summary>
        
/// <param name="values"></param>

        private void _writeFile(short[] values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
foreach (short v in values)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
byte[] b = System.BitConverter.GetBytes(v);
                _wirter.Write(b, 
0, b.Length);
            }

        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 写文件头
        
/// </summary>

        public void BeginWrite()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            _writeFile(
new short[] 0x809800x1000 });
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 写文件尾
        
/// </summary>

        public void EndWrite()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            _writeFile(
new short[] 0xa0 });
            _wirter.Close();
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            _writeFile(
new short[] 0x20314, x, y, 0 });
            
byte[] b = System.BitConverter.GetBytes(value);
            _wirter.Write(b, 
0, b.Length);
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
byte[] b = System.Text.Encoding.Default.GetBytes(value);
ExpandedSubBlockStart.gifContractedSubBlock.gif            _writeFile(
new short[] 0x204, (short)(b.Length + 8), x, y,0, (short)b.Length });
            _wirter.Write(b, 
0, b.Length);
        }

    }

}

 

直接通过excel可以识别的文件结构生成xls文件的方法,这样就可以不引用麻烦的ole了

引自http://topic.csdn.net/u/20080801/15/fefe7452-068c-43bf-a9e3-ea707e5bca0e.html

转载于:https://www.cnblogs.com/gleamy_ming/articles/1270911.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值