4

I have existing Excel file with some data and I want to append data to it... ![enter image description here][1]

try
{
    xlApp = new Excel.Application();
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
    xlWorkBook.Save();
    Object newpath = path + "Chat_Competitors.xls";
    xlWorkBook.SaveAs(newpath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();
}
catch (Exception ex)
{
    MessageBox.Show("Error" + ex.ToString());
}

this is what I done. but I want to append..

0

2 Answers 2

4

To append to an existing excel file, find the last row and increment it by 1

int _lastRow = xlWorkSheet.Cells.Find(
                              "*",
                              xlWorkSheet.Cells[1,1],
                              Excel.XlFindLookIn.xlFormulas,
                              Excel.XlLookAt.xlPart,
                              Excel.XlSearchOrder.xlByRows,
                              Excel.XlSearchDirection.xlPrevious,
                              misValue,
                              misValue,
                              misValue
                              ).Row + 1;

and then simply write to that row

Sign up to request clarification or add additional context in comments.

2 Comments

looks like there is no built in function there. anyway this will help. thanks.
this is the best option I got.. :)
1

You must open the existing file instead of create a new.

var xlApp = new Excel.Application();
xlApp.Workbooks.Open(path + "Chat_Competitors.xls");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.