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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:38:18+00:00 2026-06-18T14:38:18+00:00

I am trying to save a DataTable into an excel sheet.. my code is

  • 0

I am trying to save a DataTable into an excel sheet.. my code is like this..

Excel.Range range = xlWorkSheet.get_Range("A2");
range = range.get_Resize(dtExcel.Rows.Count, dtExcel.Columns.Count);
object[,] rng1 = new object[dtExcel.Rows.Count, dtExcel.Columns.Count];

Excel range requires range value as array[,] but I have the DataTable as jagged array[][].

object[][] rng2 = dtExcel.AsEnumerable().Select(x => x.ItemArray).ToArray();

Is there any built-in function to directly convert the jagged array[][] to a 2D array[][] ?
Iterating through Excel, DataTable and assigning seems slower with bulk data..

Also I don’t want to setup querying with DSN for excel.. I chose excel storage to avoid the configuring of any databases.. 😛
I found a detailed explanation of ways of writing data to excel here..
http://support.microsoft.com/kb/306023

  • 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-18T14:38:19+00:00Added an answer on June 18, 2026 at 2:38 pm

    At last I used NPOI library for this. It is quite simple and free.

    The code to convert DataTable to excel as follows.

    HSSFWorkbook hssfworkbook = new HSSFWorkbook();
            foreach (DataTable dt in DataSource.Tables)
            {
                ISheet sheet1 = hssfworkbook.CreateSheet(dt.TableName);
    
                //Set column titles
                IRow headRow = sheet1.CreateRow(0); 
                for (int colNum = 0; colNum < dt.Columns.Count; colNum++)
                {
                    ICell cell = headRow.CreateCell(colNum);
                    cell.SetCellValue(dt.Columns[colNum].ColumnName);
                }
    
                //Set values in cells
                for (int rowNum = 1; rowNum <= dt.Rows.Count; rowNum++)
                {
                    IRow row = sheet1.CreateRow(rowNum);
                    for (int colNum = 0; colNum < dt.Columns.Count; colNum++)
                    {
                        ICell cell = row.CreateCell(colNum);
                        cell.SetCellValue(dt.Rows[rowNum - 1][colNum].ToString());
                    }
                }
    
                // Resize column width to show all data
                for (int colNum = 0; colNum < dt.Columns.Count; colNum++)
                {
                    sheet1.AutoSizeColumn(colNum);
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to save multiple image using this code. But instead its saving
I'm trying to save some data into an array but unfortunately all the data
I'm trying to insert data from a datatable to an Excel file from my
I'm trying to save the excel file. It working fine if i save the
I am not trying to save dataset or datatable to a xml file rather
Im trying to save a path into my input varaible, but its not reading
I am trying to create the world most simple dataTable using JSF tomahawk like
When trying to save an uploaded file, I get this error and are having
Im trying to save some data into a xml file using the following PHP
Good morning all, i have a new question: I'm trying to save into mysql

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.