1
Answer

ASP.NET MVC - Import Item with Parent Category from Excel

mike idowu

mike idowu

7y
211
1
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.
  1. ParentID1 is the Level
  2. ParentID of the Root Category(Level 1) will be set to 0
  3. For the ones that are not Root
  4. 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");
}
}
Answers (1)
0
Amitesh Verma

Amitesh Verma

NA 875 68.4k 9y
private void button11_Click(object sender, EventArgs e)
{
try {
Bitmap b = new Bitmap("name of the file");
b.Save("path of the folder to save");
Bitmap b = new Bitmap(@"C:\Documents and Settings\Desktop\7506.jpg");
b.Save(@"C:\Extract\test.jpg");
}
catch( Exception ex )
{
MessageBox .Show (ex.Message );
}
}
0
Ajay Saini

Ajay Saini

NA 192 7.5k 9y
Yes i have read above. It only point to images.
0
Amitesh Verma

Amitesh Verma

NA 875 68.4k 9y
have you read the link i have provided to you?It contains your solution.
0
Ajay Saini

Ajay Saini

NA 192 7.5k 9y
Hi thanks for your comment.
Actually i just want like in ASP.NET browse feature in my windows application. User just browse the location and once he clicks on OK file should be SaveAs in my solution's directory. 
0
Amitesh Verma

Amitesh Verma

NA 875 68.4k 9y
http://www.c-sharpcorner.com/Blogs/7226/create-directory-and-copysave-image-file-on-filename-from-d.aspx
 
 
 
0
Ajay Saini

Ajay Saini

NA 192 7.5k 9y
Hi Thanks for your quick reply.
I have seen second link but it have solution to add file in binary form into database which i don't want. I just want to Save a copy of the selected file for future reference into one of the folder named as "Docs" without choose location from user. This folder is available into my solution.
0
Amitesh Verma

Amitesh Verma

NA 875 68.4k 9y
http://www.dotnetperls.com/savefiledialog
 
 
 http://www.codeproject.com/Questions/47601/save-view-remove-file-using-winforms-and-C