I am exporting database information into an excel file in a VB.NET MVC project.
I have the excel part done and I am able to export a hard code table into excel. But I need to fill that table with fields from my database. I have a stored procedure that will get me all the information I need but I'm having trouble getting it to run and be able to let me go through and place the fields I need in the order I need them in.
This is how I'm making my excel sheet if that helps.
Dim blah = New DataTable("testing")
blah.Columns.Add("Col1", Type.GetType("System.String"))
blah.Columns.Add("Col2", Type.GetType("System.String"))
'Below will be switched out for table data, this is where the try for the SQL will end up
blah.Rows.Add(1, "hello")
blah.Rows.Add(2, "hello")
blah.Rows.Add(3, "hello")
blah.Rows.Add(4, "hello")
Dim grid = New GridView()
grid.DataSource = blah
grid.DataBind()
Response.ClearContent()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment; filename=Test-Report.xls")
Response.ContentType = "application/ms-excel"
Response.Charset = ""
Dim sw = New StringWriter()
Dim htw = New HtmlTextWriter(sw)
grid.RenderControl(htw)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
EDIT---- I am having an issue with figuring out the syntax and values to use to run a stored procedure that has a return value and being able to iterate through it and take the row values. I've done a ton of google searches and most have stored procedures that don't have a return value or queries with one value being returned and I am returning a lot of fields.
The stored procedure I'm running looks like this:
SELECT
i.insurer_name
,cs.customer_name
,cs.customer_address
,cs.claim_number
,cs.work_type
,anu.FirstName
,anu.LastName
,l.location_name
,cs.completed_date
,q.question_text
,a.answer_text
FROM insurer i
inner join completed_survey cs on cs.insurer_id = i.insurer_id
inner join AspNetUsers anu on anu.Id = cs.user_id
inner join location l on l.location_id = cs.location_id
inner join answer a on a.complete_id = cs.completed_id
inner join question q on q.question_id = a.question_id
ORDER BY i.insurer_name, cs.survey_score desc
EDIT 2 ---- I'm not entirely sure how to answer what DB connection I'm using. I have a connection string in the web config and I'm using entity framework so most of my code is like
Private db As New SurveyDBModel
db.table_name.ToList()
db.table_name.Add(new_row)