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 8769353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:19:13+00:00 2026-06-13T17:19:13+00:00

A rather higeisch dataset with 16000 x 12 entries needs to be dumped into

  • 0

A rather higeisch dataset with 16000 x 12 entries needs to be dumped into a worksheet.

I use the following function now:

        for (int r = 0; r < dt.Rows.Count; ++r)
        {
            for (int c = 0; c < dt.Columns.Count; ++c)
            {
                worksheet.Cells[c + 1][r + 1] = dt.Rows[r][c].ToString();
            }
        }

I rediced the example to the center piece


Here is what i implemented after reading the suggestion from Dave Zych.
This works great.

    private static void AppendWorkSheet(Excel.Workbook workbook, DataSet data, String tableName)
    {
        Excel.Worksheet worksheet;
        if (UsedSheets == 0) worksheet = workbook.Worksheets[1];
        else worksheet = workbook.Worksheets.Add();
        UsedSheets++;
        DataTable dt = data.Tables[0];
        var valuesArray = new object[dt.Rows.Count, dt.Columns.Count];

        for (int r = 0; r < dt.Rows.Count; ++r)
        {
            for (int c = 0; c < dt.Columns.Count; ++c)
            {
                valuesArray[r, c] = dt.Rows[r][c].ToString();
            }
        }
        Excel.Range c1 = (Excel.Range)worksheet.Cells[1, 1];
        Excel.Range c2 = (Excel.Range)worksheet.Cells[dt.Rows.Count, dt.Columns.Count];
        Excel.Range range = worksheet.get_Range(c1, c2);
        range.Cells.Value2 = valuesArray;
        worksheet.Name = tableName;
    }
  • 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-06-13T17:19:14+00:00Added an answer on June 13, 2026 at 5:19 pm

    Build a 2D array of your values from your DataSet, and then you can set a range of values in Excel to the values of the array.

    object valuesArray = new object[dataTable.Rows.Count, dataTable.Columns.Count];
    for(int i = 0; i < dt.Rows.Count; i++)
    {
        //If you know the number of columns you have, you can specify them this way
        //Otherwise use an inner for loop on columns
        valuesArray[i, 0] = dt.Rows[i]["ColumnName"].ToString();
        valuesArray[i, 1] = dt.Rows[i]["ColumnName2"].ToString();
        ...
    }
    
    //Calculate the second column value by the number of columns in your dataset
    //"O" is just an example in this case
    //Also note: Excel is 1 based index
    var sheetRange = worksheet.get_Range("A2:O2", 
        string.Format("A{0}:O{0}", dt.Rows.Count + 1));
    
    sheetRange.Cells.Value2 = valuesArray;
    

    This is much, much faster than looping and setting each cell individually. If you’re setting each cell individually, you have to talk to Excel through COM (for lack of a better phrase) for each cell (which in your case is ~192,000 times), which is incredibly slow. Looping, building your array and only talking to Excel once removes much of that overhead.

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

Sidebar

Related Questions

Rather than use an ORM, I am considering the following approach in Python and
Rather odd problem in that I cannot use the data variable (the information returned
Rather than reinvent the wheel I would like to use Outlook to manage my
Rather than use WebGL, NaCl, Flash or developing a custom browser plugin, I'm interested
Rather than use a full-blown PHP MVC, I'm designing one that will best-fit my
Rather than just writing a new function called import() i'd like to know if
Rather than using a Handler(.ashx), is it possible to use a web service(.asmx) to
Rather than outputting the expected string of Bar , the following code outputs what
I have a rather standard situation where I want to use a templated class
Rather than use another widget I want to code the method to include the

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.