C# Excel Interop - 在所有其他工作表之后添加一个新工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12156302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:18:00  来源:igfitidea点击:

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

c#excelinterop

提问by JMK

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.

我正在尝试将新工作表添加到 Excel 工作簿,并使其成为 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

COMException 未处理 - 来自 HRESULT 的异常:0x800A03EC

I found this pageon 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:

我在 Microsoft.com 上找到了这个页面,它建议我先尝试添加工作表,然后再移动它,所以我尝试了如下所示的操作。我知道这个网页针对 Excel 95,但 VBA 仍然可以使用,所以我希望它仍然可以工作:

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 Afterparameter in both the Addand Movemethods, no joy!

我得到与上面相同的错误。我还尝试将我最后一个工作表的名称作为字符串传递AfterAddMove方法中的参数,没有乐趣!

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?

这就是我尝试过的,所以我的问题是如何将工作表添加到 Excel 工作簿并使用 C# Excel Interop 使其成为工作簿中的最后一个工作表?

Thanks

谢谢

采纳答案by Dave

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):

查看此处的文档http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.move(v=vs.80).aspx,它表明“之后”对象不是数字位置;它是代表要在其后放置工作表的工作表的对象。代码应该类似于(未经测试):

workbook.Sheets.Add(After: workbook.Sheets[workbook.Sheets.Count]); 

回答by Radu D

This should do the job:

这应该可以完成这项工作:

wSheet.Move(Missing.Value, workbook.Sheets[workbook.Sheets.Count]);

回答by shiroxx

This is the only way that works for me:

这是对我有用的唯一方法:

xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.Add
    (System.Reflection.Missing.Value,
     xlWorkBook.Worksheets[xlWorkBook.Worksheets.Count], 
     System.Reflection.Missing.Value, 
     System.Reflection.Missing.Value);