

Granted, you'll likely want to use index variables that you can increment as you loop through. Then create a new excel document like this: var excelDoc = new Excel.Application() Įxcel._Worksheet wksht = (Excel.Worksheet)excelDoc.ActiveSheet įinally, you'll need to loop through the data you fetched from the DB and insert it somewhere: wksht.Cells = "Some data" Start by adding a reference for, then adding a using statement to your code: using Excel = Otherwise take a look at the MS docs regarding using office interop objects.

If you are creating an excel doc via a web app or something similar you might check out EPPlus which is available as a NuGet package and seems to do what you need.

Wb.Worksheets.Add(dt, "inventory_table") Using (SqlDataAdapter sqlda = new SqlDataAdapter()) Using (sqlcmd = new SqlCommand("SELECT * FROM inventory_table WHERE category= 0")) UPDATED: private void excelToolStripMenuItem_Click(object sender, EventArgs e) Using (SqlDataReader sqldr = sqlcmd.ExecuteReader()) Sqlcmd = new SqlCommand("SELECT * FROM inventory_table WHERE category= 0", sqlconnection) string dbQuery = "SELECT * FROM inventory_table WHERE category= 0" Worksheet ws1 = new Worksheet("Electrical")
Export data from labchart reader to excel code#
Here is my code so far: private void excelToolStripMenuItem_Click(object sender, EventArgs e) The 'category' indicates which dgv the data is part of.īut anyways, back to the issue.how do I read data from my database to a new excel file? There are multiple columns in my database table, but only two will be exported (item and quantity). I want to include an option in my program to export some data from the database to an excel file.
