Follow below HttpResponseMessage Method for geting the excel file and upload list of data from excel sheet to database table.
Namespaces:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
DB_ODMSchoolEntities DbContext = new DB_ODMSchoolEntities();
[HttpPost]
public HttpResponseMessage UploadExcel()
{
try
{
var httpRequest = HttpContext.Current.Request;
string guid = Guid.NewGuid().ToString();
if (httpRequest.Files.Count > 0)
{
var postedfile = httpRequest.Files[0];
var allowedExtensions = new[] { ".xlsx", ".xls" };
var checkextension = Path.GetExtension(postedfile.FileName).ToLower();
if (!allowedExtensions.Contains(checkextension)) //Check this file is excel or not
{
return Request.CreateResponse(HttpStatusCode.OK, "Select Excel File");
}
if (postedfile != null && postedfile.ContentLength > 0)// Check posted file is null or not
{
var path =HttpContext.Current.Server.MapPath("~/Excels/" + postedfile.FileName);
//save File
postedfile.SaveAs(path);
string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = excelConnectionString;
OleDbDataAdapter command = new OleDbDataAdapter("select * from [Department_Details$]", connection);
// connection.Open();
// Create DbDataReader to Data Worksheet
DataTable dt = new DataTable();
command.Fill(dt);
string error;
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
string Department_Name = dr["Department_Name"].ToString();
if (DbContext.Tbl_Department.Any(x => x.Department_Name == Department_Name.Trim()))
{
error += dr["Department_Name"].ToString() + " Already Exists.";
}
else
{
Tbl_Department s = new Tbl_Department();
s.Department_Name = dr["Department_Name"].ToString();
s.Inserted_On = DateTime.Now;
s.Is_Active = true;
DbContext.Tbl_Department.Add(s);
DbContext.SaveChanges();
}
}
}
if ((System.IO.File.Exists(path)))
{
System.IO.File.Delete(path);
}
}
return Request.CreateResponse(HttpStatusCode.OK, "Department Details Entered Successfully");
}
else
{
return Request.CreateResponse(HttpStatusCode.OK, "Select Excel File");
}
}
catch
{
return Request.CreateResponse(HttpStatusCode.OK, "Server Error");
}
}
Namespaces:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
DB_ODMSchoolEntities DbContext = new DB_ODMSchoolEntities();
[HttpPost]
public HttpResponseMessage UploadExcel()
{
try
{
var httpRequest = HttpContext.Current.Request;
string guid = Guid.NewGuid().ToString();
if (httpRequest.Files.Count > 0)
{
var postedfile = httpRequest.Files[0];
var allowedExtensions = new[] { ".xlsx", ".xls" };
var checkextension = Path.GetExtension(postedfile.FileName).ToLower();
if (!allowedExtensions.Contains(checkextension)) //Check this file is excel or not
{
return Request.CreateResponse(HttpStatusCode.OK, "Select Excel File");
}
if (postedfile != null && postedfile.ContentLength > 0)// Check posted file is null or not
{
var path =HttpContext.Current.Server.MapPath("~/Excels/" + postedfile.FileName);
//save File
postedfile.SaveAs(path);
string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = excelConnectionString;
OleDbDataAdapter command = new OleDbDataAdapter("select * from [Department_Details$]", connection);
// connection.Open();
// Create DbDataReader to Data Worksheet
DataTable dt = new DataTable();
command.Fill(dt);
string error;
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
string Department_Name = dr["Department_Name"].ToString();
if (DbContext.Tbl_Department.Any(x => x.Department_Name == Department_Name.Trim()))
{
error += dr["Department_Name"].ToString() + " Already Exists.";
}
else
{
Tbl_Department s = new Tbl_Department();
s.Department_Name = dr["Department_Name"].ToString();
s.Inserted_On = DateTime.Now;
s.Is_Active = true;
DbContext.Tbl_Department.Add(s);
DbContext.SaveChanges();
}
}
}
if ((System.IO.File.Exists(path)))
{
System.IO.File.Delete(path);
}
}
return Request.CreateResponse(HttpStatusCode.OK, "Department Details Entered Successfully");
}
else
{
return Request.CreateResponse(HttpStatusCode.OK, "Select Excel File");
}
}
catch
{
return Request.CreateResponse(HttpStatusCode.OK, "Server Error");
}
}
0 Comments