Hey everyone,

Does anyone know how to specify which sheet you're going to be modifying in an exel worksheet? For example, if I have an Excel file that has 5 sheets, and I want to modify (4,7) on sheet 3, how would I do that? Here's what I have so far:

Excel.Application excelApp = new Excel.Application();
string myPath = "C:\\Users\\ctote.REG\\Documents\\My Dropbox\\test.xlsx";
excelApp.Workbooks.Open(myPath);

excelApp.Cells[3,3] = "TESTING INFO FROM MY AWESOME APP LOL";
excelApp.Visible = true;

This works, but it modifies sheet1 only.

Recommended Answers

All 9 Replies

Have you tried using excelApp.Sheets[0] for the first sheet, excelApp.Sheets[1] for the second sheet and so on?

This page at msdn.microsoft.com is a good reference point for finding the various properties that are available within the Excel.Application class.

Hope this helps :) Please mark as solved if it resolves your issue.

Thanks for the suggestion. Unfortunately, excelApp.Sheets[] isn't a modifier of sorts. I couldn't find anything else on the page you provided.

Try this.
excelApp.Worksheets[0].Cells[4,7]

commented: Been looking for this answer for a couple of hours. Thanks! +7
cout << "Nick saves the day again." << endl ;

Thank you!!

Another question though - How do I (add a new / change the name of) a sheet?

Use excelApp.Worksheets.Add to add a new sheet
and excelApp.Worksheets[0].name = "NewUniqueName";
or maybe excelApp.Worksheets[0].Name = "NewUniqueName";
to rename a sheet. (not sure about the case)
The name must be unique as the name can also be used in the Worksheets index
E.g. excelApp.Worksheets["NewUniqueName"].Cells[0,0] = "This is a new sheet";

Ok, now I'm getting locked errors. Every file I open gets permanently locked (when I try to open it, it says "Read Only"). Here's my code:

object oMissing = System.Reflection.Missing.Value;
            Excel.Application xl = new Excel.Application();
            Excel.Workbook xlBook;
            Excel.Worksheet xlSheet;
            string laPath = "C:\\Users\\ctote.REG\\Documents\\My Dropbox\\test3.xlsx";

            try
            {
                xlBook = xl.Workbooks.Open(laPath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                xlSheet = xlBook.Worksheets.get_Item(1);
                xlSheet.Name = "108";
                xl.Worksheets.Add();
                xl.Worksheets[0].name = "Testing";
                xlBook.Save();
            }
            catch (Exception)
            {
                MessageBox.Show("locked");
            }
            finally
            {
                xl.Application.Workbooks.Close();
                xl.Workbooks.Close();
            }

I'll start a new thread since my first question was solved.

Sorry, I pasted text in the wrong part of that Sheets.Add.
Add takes four parameters.
Here it is again Sheets.Add

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.