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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:23:57+00:00 2026-06-09T12:23:57+00:00

I have DataTable object. How can I export it into an XLS file? I

  • 0

I have DataTable object. How can I export it into an XLS file?
I tried to render it via DataGrid

DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
dgGrid.RenderControl(hw);

but the file is very large and the OutOfMemoryException appears.

I can use http://epplus.codeplex.com/.
I need C# function.

  • 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-09T12:23:58+00:00Added an answer on June 9, 2026 at 12:23 pm

    Ok, find a solution here: http://bytesofcode.hubpages.com/hub/Export-DataSet-and-DataTable-to-Excel-2007-in-C

    Just download epplus library and call method:

    private void GenerateExcel(DataTable dataToExcel, string excelSheetName)
            {
                string fileName = "ByteOfCode";
                string currentDirectorypath = Environment.CurrentDirectory;
                string finalFileNameWithPath = string.Empty;
    
                fileName = string.Format("{0}_{1}", fileName, DateTime.Now.ToString("dd-MM-yyyy"));
                finalFileNameWithPath = string.Format("{0}\\{1}.xlsx", currentDirectorypath, fileName);
    
                //Delete existing file with same file name.
                if (File.Exists(finalFileNameWithPath))
                    File.Delete(finalFileNameWithPath);
    
                var newFile = new FileInfo(finalFileNameWithPath);
    
                //Step 1 : Create object of ExcelPackage class and pass file path to constructor.
                using (var package = new ExcelPackage(newFile))
                {
                    //Step 2 : Add a new worksheet to ExcelPackage object and give a suitable name
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(excelSheetName);
    
                    //Step 3 : Start loading datatable form A1 cell of worksheet.
                    worksheet.Cells["A1"].LoadFromDataTable(dataToExcel, true, TableStyles.None);
    
                    //Step 4 : (Optional) Set the file properties like title, author and subject
                    package.Workbook.Properties.Title = @"This code is part of tutorials available at http://bytesofcode.hubpages.com";
                    package.Workbook.Properties.Author = "Bytes Of Code";
                    package.Workbook.Properties.Subject = @"Register here for more http://hubpages.com/_bytes/user/new/";
    
                    //Step 5 : Save all changes to ExcelPackage object which will create Excel 2007 file.
                    package.Save();
    
                    MessageBox.Show(string.Format("File name '{0}' generated successfully.", fileName)
                        , "File generated successfully!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGrid , which is bound to a DataTable object. The user
I would like to have an ace:datatable where I can compare an old object
I have simple methods to export DataTable to XLS using string. Number of columns
I have a DataTable object. Every column is of type string. Using LINQ, how
Importing a spreadsheet I have filled a DataTable object with that data and returns
I have a Business Object which contains a DataTable among other properties. I pass
I have a array of string say: String[] Fields=new String[]{RowField,RowField1} In which I can
I have a DataTable object with 5 columns returned to me by a service.
Say i have a DataTable object with data from the database, and I need
I'm using JSF and I have a complex datatable where the user can sort

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.