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.
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 currently an novice user - working with excel libraries. Any further assistance would be most appreciated.
Thanks
friend please use below code,use your column name instance of this query "select columnname from sheetName"
I call it this way:
for more information find this site. http://www.codedisqus.com/CyVjkWkUeg/copying-data-from-one-excel-file-to-another-using-c-adonet-ms-jet-returns-error-about-key-column.html