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
mike idowu
1.3k
448
63.9k
ASP.NET MVC - Import Item with Parent Category from Excel
Nov 30 2017 6:15 PM
I have this Excel sheet as shown below
The table is shown below
I want to import from the Excel and save into the database using ASP.NET MVC.
From the Excel, when it is Level 1(Root Category), then ParentID will be 0.
ParentID1 is the Level
ParentID of the Root Category(Level 1) will be set to 0
For the ones that are not Root
The Category can be to any Level
Please how do I import this data from the Excel and save into the database.
I started a code but don't know how to complete it, because of the complexity. See my Controller below.
public ActionResult ImportCountriesExcel(HttpPostedFileBase FileUpload)
{
string data = "";
var bodsList = new List<CategoryLevel>();
if (FileUpload != null)
{
HttpPostedFileBase file = Request.Files["FileUpload"];
if (true)
{
string message = "";
using (var package = new ExcelPackage(FileUpload.InputStream))
{
var currentSheet = package.Workbook.Worksheets;
// if(currentSheet.Count)
var workSheet = currentSheet.First();
var noOfCol = workSheet.Dimension.End.Column;
var noOfRow = workSheet.Dimension.End.Row;
for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
{
var bod = new CategoryLevel();
bod.CategoryName = Convert.ToString(workSheet.Cells[rowIterator, 1].Value).Trim();
bod.ParentID = Convert.ToSInt(workSheet.Cells[rowIterator, 2].Value);
bod.ParentID1 = Convert.ToSInt(workSheet.Cells[rowIterator, 3].Value);
bod.ParentID2 = Convert.ToSInt(workSheet.Cells[rowIterator, 4].Value);
bodsList.Add(bod);
}
}
foreach (var item in bodsList)
{
_categorylevelService.AddCategoryLevel(item);
}
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Index");
}
}
else
{
return RedirectToAction("Index");
}
}
Reply
Answers (
1
)
LowdiskSpace alert in console application
How to get specific process idle time using C# ?