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

  Public Class ExcelWriter

   
Private _wirter As System.IO.FileStream

   
Public Sub New ( ByVal strPath As String )
        _wirter
= New System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate)
   
End Sub

   
''' <summary>
   
''' 写入short数组
   
''' </summary>
   
''' <param name="values"></param>
    Private Sub _writeFile( ByVal values As Short ())
       
For Each v As Short In values
           
Dim b As Byte () = System.BitConverter.GetBytes(v)
            _wirter.Write(b,
0 , b.Length)
       
Next
   
End Sub

   
''' <summary>
   
''' 写文件头
   
''' </summary>
    Public Sub BeginWrite()
        _writeFile(
New Short () { & H809, 8 , 0 , & H10, 0 , 0 })
   
End Sub


   
''' <summary>
   
''' 写文件尾
   
''' </summary>
    Public Sub EndWrite()
        _writeFile(
New Short () { & HA, 0 })
        _wirter.Close()
   
End Sub


   
''' <summary>
   
''' 写一个数字到单元格x,y
   
''' </summary>
   
''' <param name="x"></param>
   
''' <param name="y"></param>
   
''' <param name="value"></param>
    Public Sub WriteNumber( ByVal x As Short , ByVal y As Short , ByVal value As Double )
        _writeFile(
New Short () { & H203, 14 , x, y, 0 })
       
Dim b As Byte () = System.BitConverter.GetBytes(value)
        _wirter.Write(b,
0 , b.Length)
   
End Sub


   
''' <summary>
   
''' 写一个字符到单元格x,y
   
''' </summary>
   
''' <param name="x"></param>
   
''' <param name="y"></param>
   
''' <param name="value"></param>
    Public Sub WriteString( ByVal x As Short , ByVal y As Short , ByVal value As String )
       
Dim b As Byte () = System.Text.Encoding.Default.GetBytes(value)
        _writeFile(
New Short () { & H204, CShort ((b.Length + 8 )), x, y, 0 , CShort (b.Length)})
        _wirter.Write(b,
0 , b.Length)
   
End Sub

End Class

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值