复制单元格在Excel中使用C# [英] Copy cells in excel using C#

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

问题描述

如何复制到特定的行中靶纸?



我需要A1从一个表复制到J10擅长从A15在第二Excel工作表起始位置。我如何在C#中实现这一目标?在下面的Copy方法似乎没有选项的目标指定位置Excel工作表。

  ObjWorkSheet =(Microsoft.Office .Interop.Excel.Worksheet)ObjWorkBookTemp.Sheets [1]; 
ObjWorkSheet.Copy(Type.Missing,ObjWorkBookGeneral.Sheets [1]);


解决方案

我觉得你在这里使用了错误的方法..你想使用粘贴方法,而不是复制方法。



尝试Range.PasteSpecial方法......应该为你做的伎俩。



这样的事情...

  Excel.Range sourceRange = firstWorksheet.get_Range(A1 ,J10); 
Excel.Range destinationRange = secondWorksheet.get_Range(A15,J25);

sourceRange.Copy(Type.Missing);
destinationRange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormulas,Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,假的,假的);


How to copy to specific row in target sheet?

I need to copy A1 to J10 from a sheet in one excel to location starting from A15 in second excel sheet. How can I achieve this in c#? In the below Copy method there seems to be no option to specify the location in target excel sheet.

ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ObjWorkBookTemp.Sheets[1];
ObjWorkSheet.Copy(Type.Missing, ObjWorkBookGeneral.Sheets[1]);

解决方案

I think you are using the wrong method here... you want to use a Paste method not a copy method.

Try the Range.PasteSpecial method... should do the trick for you.

Something like this...

Excel.Range sourceRange = firstWorksheet.get_Range("A1", "J10");
Excel.Range destinationRange = secondWorksheet.get_Range("A15", "J25");

sourceRange.Copy(Type.Missing);
destinationRange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormulas, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);

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

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