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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:40:23+00:00 2026-05-13T17:40:23+00:00

I have a spreadsheet with multiple pages in it.When I click on a button

  • 0

I have a spreadsheet with multiple pages in it.When I click on a button I need to open this spreadsheet and write all the data(dataset/datatable) returned from the database into one of the pages in the spreadsheet.I saw so many articles for exporting dataset to a new excel sheet.how do i open an existing spreadsheet and write a dataset into it using asp.net/C#?

Please help..

Thanks.

UPDATE:

Basically I have the following code to export a dataset to a new excel sheet.

private void createDataInExcel(DataSet ds)
{

    Application oXL;

    _Workbook oWB;

    _Worksheet oSheet;

    Range oRng;

    string strCurrentDir = Server.MapPath(".") + "\\excelreports\\";

    try
    {

        oXL = new Application();

        oXL.Visible = false;

        //Get a new workbook.

        oWB = (_Workbook)(oXL.Workbooks.Add(Missing.Value));

        oSheet = (_Worksheet)oWB.ActiveSheet;

        //System.Data.DataTable dtGridData=ds.Tables[0];

        int iRow = 2;

        if (ds.Tables[0].Rows.Count > 0)
        {


            for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
            {

                oSheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].ColumnName;

            }

            // For each row, print the values of each column.

            for (int rowNo = 0; rowNo < ds.Tables[0].Rows.Count; rowNo++)
            {

                for (int colNo = 0; colNo < ds.Tables[0].Columns.Count; colNo++)
                {

                    oSheet.Cells[iRow, colNo + 1] = ds.Tables[0].Rows[rowNo][colNo].ToString();

                }
                iRow++;

            }              

        }

        oRng = oSheet.get_Range("A1", "IV1");

        oRng.EntireColumn.AutoFit();

        oXL.Visible = false;

        oXL.UserControl = false;

        string strFile = "excelreport" + DateTime.Now.Ticks.ToString() + ".xls";//+

        oWB.SaveAs(strCurrentDir +strFile, XlFileFormat.xlWorkbookNormal, null, null, false, false, XlSaveAsAccessMode.xlShared, false, false, null,null, null);

        // Need all following code to clean up and remove all references!!!

        oWB.Close(null, null, null);

        oXL.Workbooks.Close();

        oXL.Quit();

        Marshal.ReleaseComObject(oRng);

        Marshal.ReleaseComObject(oXL);

        Marshal.ReleaseComObject(oSheet);

        Marshal.ReleaseComObject(oWB);


    }

    catch (Exception theException)
    {

        Response.Write(theException.Message);

    }
    Response.Write("data exported");

}

Is it possible to improve the above code to write the dataset to an existing sheet?Also with the above code its taking about a minute to write the data into excel sheet..I do not understand why is it taking that long.

  • 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-13T17:40:23+00:00Added an answer on May 13, 2026 at 5:40 pm

    not 100% sure where you are with your code, however using the excel com object referenced in your project you can open a workbook using the Workbooks._Open method, then you can get the sheet by name using the sheets collection of the workbook and the get_Item.

    if you need to add a sheet to the workbook you can use the add on the sheets collect.

    Maybe if you post the code you have we can suggest where to improve it.

    this line

    oWB = (_Workbook)(oXL.Workbooks.Add(Missing.Value));
    

    is creating a new workbook. use

    string workbookPath = "c:/SomeWorkBook.xls";
    
    oWB = Workbooks.Open(workbookPath,
            0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
            true, false, 0, true, false, false);
    

    now it depends on what you want to do add a new sheet, use an existing sheet etc.

    this is a codeproject link that shows more in depth here

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

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer All WPF controls have thread affinity. The Dispatcher manages the… May 14, 2026 at 8:37 pm
  • Editorial Team
    Editorial Team added an answer So this is the solution I like right now: class… May 14, 2026 at 8:37 pm
  • Editorial Team
    Editorial Team added an answer Yes, OS X has sandboxing support on a per-process level.… May 14, 2026 at 8:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.