使用 c# 将 cell/cells 的内容从工作簿 1 中的一个 sheet 复制到工作簿 2 中另一个现有的 sheet
Copy contents of cell/cells from one sheet in workBook1 to the other existing sheet in workbook2 using c#
我有两个 excel 文件。一个人的信息是混乱的,因为它是从网络上删除并存储为 excel 文件,因为源 HTML 包含很多表格。现在我想将 sheet1 中的一些选择(单元格内容)从工作簿 1 复制到工作簿 2 中 sheet1 中的另一个单元格。我写了一些东西,但这会将单元格从同一个 sheet 复制到它自己。
//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);
我尝试了几种方法,但有时会显示 COM 运行时错误或将内容复制到自身。有人有什么主意吗?感谢您的宝贵时间!
您似乎将同一个工作表分配给两个不同的变量:
//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) 两行。
这是一个简单的修复。下面是修复它的代码。
//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);
我有两个 excel 文件。一个人的信息是混乱的,因为它是从网络上删除并存储为 excel 文件,因为源 HTML 包含很多表格。现在我想将 sheet1 中的一些选择(单元格内容)从工作簿 1 复制到工作簿 2 中 sheet1 中的另一个单元格。我写了一些东西,但这会将单元格从同一个 sheet 复制到它自己。
//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);
我尝试了几种方法,但有时会显示 COM 运行时错误或将内容复制到自身。有人有什么主意吗?感谢您的宝贵时间!
您似乎将同一个工作表分配给两个不同的变量:
//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) 两行。
这是一个简单的修复。下面是修复它的代码。
//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);