C#:将受保护的工作表复制到另一个Excel文件 [英] C#:Copy protected worksheet to another excel file

查看:61
本文介绍了C#:将受保护的工作表复制到另一个Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将粘贴一个受保护的工作表复制到另一个excel文件中,但是却出现类似

I was trying to copy paste one protected worksheet to another excel file but i`m getting error like

无效的索引.(来自HRESULT的异常:0x8002000B(DISP_E_BADINDEX))"

"Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))"

C#代码:

try
{
    string startPath = System.IO.Path.GetDirectoryName(
        System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
    string filePath = System.IO.Path.Combine(startPath, strPath);

    wBook = xCel.Workbooks.Open(filePath);
    wSheet = (Excel.Worksheet)wBook.Worksheets.get_Item(1);
    wSheet.Copy(Type.Missing, Type.Missing);
    wSheet = (Excel.Worksheet)xlApp.Workbooks[0].ActiveSheet;
    //wSheet = (Excel.Worksheet)xlApp.Workbooks[1].Sheets[1];
}
finally
{
    if (wBook != null)
    {
        wBook.Close();
    }
    if (xlApp != null)
    {
        xlApp.Quit();
    }                
}

有人可以告诉我我在这里做什么错吗?

Could somebody tel what wrong i`m doing here???

OR

请告诉我是否有更好的方法?

Please tell me if there are any better ways to do this??

谢谢.

推荐答案

我会检查:

wSheet = (Excel.Worksheet)wBook.Worksheets.get_Item(1);

wSheet = (Excel.Worksheet)xlApp.Workbooks[0].ActiveSheet;

您的错误似乎表明wBook或xlApp没有任何值.这就是为什么索引无效的原因(我认为工作簿或工作表为空.

Your error seems to suggest either the wBook or the xlApp has no values in it. This is why the index would be invalid (well rather the workbooks or worksheets are empty. I think)

如果它是受保护的工作表,那么受保护的事实会阻止您复制吗?

If it is a protected worksheet, wouldn't the fact its protected stop you copying it?

这篇关于C#:将受保护的工作表复制到另一个Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆