Click here to Skip to main content
15,876,805 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
I'm fairly new with C# and I am trying to export some data from a DataGridView in C# into an Excel file. The inputs from the datagridview are filled in by the user.

Currently, my program can create an excel file along with the values from the datagridview with the given date as its file name.

My problem is I can't seem to find a way to append the data from the gridview IF the excel file already exists, it overwrites the current excel file instead.

Any help/tips/suggestion is highly appreciated.

Thanks :)


What I have tried:

Updated 25-Jan-17 11:47am
Member 12966494 25-Jan-17 17:28pm    
solved it

Check this out,this may help you as I am doing the same just in VB.
How to Use EPPlus in VB[^]

If you want this code to be converted to C#,let me know.
Share this answer
You already posted this in the C# forum at Appending data to existing excel file using C# - C# Discussion Boards[^]. Please do not post the same question in multiple forums.
Share this answer
int lastRow_ = 3;

I fear you forgot to skip existing data before writing new data.
You need to detect existing data and write new data after existing data. you probably also want to not write headers again.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900