科创茶话
 
【VB.NET】直接通过文件结构生成xls文件
93° 2009-7-16 02:16:57
这个是给某人做项目的时候写的。

原本是C#的,我改写成VB.NET了,并且编译了一个dll。

--->>>>>>>><<<<<<<<---

测试通过,很好用

原文地址http://blog.csdn.net/jinjazz/archive/2008/08/01/2753869.aspx

调用方法:

Private Sub button1_Click_1(ByVal sender As Object, ByVal e As EventArgs)
    Dim excel As New ExcelWriter(&quot;e:\test.xls&quot;)
    excel.BeginWrite
    excel.WriteString(0, 0, &quot;1&quot;)
    excel.WriteString(0, 1, &quot;2&quot;)
    excel.WriteString(1, 0, &quot;3&quot;)
    excel.WriteNumber(1, 1, 4)
    excel.WriteNumber(2, 1, 5)
    excel.EndWrite
End Sub


这个,要写多维数组到xls就简单了吧 = = 直接循环 嗯

下面是代码

Public Class Exel_Writer
    Dim _r As System.IO.FileStream

    Public Function Init(ByVal strPath As String)
        _r = New System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate)
        Return Nothing
    End Function

    Public Sub BeginWrite()
        Me._writeFile(New Short() {&H809, 8, 0, &H10, 0, 0})
    End Sub


    Private Function _writeFile(ByVal values As Short())
        Dim v As Short
        For Each v In values
            Dim b As Byte() = BitConverter.GetBytes(v)
            Me._r.Write(b, 0, b.Length)
        Next
        Return Nothing
    End Function

    Public Function EndWrite()
        Dim CS As Short() = New Short(2 - 1) {}
        CS(0) = 10
        Me._writeFile(CS)
        Me._r.Close()
        Return Nothing
    End Function

    Public Function WriteNumber(ByVal x As Short, ByVal y As Short, ByVal value As Double)
        Dim CS As Short() = New Short(5 - 1) {}
        CS(0) = &H203
        CS(1) = 14
        CS(2) = x
        CS(3) = y
        Me._writeFile(CS)
        Dim b As Byte() = BitConverter.GetBytes(value)
        Me._r.Write(b, 0, b.Length)
        Return Nothing
    End Function

    Public Function WriteString(ByVal x As Short, ByVal y As Short, ByVal value As String)
        Dim b As Byte() = System.Text.Encoding.Default.GetBytes(value)
        Dim CS As Short() = New Short(6 - 1) {}
        CS(0) = &H204
        CS(1) = CShort((b.Length + 8))
        CS(2) = x
        CS(3) = y
        CS(5) = CShort(b.Length)
        Me._writeFile(CS)
        Me._r.Write(b, 0, b.Length)
        Return Nothing
    End Function
End Class


附:原C#代码



24887
2009-7-16 02:18:58
1楼
Button1...我又见到了……

话说您直接Imports System.IO不就完了,每次都写一遍累不累(虽然我们有IntelliSense……)

= =...还有无爱的Class1……--~~~~
93°(作者)
2楼
……
好吧,button1是囧3程序特色
3楼
xls还是xlsx?
4楼
_writeFile(new short[] { 0x809, 8, 0, 0x10, 0, 0 }
这种让人看到心里不踏实。

其实写成带分割符的txt, 然后一个批处理导入到excel中就搞定了。
5楼
引用第3楼小光Telnet于2009-07-16 08:01发表的  :
xls还是xlsx?

显然是XLS...XLSX直接imports下XML库就完了
2009-7-18 13:58:25
93°(作者)
6楼
话说……没人鸟我么 = =
7楼
不错,难以不鸟你,现在授予93°  “XLS勋章”,以资鼓励。
2009-7-21 10:59:28
8楼
厉害,结构都掌握了.难道想玩fuzz?
2009-8-15 10:52:40
9楼
有C++版的吗?要是有能在单片机上运行的更好
93°(作者)
10楼
引用第9楼warmonkey于2009-08-15 10:52发表的  :
有C++版的吗?要是有能在单片机上运行的更好

把system.IO换成fputc (pia)就可以了。。。。。

想参与大家的讨论?现在就 登录 或者 注册

{{submitted?"":"投诉"}}
请选择违规类型:
{{reason.description}}
支持的图片格式:jpg, jpeg, png