Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6348637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:27:34+00:00 2026-05-24T21:27:34+00:00

I have a DataTable with multiple columns including AccountNumber, Year, and Month. I am

  • 0

I have a DataTable with multiple columns including AccountNumber, Year, and Month. I am exporting the information in this table to an Excel workbook. Since this table has the potential to be extremely large, I must check the number of records in the table (since Excel can only have 65536 rows or something like that. If the original table is small enough, I need to put all of the records in a single worksheet. If there are too many records for one worksheet, I need to separate the records into multiple worksheets based on AccountNumber. Additionally, if there are too many records for a specific AccountNumber then I need to split that AccountNumber into multiple worksheets based on Year. If a specific Year still has too many records then I have to split them by Month.

For example:

If I have a total of 500,000 records, I must split them by AccountNumber getting:

Worksheet Name —- Number of Records

  • 1111 —- 150,000
  • 2222 —- 50,000
  • 3333 —- 100,000

Then I would need to split Accounts 1111, and 3333 into multiple worksheets based on the Year. I would then have something like this:

Worksheet Name —- Number of Records

  • 1111 – 2010 —- 50,000
  • 1111 – 2011 —- 100,000
  • 2222 —- 50,000
  • 3333-2010 —- 50,000
  • 3333-2011 —- 50,000

Then, Since 1111 – 2011 is still too large I would have to split that one based on Month, finally giving:

Worksheet Name —- Number of Records

  • 1111-2010 —- 50,000
  • 1111-201105 —- 30,000
  • 1111-201106 —- 30,000
  • 1111-201107 —- 40,000
  • 2222 —- 50,000
  • 3333-2010 —- 50,000
  • 3333-2011 —- 50,000

The code that is being used to create the Excel file is a project that was written by my company. To make it simple, the function accepts a DataTable and writes out the records in the DataTable in the format of an Excel spreadsheet. Any ideas on how I can do this without making anymore calls to the database? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:27:35+00:00Added an answer on May 24, 2026 at 9:27 pm

    You can use LINQ-to-DataSet to group the DataRows accordingly and add them to separate DataTables with less rows than 65536.

    Have a look at this working sample which also generates the correct File/DataTable-Names:

    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
    

    This sample uses randomly 4 static AccountNumbers in the period 2005-2012 with 500.000 records. It generates 28 DataTables and clones all DataRows in only 1,6 seconds since LINQ-to-DataSet is working entirely in memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple excel files which I open as datatable I want to merge
I'm using ICEFaces. I have datatable with multiple columns. One I load the page
I have a datatable containing system profile names. This table is then assigned as
I have a datatable that contains rows of transaction data for multiple users. Each
I have the following data in a database table, Columns : Date, Hour(from 1
I've a datatable which has a single text column 'Title' which can have multiple
I have a data table 44 columns wide that I need to write to
I have some DataGridViews each displaying one DataTable from a DataSet with multiple DataTables.
This is very interesting.. I want to have multiple gridviews in a panel. and
I have a dynamic data table in JSF which has multiple input elements which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.