Skip to content

Instantly share code, notes, and snippets.

View CoreProgramm's full-sized avatar
🎯
Focusing

CoreProgramm CoreProgramm

🎯
Focusing
View GitHub Profile
@CoreProgramm
CoreProgramm / ImportExport.cshtml
Last active January 25, 2020 18:40
Import and Export Excel file in ASP .NET Core 3.1 razor page
<form asp-controller="Home" asp-action="Export">
<div class="container">
<div class="row">
<div class="col-md-4">
<input type="file" id="fileupload" name="files" class="form-control" />
</div>
<div class="col-md-3">
<input type="button" name="Upload" value="Upload" id="btnupload" class="btn btn-primary" />
<a href="@Url.Action("Download", "Home")">Download</a>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(function () {
$('#btnupload').on('click', function () {
var fileExtension = ['xls', 'xlsx'];
var filename = $('#fileupload').val();
if (filename.length == 0) {
alert("Please select a file.");
return false;
}
public ActionResult Import()
{
IFormFile file = Request.Form.Files[0];
string folderName = "UploadExcel";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
StringBuilder sb = new StringBuilder();
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
public ActionResult Download()
{
string Files = "wwwroot/UploadExcel/CoreProgramm_ExcelImport.xlsx";
byte[] fileBytes = System.IO.File.ReadAllBytes(Files);
System.IO.File.WriteAllBytes(Files, fileBytes);
MemoryStream ms = new MemoryStream(fileBytes);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "employee.xlsx");
}
public async Task<IActionResult> Export()
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = @"Employees.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;
public class HomeController : Controller
{
private readonly IHostingEnvironment _hostingEnv = null;
string AppBasePath = null;
string ContentRootPath = null;
public HomeController(IHostingEnvironment HostingEnv)
{
_hostingEnv = HostingEnv;
AppBasePath = _hostingEnv.WebRootPath; //path to wwwroot
ContentRootPath = _hostingEnv.ContentRootPath; //path to GetBasePath
public Startup(IConfiguration configuration,IHostingEnvironment _env)
{
Configuration = configuration;
string sAppPath = _env.ContentRootPath; //Application Base Path
string swwwRootPath = _env.WebRootPath; //wwwroot folder path
}
{
"mailConfig": {
"mailFrom": "info@coreprogramm.com",
"host": "smtp.google.com",
"portNo": "465",
"password": "CorePa$$word"
}
}
public class MailConfig
{
public string mailFrom { get; set; }
public string host { get; set; }
public string password { get; set; }
public int portNo { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<MailConfig>(Configuration.GetSection("mailConfig"));
services.AddControllersWithViews();
}