Export database data to csv from view by date range asp.net mvc3
Posted
by
Benjamin Randal
on Stack Overflow
See other posts from Stack Overflow
or by Benjamin Randal
Published on 2012-11-12T00:47:03Z
Indexed on
2012/11/12
17:00 UTC
Read the original article
Hit count: 328
I am trying to find a way to export data from my database and save it as a .csv file. Ideally the user will be able to select a date range on a view, which will display the data to be exported, then the user can click an "export to CSV" link. I've done quite a bit of searching and but have not found much specific enough to help me step through the process. Any help would be great.
I would like to export data from this database model...
{
public class InspectionInfo
{
[Key]
public int InspectionId { get; set; }
[DisplayName("Date Submitted")]
[DataType(DataType.Date)]
// [Required]
public DateTime Submitted { get; set; }
[DataType(DataType.MultilineText)]
[MaxLength(1000)]
// [Required]
public string Comments { get; set; }
// [Required]
public Contact Contact { get; set; }
[ForeignKey("Contact")]
public Int32 ContactId { get; set; }
[MaxLength(100)]
public String OtherContact { get; set; }
I have a service for search also, just having difficulty implementing
public SearchResults SearchInspections(SearchRequest request)
{
using (var db = new InspectionEntities())
{
var results = db.InspectionInfos
.Where( i=>
(
(null == request.StartDate || i.Submitted >= request.StartDate.Value) &&
(null == request.EndDate || i.Submitted <= request.EndDate.Value)
)
)
.OrderBy(i=>i.Submitted)
.Skip(request.PageSize*request.PageIndex).Take(request.PageSize);
return new SearchResults{
TotalResults=results.Count(),
PageIndex=request.PageIndex,
Inspections=results.ToList(),
SearchRequest=request
};
}
}
© Stack Overflow or respective owner