[ClosedXml]几种可以批量写入的数据类型

 1 Imports System.IO
 2 Imports ClosedXML.Excel
 3 
 4 Module Program
 5 
 6     Sub Main(args As String())
 7         Using book As New XLWorkbook()
 8             Dim sheet = book.Worksheets.Add()
 9             Dim rowCount = 10
10             Dim colCount = 4
11             ' ==========================================
12             ' 二维数组:打咩
13             ' ==========================================
14             'Dim data(0 To rowCount - 1, 0 To colCount - 1) As Object
15             'For i As Integer = 0 To rowCount - 1
16             '    For j As Integer = 0 To colCount - 1
17             '        data(i, j) = i & j
18             '    Next
19             'Next
20             ' ==========================================
21             ' List(Of List(Of Object)) :可行
22             ' ==========================================
23             'Dim data As New List(Of List(Of Object))
24             'For i As Integer = 0 To rowCount - 1
25             '    Dim item As New List(Of Object)
26             '    For j As Integer = 0 To colCount - 1
27             '        item.Add(i & j)
28             '    Next
29             '    data.Add(item)
30             'Next
31 
32             ' ==========================================
33             ' List(Of class) :可行
34             ' ==========================================
35             'Dim data As New List(Of TestCLass)
36             'For i As Integer = 0 To rowCount - 1
37             '    Dim item As New TestCLass(i.ToString(), i, CDec(i), DateTime.Now.AddDays(i))
38             '    data.Add(item)
39             'Next
40 
41             ' ==========================================
42             ' DataTable :可行
43             ' ==========================================
44             Dim data As New System.Data.DataTable
45             data.Columns.Add(New System.Data.DataColumn("A", GetType(String)))
46             data.Columns.Add(New System.Data.DataColumn("B", GetType(Integer)))
47             data.Columns.Add(New System.Data.DataColumn("C", GetType(Decimal)))
48             data.Columns.Add(New System.Data.DataColumn("D", GetType(DateTime)))
49             For i As Integer = 0 To rowCount - 1
50                 Dim item = data.NewRow
51                 item.ItemArray = New Object() {i.ToString(), i, i, DateTime.Now.AddDays(i)}
52                 data.Rows.Add(item)
53             Next
54 
55             sheet.Cell(1, 1).InsertData(data)
56             Dim filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.xlsx")
57             book.SaveAs(filePath)
58         End Using
59     End Sub
60 
61     Class TestCLass
62         Public Property A As String
63         Public Property B As Integer
64         Public Property C As Decimal
65         Public Property D As DateTime
66 
67         Public Sub New(a As String, b As Integer, c As Decimal, d As DateTime)
68             Me.A = a
69             Me.B = b
70             Me.C = c
71             Me.D = d
72         End Sub
73 
74     End Class
75 End Module

 

posted on 2024-07-17 16:04  Osiris4Net  阅读(4)  评论(0编辑  收藏  举报

导航