将数据从一个Excel工作簿列复制到另一个 [英] Copy data from one excel workbook column to another

查看:203
本文介绍了将数据从一个Excel工作簿列复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我做了以下代码,将一个excel的数据复制到给定范围的其他代码



但我必须将数据复制到另一个工作表的特定位置。喜欢它应该在工作表的第5行之后复制,范围应该是B1到D1



请告诉我该怎么做。



我的尝试:



 Excel.Application excelApplication = < span class =code-keyword> new  Excel.Application(); 

srcPath = C:\\Users \\sn314708 \\\ \\Desktop\\Excel\\1.xlsx;
Excel.Workbook srcworkBook = excelApplication.Workbooks.Open(srcPath);
Excel.Worksheet srcworkSheet =(Microsoft.Office.Interop.Excel.Worksheet)srcworkBook.Sheets.get_Item( 1 );

destPath = C:\\Users \\sn314708 \\\ \\Desktop\\Excel\\2.xlsx;
Excel.Workbook destworkBook = excelApplication.Workbooks.Open(destPath, 0 false );
Excel.Worksheet destworkSheet =(Microsoft.Office.Interop.Excel.Worksheet)destworkBook.Sheets.get_Item( 1 );

Excel.Range 来自 = srcworkSheet.Range [ A1:C100\" ];
Excel.Range to = destworkSheet.Range [ A1:C100];


来自 .Copy(to);

destworkBook.SaveAs( C:\\Users\\sn314708 \\ \\\Desktop\\Excel\\2 + DateTime.Now.ToString( MM_dd_yyyy)+ 。xlsx);

解决方案

这应该像更改to范围一样简单:

 Excel.Range to = destworkSheet.Range [  B6:D105]; 


请阅读我对该问题的评论。来自变量名的是错误的。这是一个保留字。请参阅: C#关键字 [ ^ ]



我建议将变量的名称更改为从范围 - 例如。



A to 范围不需要像 fromrange 那样定义(不必是相同的大小)。将其定义为单元格就足够了:

 Excel.Range torange = destworkSheet.Range [  B1]; 


Hi all

I did the below code which is coping the data from one excel to other for a given range

but i have to copy the data to another worksheet's specific place. like it should copy after 5th row of the sheet and the range should be B1 to D1

Please tell me how to do that.

What I have tried:

Excel.Application excelApplication = new Excel.Application();

            srcPath = "C:\\Users\\sn314708\\Desktop\\Excel\\1.xlsx";
            Excel.Workbook srcworkBook = excelApplication.Workbooks.Open(srcPath);
            Excel.Worksheet srcworkSheet = (Microsoft.Office.Interop.Excel.Worksheet)srcworkBook.Sheets.get_Item(1);

            destPath = "C:\\Users\\sn314708\\Desktop\\Excel\\2.xlsx";
            Excel.Workbook destworkBook = excelApplication.Workbooks.Open(destPath, 0, false);
            Excel.Worksheet destworkSheet = (Microsoft.Office.Interop.Excel.Worksheet)destworkBook.Sheets.get_Item(1);

            Excel.Range from = srcworkSheet.Range["A1:C100"];
            Excel.Range to = destworkSheet.Range["A1:C100"];

           
            from.Copy(to);

            destworkBook.SaveAs("C:\\Users\\sn314708\\Desktop\\Excel\\2 " + DateTime.Now.ToString("MM_dd_yyyy") + ".xlsx");

解决方案

That should be as simple as changing the "to" range:

Excel.Range to = destworkSheet.Range["B6:D105"];


Please, read my comment to the question. A from variable name is wrong. This is a reserved word. Please, see: C# Keywords[^]

I'd suggest to change the name of variable to fromrange - for example.

A "to" range doesn't need to be defined the same way as "fromrange" (doesn't have to be same size). It is sufficient to define it as a sigle cell:

Excel.Range torange = destworkSheet.Range["B1"];


这篇关于将数据从一个Excel工作簿列复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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