Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to add a COM reference in your project to the <strong>"<code>Microsoft Excel 11.0 Object Library</code>"</strong> - or whatever version is appropriate.</p> <p>This code works for me:</p> <pre><code>private void AddWorksheetToExcelWorkbook(string fullFilename,string worksheetName) { Microsoft.Office.Interop.Excel.Application xlApp = null; Workbook xlWorkbook = null; Sheets xlSheets = null; Worksheet xlNewSheet = null; try { xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) return; // Uncomment the line below if you want to see what's happening in Excel // xlApp.Visible = true; xlWorkbook = xlApp.Workbooks.Open(fullFilename, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); xlSheets = xlWorkbook.Sheets as Sheets; // The first argument below inserts the new worksheet as the first one xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing); xlNewSheet.Name = worksheetName; xlWorkbook.Save(); xlWorkbook.Close(Type.Missing,Type.Missing,Type.Missing); xlApp.Quit(); } finally { Marshal.ReleaseComObject(xlNewSheet); Marshal.ReleaseComObject(xlSheets); Marshal.ReleaseComObject(xlWorkbook); Marshal.ReleaseComObject(xlApp); xlApp = null; } } </code></pre> <blockquote> <p>Note that you want to be very careful about <a href="https://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c">properly cleaning up and releasing your COM object references</a>. Included in that StackOverflow question is a useful rule of thumb: <em>"Never use 2 dots with COM objects"</em>. In your code; you're going to have real trouble with that. <em>My demo code above does NOT properly clean up the Excel app, but it's a start!</em></p> </blockquote> <p>Some other links that I found useful when looking into this question:</p> <ul> <li><a href="http://www.codeproject.com/KB/office/csharp_excel.aspx" rel="nofollow noreferrer">Opening and Navigating Excel with C#</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/ms173186(VS.80).aspx" rel="nofollow noreferrer">How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide)</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/6fczc37s(VS.80).aspx" rel="nofollow noreferrer">How to: Add New Worksheets to Workbooks</a></li> </ul> <p>According to MSDN </p> <blockquote> <p>To use COM interop, you must have administrator or Power User security permissions.</p> </blockquote> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload