且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将列从一个excel文件复制到另一个?

更新时间:2022-10-23 07:43:50

可能有助于解决这个问题的一些事情。

  //   ~~>设置目的地范围 
xlDestRange = xlWsheet2.Range [ A2] ;
xlDestRange1 = xlWsheet2.Range [ B2];

将导致错误

Quote:

无法粘贴信息,因为复制区域和粘贴区域的大小和形状不同。

如果您要尝试粘贴 EntireColumn

,则需要选择第1行

 xlDestRange = xlWsheet2.Range [  A1]; 
xlDestRange1 = xlWsheet2.Range [ B1];



接下来需要注意的是 xlPasteSpecialOperationAdd - 如果源范围中存在非数字值,则粘贴将失败。 />


最后 - 我不认为这是一个问题,但你的文件路径很难遵循 - 我建议简化路径或使用显式路径名

I using excel addin application, to create function which can copy columns from one excel file to another. Here is the code so far but when I render the application, however the program outputs a blank book.xls file.

Updated:

private void button1_Click(object sender, EventArgs e)
       {
           Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
           Excel.Workbook xlWorkBook;
           Excel.Workbook xlWorkBook2;

           Excel.Worksheet xlWorkSheet;
           Excel.Worksheet xlWsheet2;

           Excel.Range xlSourceRange;
           Excel.Range xlSourceRange1;

           Excel.Range xlDestRange;
           Excel.Range xlDestRange1;

           xlWorkBook = xlApp.Workbooks.Open("C:/../../../../../../../Test.xls");


           xlWorkBook2 = xlApp.Workbooks.Open("C:/../../../../../../../Book1.xls");

           //~~> Display Excel
           xlApp.Visible = true;

           //~~> Set the source worksheet
           xlWorkSheet = xlWorkBook.Sheets["Sheet1"];
           //~~> Set the destination worksheet
           xlWsheet2 = xlWorkBook2.Sheets["Sheet1"];

           //~~> Set the source range
           xlSourceRange = xlWorkSheet.Range["E15"].EntireColumn;
           xlSourceRange1 = xlWorkSheet.Range["D15"].EntireColumn;

           //~~> Set the destination range
           xlDestRange = xlWsheet2.Range["A2"];
           xlDestRange1 = xlWsheet2.Range["B2"];


           xlSourceRange.Copy(Type.Missing);

           xlDestRange.PasteSpecial(Excel.XlPasteType.xlPasteAll,
   Excel.XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false);


           xlSourceRange1.Copy(Type.Missing);

           xlDestRange1.PasteSpecial(Excel.XlPasteType.xlPasteAll,
   Excel.XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false);

       }




I am little unsure how I to go about tracing for errors, as I am quite novice in this area. Any further assistance would be most appreciated.

Thank you

Couple of things that might help sort this out.
The lines
//~~> Set the destination range
xlDestRange = xlWsheet2.Range["A2"];
xlDestRange1 = xlWsheet2.Range["B2"];

will result in an error

Quote:

The information cannot be pasted because the Copy area and the paste area are not the same size and shape.

You need to select Row 1 if you are trying to paste an EntireColumn
I.e.

xlDestRange = xlWsheet2.Range["A1"];
xlDestRange1 = xlWsheet2.Range["B1"];


The next thing to be wary of is xlPasteSpecialOperationAdd - if there are non-numeric values in your source range then the paste will fail.

Finally - I don't think it's a problem at the moment, but the path to your files is very difficult to follow - I'd advise simplifying the path OR using explicit pathnames