TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
2.1k
470.7k
How to export file to excel in linq sql using asp.net mvc
Apr 26 2021 12:38 PM
Hi Team
I have excel spreedsheet, but the problem is formatting of data is wrong and need some help on my sql query when using linq on my MVC application. The reqirement is that first column(year must start with 2021), second column(week must be 9 etc if there is 10 must continue per year week etc), Day column must be 03/02/2021 if there was a schedule on this day. VW250 this model column be 79 if there was a model produce, VW250/2PA this model column must be 574. Below snipping image is my current output and formatting of it is not what is expecting and need help. Second is what is required for this exercise
// Model class
public
class
ExtractionViewModel
{
public
string
Year {
get
;
set
; }
public
int
Week {
get
;
set
; }
public
DateTime Day {
get
;
set
; }
public
string
VW250 {
get
;
set
; }
public
string
VW270 {
get
;
set
; }
public
string
VW2502PA {
get
;
set
; }
public
string
VW270PA {
get
;
set
; }
}
// Controller
public
void
ExportToExcel()
{
var v =
new
GridView();
v.DataSource =
this
.GetExtractionViewModels();
v.DataBind();
Response.ClearContent();
Response.Buffer =
true
;
Response.AddHeader(
"content-disposition"
,
"attachment; filename=ExtractionRecords.xls"
);
Response.ContentType =
"application/ms-excel"
;
Response.Charset =
""
;
StringWriter objStringWriter =
new
StringWriter();
HtmlTextWriter htmlTextWriter =
new
HtmlTextWriter(objStringWriter);
v.RenderControl(htmlTextWriter);
Response.Output.Write(objStringWriter.ToString());
Response.Flush();
Response.End();
//return View("DataResult");
}
public
ActionResult DataResult()
{
return
View(
this
.GetExtractionViewModels());
}
public
IList
GetExtractionViewModels()
{
var db =
new
ProductionManagementEntities();
var scheduleList = (from p
in
db.ProductionDays
from m
in
db.Models
join w
in
db.Weeks on p.WeekId equals w.WeekId
orderby w.Year ascending
orderby m.Name ascending
where(m.InActive ==
true
)
select
new
ExtractionViewModel
{
Year = w.Year,
Week = w.WeekNum,
Day = p.ProductionDate,
VW250 = m.Name,
VW270 = m.Name,
VW2502PA = m.Name,
VW270PA = m.Name
}).ToList();
return
scheduleList;
}
Reply
Answers (
3
)
User registered data only display.
How to extract data correctly using linq sql in asp.net mvc 5?