在C#中使用Excel的PasteSpecial时出错. [英] Error while using PasteSpecial for Excel in C#.

查看:380
本文介绍了在C#中使用Excel的PasteSpecial时出错.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码集粘贴复制的值,但始终会收到错误消息..


xlWorkSheet1 =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


Excel.Range R1 =(Excel.Range)xlWorkSheet1.Cells [1,1];


R1.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues,Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,false,false);


错误:

说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

异常详细信息:System.Runtime.InteropServices.COMException:Range类的PasteSpecial方法失败

我尝试添加R1.Select(),但仍然失败..

非常感谢任何帮助.

Using following set of code to paste the values copied, But keep getting error messages..


xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


Excel.Range R1 = (Excel.Range)xlWorkSheet1.Cells[1, 1];


R1.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues,Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,false,false);


Error :

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: PasteSpecial method of Range class failed

I tried to add R1.Select () , but it still failed..

Any help much appreciated

推荐答案

在粘贴之前已将某些内容复制到剪贴板上了吗.您需要进行剪切或复制.以下代码演示了这一点. br/>
Have you copied something on the clipboard before pasting.. You need to do a cut or copy.. The following code demonstrates that..

Excel.Range R1 = (Excel.Range)oSheet.Cells[11, 11];
R1.Copy(Type.Missing);

Excel.Range R2 = (Excel.Range)oSheet.Cells[15, 15];
R2.PasteSpecial(Excel.XlPasteType.xlPasteValues,    Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);



发表评论后的新代码



The new code after your comments

Excel.Range FilterRange = xlWorkSheet.get_Range("A1", "X6100");
       FilterRange.AutoFilter(6, "Project Leader", Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);
       Excel.Range splRange1 = FilterRange.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeVisible, Type.Missing);
       splRange1.Copy(Type.Missing);
       xlWorkSheet = (Excel.Worksheet) oWB.Worksheets.get_Item(2);
       Excel.Range R1 = (Excel.Range)xlWorkSheet.Cells[1, 1];
       R1.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);


这篇关于在C#中使用Excel的PasteSpecial时出错.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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