我上传Excel文件并使用ASP.NET MVC保存到我的数据库中。我成功了:
导入Excel文件
保存到数据库
请参阅下面的控制器
控制器
public ActionResult ImportCountriesExcel(HttpPostedFileBase FileUpload)
{
string data = "";
var notif = new UINotificationViewModel()
{
notif_message = "Record Saved successfully",
notif_type = NotificationType.SUCCESS,
};
var bodsList = new List<COUNTRIES>();
if (FileUpload != null)
{
// tdata.ExecuteCommand("truncate table OtherCompanyAssets");
HttpPostedFileBase file = Request.Files["FileUpload"];
if (true)//FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
try {
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 COUNTRIES();
bod.COUNTRY_CODE = Convert.ToString(workSheet.Cells[rowIterator, 1].Value);
if (bod.COUNTRY_CODE == "")
message += "<li> Country Code is empty at row " + rowIterator + "</li>";
bod.COUNTRY_NAME = Convert.ToString(workSheet.Cells[rowIterator, 2].Value);
if (bod.COUNTRY_NAME == "")
message += "<li> Country Name is empty at row " + rowIterator + "</li>";
if (bod.COUNTRY_DESC == "")
message += "<li> Country Desc is empty at row " + rowIterator + "</li>";
bodsList.Add(bod);
}
}
foreach (var item in bodsList)
{
_countriesService.AddCountry(item);
}
return RedirectToAction("Index");
}
catch (Exception e)
{
notif.notif_message = "Application error, Contact the Administrator";
notif.notif_type = NotificationType.ERROR;
}
TempData["notif"] = notif;
return RedirectToAction("Index");
}
else
{
notif.notif_type = NotificationType.ERROR;
//alert message for invalid file format
data += "<ul>";
data += "<li>Only Excel file format is allowed</li>";
data += "</ul>";
notif.notif_message = data;
TempData["notif"] = notif;
return RedirectToAction("Index");
//return Json(data, JsonRequestBehavior.AllowGet);
}
}
else
{
data += "<ul>";
if (FileUpload == null) data += "<li>Please choose Excel file</li>";
data += "</ul>";
notif.notif_message = data;
TempData["notif"] = notif;
return RedirectToAction("Index");
//return Json(data, JsonRequestBehavior.AllowGet);
}
}
我遇到的问题是这个
我该怎么做:
答案 0 :(得分:0)
为防止重复,您可以从数据库中读取所有现有项目并将其存储在字典中,并在迭代excel行中的每个项目时,检查字典是否存在,如果不存在则添加到数据库中并添加到字典中。
var countryDict=db.Countries.ToDictionary(a => a.Code, a => a.Name);
var duplicateItems = new List<string>();
//your existing code goes here
for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
{
var countryCode = "Read this from the excel cell";
var countryName = "Read this from the excel cell";
if (countryDict.ContainsKey(countryCode))
{
duplicateItems.Add(countryCode);
}
else
{
countryDict.Add(countryCode,countryName);
//existing code to save
}
}
if(duplicateItems.Any()
{
//This means duplicates exist
}
修剪只是一种方法,你可以调用字符串来删除前导和尾随空格。确保你没有在NULL上调用它。
var trimmedValue= someStringVariableWhichCouldBeNull?.Trim();