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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:14:02+00:00 2026-06-08T22:14:02+00:00

I am writing a program to fill Excel work books with data, but at

  • 0

I am writing a program to fill Excel work books with data, but at the moment i can only access the first sheet.

With this test i cant get sheet2 and Sheet3 to receive any data?

Please could someone point out what i’m missing?

 Excel.Application xlApp;
 Excel.Workbook xlWorkBook;
 Excel.Worksheet[] xlWorkSheet = new Excel.Worksheet[3];
 object misValue = System.Reflection.Missing.Value;

private void Form1_Load(object sender, EventArgs e)
        {

     if (File.Exists("C:/Users/Shaun/Documents/Template.xls"))
                {
                    //Load Templete SpreadSheet
                    xlApp = new Excel.ApplicationClass();
                    xlWorkBook = xlApp.Workbooks.Open("Template.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                    xlWorkSheet[0] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkSheet[1] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkSheet[2] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                }
                else
                {
                    MessageBox.Show("Could not find file Template.xls");
                }
            }



 private void button1_Click(object sender, EventArgs e)
        {
          xlWorkSheet[0].Cells[1, 1] = "Sheet1";
          xlWorkSheet[1].Cells[1, 1] = "Sheet2";
          xlWorkSheet[2].Cells[1, 1] = "Sheet3";  

        String Saveas;
        Saveas = Microsoft.VisualBasic.Interaction.InputBox("Save As", "Save As", "");
        xlWorkBook.SaveAs(Saveas + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();
        for (int p = 0; p < 3; p++)
        {
            releaseObject(xlWorkSheet[p]);
        }
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
        MessageBox.Show("File Saved!");    
        }

 private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

Edit

Even with this change there is still nothing happening, i can’t pick up any sheets but the first one.

 xlWorkSheet[0] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
 xlWorkSheet[1] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
 xlWorkSheet[2] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
  • 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-08T22:14:04+00:00Added an answer on June 8, 2026 at 10:14 pm

    Maybe the error is here:

    xlWorkSheet[0] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    xlWorkSheet[1] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    xlWorkSheet[2] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    

    should be get_Item(2), get_Item(3)?

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

Sidebar

Related Questions

This is my first attempt at writing a program that accesses a database from
I'm writing a program which will use scan conversion on triangles to fill in
I've been writing a program in C to move the first 4 bits of
I am writing a program where I where the first time a user runs
Iam writing a program to gather data on ttl of various DNS records.I could
I have tried writing program that plays a sound file but have been unsuccessful
I am writing the program in Java, and i want to fill out the
I am writing a program in C++ that is sending 1500 bytes of data
Im writing a program that should read input via stdin, so I have the
I'm writing a program that handles DBs and writes any changes into ListView for

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.