vendredi 13 mars 2015

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


Vote count:

0




I have two excel files. 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. Now I want to copy some selection(cells content) in sheet1 from workbook1 to another cell in sheet1 in workbook2. 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. Does anyone have any idea? Thank you for your time!



asked 2 mins ago







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

Aucun commentaire:

Enregistrer un commentaire