您可以使用LINQ-to-DataSet分組相應的DataRows
並將其添加到與更少的行超過65536
分離
看一看這方面的工作樣本也產生了正確的文件/數據表 - 姓名:
Private Shared sampleAccountNumbers As List(Of Int32) = {1111, 2222, 3333, 4444}.ToList
Private Const MAX_ROWS = 65536
Private tblSource As New DataTable()
Private allResultTables As New List(Of DataTable)
Private Sub SplitTableRows()
Dim accQuery = _
From row In tblSource
Group row By AccountNumber = row("AccountNumber") Into AccNumGroup = Group
For Each acc In accQuery
If acc.AccNumGroup.Count > MAX_ROWS Then
Dim yearQuery = _
From row In acc.AccNumGroup
Group row By Year = row("Year") Into YearGroup = Group
For Each y In yearQuery
If y.YearGroup.Count > MAX_ROWS Then
Dim monthQuery = _
From row In y.YearGroup
Group row By Month = row("Month") Into MonthGroup = Group
For Each m In monthQuery
If m.MonthGroup.Count > MAX_ROWS Then
'split by days or whatever...'
Else
Dim tblMonth = m.MonthGroup.CopyToDataTable()
tblMonth.TableName = String.Format("{0}-{1}{2}", acc.AccountNumber, y.Year, m.Month)
allResultTables.Add(tblMonth)
End If
Next
Else
Dim tblYear = y.YearGroup.CopyToDataTable()
tblYear.TableName = String.Format("{0}-{1}", acc.AccountNumber, y.Year)
allResultTables.Add(tblYear)
End If
Next
Else
Dim tblAcc = acc.AccNumGroup.CopyToDataTable()
tblAcc.TableName = acc.AccountNumber
allResultTables.Add(tblAcc)
End If
Next
End Sub
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
InitSourceDataTable()
If tblSource.Rows.Count > MAX_ROWS Then
SplitTableRows()
Else
allResultTables.Add(tblSource)
End If
For Each tbl In allResultTables
'call your generate-excel-method...'
Next
End Sub
Private Sub InitSourceDataTable()
Dim rndAccNum As New Random(Date.Now.Millisecond)
Dim rndYear As New Random(Date.Now.Millisecond)
Dim rndMonth As New Random(Date.Now.Millisecond)
tblSource.Columns.Add(New DataColumn("ID", GetType(Integer)))
tblSource.Columns.Add(New DataColumn("AccountNumber", GetType(Integer)))
tblSource.Columns.Add(New DataColumn("Year", GetType(Integer)))
tblSource.Columns.Add(New DataColumn("Month", GetType(Integer)))
For i As Int32 = 1 To 500000
Dim newRow = tblSource.NewRow
newRow("ID") = i
newRow("AccountNumber") = sampleAccountNumbers(rndAccNum.Next(0, sampleAccountNumbers.Count))
newRow("Year") = rndAccNum.Next(2005, 2012)
newRow("Month") = rndMonth.Next(1, 13)
tblSource.Rows.Add(newRow)
Next
End Sub
此示例使用在2005 - 2012年期間以500.000記錄隨機4靜態AccountNumbers。由於LINQ-to-DataSet完全在內存中工作,因此它僅生成28個DataTable並在1.6秒內克隆所有DataRows。
僅供參考,Excel 2007和更高版本可以有100萬行。 –
是的,但我們公司的一些個人電腦仍在使用2003 –
似乎它可能會更容易,只是升級他們;) –