粘贴范围类的特殊方法失败 [英] Paste special method of range class failed
本文介绍了粘贴范围类的特殊方法失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我收到Range类的PasteSpecial方法失败错误
请在下面提供帮助代码
Hi All,
I am getting a PasteSpecial method of Range class failed" error
Please help code is below
Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(path, 0, true, 1, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
Range last = workSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell,Type.Missing);
//Range wrange = workSheet.get_Range("A1",last);
Range r1 = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[1, 1]).EntireColumn;
r1.Insert(XlInsertShiftDirection.xlShiftToRight,XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
Range r2 = (Microsoft.Office.Interop.Excel.Range)workSheet.Cells[1, 1];
r2.Select();
r2.FormulaR1C1 = "=LEFT(RC[1],100)";
r2.Select();
Range r3 = (Microsoft.Office.Interop.Excel.Range)workSheet.get_Range("A1","A160237");
r2.AutoFill(r3,XlAutoFillType.xlFillCopy);
r3.Copy(r3);
sheets.Add(Type.Missing, workSheet, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
Microsoft.Office.Interop.Excel.Worksheet workSheetA = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(2);
Range r4 = (Microsoft.Office.Interop.Excel.Range)workSheetA.Cells[1, 1];
r4.Select();
r4.PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone,false,false);
Paste special is working fine in my VBA code
VBA code is as follow
Selection.Copy
Sheets("Sheet3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
推荐答案
该线程在同一问题上:
http://social.msdn.microsoft.com /forums/zh-CN/vbgeneral/thread/82e773a2-e0d4-43d1-b4ed-0112fc7c20b2/ [ http://support.microsoft.com/kb/231090 [
This thread is on the same issue :
http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/82e773a2-e0d4-43d1-b4ed-0112fc7c20b2/[^]
The marked answer uses VB code but you should be able to get that converted to C#.
The core idea there is to use a fixed column width of 8. For more info on why that''s needed, see:
http://support.microsoft.com/kb/231090[^]
这篇关于粘贴范围类的特殊方法失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文