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
Thamsanqa Ngcongwane
NA
184
24.9k
How to export list data to excel in C#
Nov 20 2019 3:59 AM
The following is my c# code Im only stuck on the foreach part where by I have to insert data from list to excel.
public
IActionResult ExportData() {
List < Users > ApplicationList = _context.Users.ToList();
string
sWebRootFolder = _hostingEnvironment.WebRootPath;
string
sFileName = @
"Candidates.xlsx"
;
string
URL =
string
.Format(
"{0}://{1}/{2}"
, Request.Scheme, Request.Host, sFileName);
FileInfo file =
new
FileInfo(Path.Combine(sWebRootFolder, sFileName));
var memory =
new
MemoryStream();
using
(var fs =
new
FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Create, FileAccess.Write)) {
IWorkbook workbook;
workbook =
new
NPOI.XSSF.UserModel.XSSFWorkbook();
ISheet excelSheet = workbook.CreateSheet(
"Candidates"
);
IRow row = excelSheet.CreateRow(0);
foreach
(var data
in
ApplicationList) {}
workbook.Write(fs);
}
using
(var stream =
new
FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Open)) {
stream.CopyTo(memory);
}
memory.Position = 0;
return
File(memory,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
, sFileName);
}
Reply
Answers (
3
)
What is the difference between SSRS and PowerBI?
how to Add two json object using sql server