Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Interop - Add a new worksheet after all of the others

Tags:

c#

excel

interop

I am trying to add a new worksheet to an Excel workbook and make this the last worksheet in the book in C# Excel Interop.

It seems really simple, and I thought the below code would do it:

using System.Runtime.InteropServices; using Excel = Microsoft.Office.Interop.Excel;  namespace ConsoleApplication2 {     class Program     {         static void Main(string[] args)         {             var excel = new Excel.Application();              var workbook = excel.Workbooks.Open(@"C:\test\Test.xlsx");             workbook.Sheets.Add(After: workbook.Sheets.Count);              workbook.Save();             workbook.Close();              Marshal.ReleaseComObject(excel);         }     } } 

No such luck. I get this helpful error:

COMException was unhandled - Exception from HRESULT: 0x800A03EC

I found this page on Microsoft.com which suggested I try and add the sheet first and then move it so I tried that as shown below. I know that this webpage targets Excel 95 but the VBA is still there to use so I was hoping it would still work:

using System.Runtime.InteropServices; using Excel = Microsoft.Office.Interop.Excel;  namespace ConsoleApplication2 {     class Program     {         static void Main(string[] args)         {             var excel = new Excel.Application();              var workbook = excel.Workbooks.Open(@"C:\test\Test.xlsx");             workbook.Sheets.Add();             workbook.Sheets.Move(After: workbook.Sheets.Count);              workbook.Save();             workbook.Close();              Marshal.ReleaseComObject(excel);         }     } } 

I get the same error as above. I have also tried passing the name of my last worksheet as a string as the After parameter in both the Add and Move methods, no joy!

That is what I have tried, so my question is how do I add a worksheet to an Excel workbook and make this the last sheet in the workbook using C# Excel Interop?

Thanks

like image 804
JMK Avatar asked Aug 28 '12 09:08

JMK


People also ask

How do I auto pull data from another workbook in Excel?

We can do that by using the same two methods we've covered. Copy the cell value of 200 from cell A1 on Sheet1. Go to Sheet2, click in cell A1 and click on the drop-down arrow of Paste button on the Home tab and select Paste Link button. It will generate a link by automatically entering the formula =Sheet1!

How do I link multiple data from one Excel sheet to another?

On the Data tab, under Tools, click Consolidate. In the Function box, click the function that you want Excel to use to consolidate the data. In each source sheet, select your data, and then click Add. The file path is entered in All references.

How many sheets are there by default when we create a new Excelfile?

By default, a new workbook contains three worksheets, but you can change the number of worksheets that you want a new workbook to contain. For more information, see Change the number of worksheets in a new workbook. You can also add and remove worksheets as needed.


2 Answers

Looking at the documentation here http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.move(v=vs.80).aspx, it indicates that the 'after' object isn't a numerical position; it's the object representing the sheet you want to position your sheet after. The code should probably be something like (untested):

workbook.Sheets.Add(After: workbook.Sheets[workbook.Sheets.Count]);  
like image 173
Dave Avatar answered Oct 12 '22 14:10

Dave


This should do the job:

wSheet.Move(Missing.Value, workbook.Sheets[workbook.Sheets.Count]); 
like image 39
Radu D Avatar answered Oct 12 '22 12:10

Radu D