C
C#UnawareModesty

✅ 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 🙂
U
UnawareModesty15d ago
I guess note: I use Visual Studio 2022
S
SpReeD15d ago
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/
U
UnawareModesty15d ago
its datagridview
S
SpReeD15d ago
A DataGridView is a control, it holds the data within DataSource
U
UnawareModesty15d ago
what is epplus and whats the line of code youre talking bout
S
SpReeD15d ago
As I wrote, an excel framework like ClosedXML It depends on your data, DataSource holds object, which can be anything
U
UnawareModesty15d ago
i mean the columns from the table im pulling are string and int but idk what youre trying to explain to me rn
S
SpReeD15d ago
How do you fill your DataGridView?
U
UnawareModesty15d ago
with a datareader i guess more specificaly this is the code
string connectionString12 = null;
SqlConnection cnn12;
SqlCommand command12;
SqlDataReader dataReader12;
connectionString12 = GlobalConnection.decoyconnection;
cnn12 = new SqlConnection(connectionString12);
cnn12.Open();
string sql12 = $"select ppi.LocationID, pl.Name as LocationName, ppi.ProductID, pp.Name as ProductName, ppi.Quantity from production.ProductInventory ppi\r\njoin production.Product pp\r\non ppi.ProductID = pp.ProductID\r\njoin production.Location pl\r\non pl.LocationID = ppi.LocationID\r\norder by ppi.LocationID";
command12 = new SqlCommand(sql12, cnn12);
SqlDataAdapter da12 = new SqlDataAdapter(command12);
DataTable dt12 = new DataTable();
da12.Fill(dt12);
dataGridView1.DataSource = dt12;
command12.Dispose();
cnn12.Close();
string connectionString12 = null;
SqlConnection cnn12;
SqlCommand command12;
SqlDataReader dataReader12;
connectionString12 = GlobalConnection.decoyconnection;
cnn12 = new SqlConnection(connectionString12);
cnn12.Open();
string sql12 = $"select ppi.LocationID, pl.Name as LocationName, ppi.ProductID, pp.Name as ProductName, ppi.Quantity from production.ProductInventory ppi\r\njoin production.Product pp\r\non ppi.ProductID = pp.ProductID\r\njoin production.Location pl\r\non pl.LocationID = ppi.LocationID\r\norder by ppi.LocationID";
command12 = new SqlCommand(sql12, cnn12);
SqlDataAdapter da12 = new SqlDataAdapter(command12);
DataTable dt12 = new DataTable();
da12.Fill(dt12);
dataGridView1.DataSource = dt12;
command12.Dispose();
cnn12.Close();
S
SpReeD15d ago
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:
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add((DataTable)dataGridView1.DataSource,"WorksheetName");
wb.SaveAs(myPath);
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add((DataTable)dataGridView1.DataSource,"WorksheetName");
wb.SaveAs(myPath);
U
UnawareModesty15d ago
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
S
SpReeD15d ago
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/
U
UnawareModesty15d ago
well the path for mypath requires at teh end a name for the created file, example "c::\users\decoyuser\desktop\name_of_file.xlsx"
S
SpReeD15d ago
Indeed
U
UnawareModesty15d ago
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:
S
SpReeD15d ago
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.
U
UnawareModesty15d ago
yeye i might read on that thank you tho :NPCNurseThumbsUp:
Want results from more Discord servers?
Add your server
More Posts
Problem with Material Design Lib in wpfI have a Combobox item in Stack panel. Im using Material Desing for automatic styling but the Popup✅ Title: Issues with Authentication and Authorization using JWT with AutoRender Mode in Blazor NET8Hello friends, I am currently facing challenges with Authentication and Authorization using JWT wit✅ How do I reference a specific private instance of a classI would like to know how I could reference a specific instance of a class, if the class is private aReceiving Game Controller Input while Minimized (WinForms C#)Pretty general question here. How would I go about receiving input from my user while they aren't tprocess startIve been trying to figure out why the proccess.start() hasnt been working✅ Configurations say that appsettings is returning nullBasically, i have a configuration and i'm trying to access a value from my appsettings, and all that✅ ASP.NET Core Web Api + SignalRHi, I have an API deployed on IIS but when a person wants to access it they get an error: "ERR_CONNE✅ .NET sdk not workingSo, I've installed the .NET sdk and did what I was told, but it's still not being detected by vscodeMEF No exports were found that match the constraintHi, i'm trying to implement MEF in my WPF app, however i'm getting this error, not sure why anymore.✅ WPF - set UIElement (image) in XAML depending on databound Property Enum valueI am trying to dynamically change a StatusBar image via a view's XAML depending upon a databound VieASP.NET Controller API + Blazor Client UI resources and helpHello! I'm doing a side project where I created the backend API with simple CRUD functionality and A✅ Run PuppeteerSharp in Docker ContainerI have an endpoint that creates PDFs using PuppeteerSharp. I work fine locally because it can automaDocker + Blazor + MSSQL handshake errorWhen I run dotnet ef database update I get the following error : ``Microsoft.Data.SqlClient.SqlExceProblem with corner radius XAML propertyI use a border with these properties: ```<Border CornerRadius="3" Background="{StaticResource Edito✅ :white_check_mark: Dictionary<int, ...> lookup time seems a bit too slowI was profiling a program, and I saw this: ```cs 100 % HasObject • 136 ms • 189 217 Calls • Engine.Rhow to read .txt file next to the Program.csi have the following code that is begin run ```cs string fileContent = File.ReadAllText("hamlet.txt✅ Linker Error Trying to Build OpenSSL Static LinkingHello, I've been trying to compile and build OpenSSL to try and encrypt a text file using AES 256 buHow things get to the placeI have found this one thing by Microsoft that confused me so much ```cs public async Task<IActionResVisual code studio and msbuildim currently having problems while scripting apparently when i press (Run Build Task) im getting thiMediatR - System.Text.Json.JsonException: 'S' is an invalid start of a value.I have a project with Clean Architecture and MediatR. My API gets the request fine and the command