✅ How to export data from datagridview into excel file
I've tried like a few ways and cant really make sense of it, if someone can guide me through it it would be nice, thanks in advance 🙂
17 Replies
I guess note: I use Visual Studio 2022
If you're data is a
DataTable
or DataSet
it only takes a line of code with one of these excel frameworks:
https://github.com/ClosedXML/ClosedXML
https://www.epplussoftware.com/its datagridview
A
DataGridView
is a control, it holds the data within DataSource
what is epplus and whats the line of code youre talking bout
As I wrote, an excel framework like ClosedXML
It depends on your data,
DataSource
holds object
, which can be anythingi mean the columns from the table im pulling are string and int
but idk what youre trying to explain to me rn
How do you fill your
DataGridView
?with a datareader
i guess more specificaly this is the code
So, it's a
DataTable
...
A DataTable
is an object of representation of a Table, so with both frameworks it's easy to export it to an Excel Table.
For instance, with ClosedXML:
ok thank you it finally works
i guess i need to put a separate thing to be able to change the name of the file everytime cause it kinda doesnt work if you want to put one with a duplicate name
A duplicate file? another Worksheet?
An Excel file or Workbook is a container of Worksheets or Tables.
However, I encourage you to read the docs for detailed information on each function of ClosedXML: https://docs.closedxml.io/en/latest/
well the path for mypath requires at teh end a name for the created file, example "c::\users\decoyuser\desktop\name_of_file.xlsx"
Indeed
thats kidna an easy solve tho to check and be able to create names for the files
ill be sure to read the doc
thanks for the help
:peepohappy:
I guess what you describe is a rolling filename, yes you need another function for that; since it has nothing to do with Excel or
DataTable
etc.yeye i might read on that
thank you tho
:NPCNurseThumbsUp: