简体   繁体   English

使用c#将一个或多个单元格的内容从workBook1中的一个工作表复制到workbook2中的另一个现有工作表

[英]Copy contents of cell/cells from one sheet in workBook1 to the other existing sheet in workbook2 using c#

I have two excel files. 我有两个Excel文件。 One has information that is messed up as it is scrapped from web and stored as excel file because the source HTML contained lots of tables. 由于从源HTML包含大量表,因此其中的信息会从Web上被废弃并存储为excel文件,从而使信息混乱。 Now I want to copy some selection(cells content) in sheet1 from workbook1 to another cell in sheet1 in workbook2. 现在,我想将工作表1中工作表1中的某些选择(单元格内容)复制到工作簿2中工作表1中的另一个单元格。 I wrote something, but this copies cells from the same sheet to itself. 我写了一些东西,但这将单元格从同一张纸复制到自己。

        //Check to see if path exists
        if (!File.Exists(path1))
        {
            //if file does not exist, 

            MessageBox.Show("File does not exixt.");
        }
        //if exists, it will open the file to write to it
        else
        {
            workBook1 = exlApp.Workbooks.Open(path1, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
                false, 0, true, false, false);
        }

        workBook2 = exlApp.Workbooks.Open(path2, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
               false, 0, true, false, false);

        //Get the already existing sheets from worksheet1 and worksheet2
        workSheet1 = (Excel.Worksheet)exlApp.Worksheets.get_Item(1);
        workSheet2 = (Excel.Worksheet)exlApp.Worksheets.get_Item(1);

        //Get the cell to copy the contents from 
        Excel.Range sourceRange = workSheet1.get_Range("A1");

        //Get the destination cell(in a different workbook) to copy
        Excel.Range destinationRange = workSheet2.get_Range("B2");

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

I tried several ways, but sometimes it shows COM runtime error or copies contents to itself. 我尝试了几种方法,但有时会显示COM运行时错误或将内容复制到自身。 Does anyone have any idea? 有人有什么主意吗? Thank you for your time! 感谢您的时间!

It seems that you are assigning the same worksheet to two different variables: 似乎您正在将同一工作表分配给两个不同的变量:

//Get the already existing sheets from worksheet1 and worksheet2
workSheet1 = (Excel.Worksheet)exlApp.Worksheets.get_Item(1);
workSheet2 = (Excel.Worksheet)exlApp.Worksheets.get_Item(1);

get_Item(1) in both lines. 两行都包含get_Item(1)。

It was an easy fix. 这很容易解决。 Below is the code that fixed it. 下面是修复它的代码。

 //Check to see if path exists
        if (!File.Exists(path1))
        {
            //if file does not exist, 

            MessageBox.Show("File does not exixt.");
        }
        //if exists, it will open the file to write to it
        else
        {
            workBook1 = exlApp.Workbooks.Open(path1, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
                false, 0, true, false, false);
        }

        workBook2 = exlApp.Workbooks.Open(path2, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
               false, 0, true, false, false);




        //Get the cell to copy the contents from 
        Excel.Range sourceRange = workBook1.Sheets[1].Range("A1");

        //Get the destination cell(in a different workbook) to copy
        Excel.Range destinationRange = workBook2.Sheets[1].Range("B2");

        sourceRange.Copy(destinationRange);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM