Skip to content

Commit 148b48b

Browse files
committed
Add project files.
1 parent 705dc51 commit 148b48b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+40263
-0
lines changed

ExportExcel-in-ASPNetCore.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31912.275
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExportExcel-in-ASPNetCore", "ExportExcel-in-ASPNetCore\ExportExcel-in-ASPNetCore.csproj", "{E58F8DEF-0269-4F30-8728-D81BA6F62250}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E58F8DEF-0269-4F30-8728-D81BA6F62250}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E58F8DEF-0269-4F30-8728-D81BA6F62250}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E58F8DEF-0269-4F30-8728-D81BA6F62250}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E58F8DEF-0269-4F30-8728-D81BA6F62250}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C8A750E2-A817-4DB1-8875-8803B03A4060}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using ClosedXML.Excel;
2+
using ExportExcel_in_ASPNetCore.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Extensions.Logging;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
12+
namespace ExportExcel_in_ASPNetCore.Controllers
13+
{
14+
public class HomeController : Controller
15+
{
16+
private readonly ILogger<HomeController> _logger;
17+
private readonly myDataContext _context;
18+
19+
public HomeController(ILogger<HomeController> logger, myDataContext context)
20+
{
21+
_logger = logger;
22+
_context = context;
23+
}
24+
25+
public IActionResult Index()
26+
{
27+
List<Customer> customers = _context.customers.ToList();
28+
return View(customers);
29+
}
30+
[HttpPost]
31+
public IActionResult ExportExcel()
32+
{
33+
var customers = _context.customers.ToList();
34+
using (XLWorkbook wb = new XLWorkbook())
35+
{
36+
wb.Worksheets.Add(Common.ToDataTable(customers.ToList()));
37+
using (MemoryStream stream = new MemoryStream())
38+
{
39+
wb.SaveAs(stream);
40+
return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "customer.xlsx");
41+
}
42+
}
43+
return View();
44+
}
45+
46+
public IActionResult Privacy()
47+
{
48+
return View();
49+
}
50+
51+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
52+
public IActionResult Error()
53+
{
54+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
55+
}
56+
}
57+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>ExportExcel_in_ASPNetCore</RootNamespace>
6+
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="ClosedXML" Version="0.95.4" />
11+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.9" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Reflection;
5+
6+
namespace ExportExcel_in_ASPNetCore.Models
7+
{
8+
public static class Common
9+
{
10+
public static DataTable ToDataTable<T>(List<T> items)
11+
{
12+
DataTable dataTable = new DataTable(typeof(T).Name);
13+
//Get all the properties
14+
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
15+
foreach (PropertyInfo prop in Props)
16+
{
17+
//Setting column names as Property names
18+
dataTable.Columns.Add(prop.Name);
19+
}
20+
foreach (T item in items)
21+
{
22+
var values = new object[Props.Length];
23+
for (int i = 0; i < Props.Length; i++)
24+
{
25+
//inserting property values to datatable rows
26+
values[i] = Props[i].GetValue(item, null);
27+
}
28+
dataTable.Rows.Add(values);
29+
}
30+
//put a breakpoint here and check datatable
31+
return dataTable;
32+
}
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ExportExcel_in_ASPNetCore.Models
4+
{
5+
public class Customer
6+
{
7+
[Key]
8+
public int customer_id { get; set; }
9+
public string first_name { get; set; }
10+
public string last_name { get; set; }
11+
public string phone { get; set; }
12+
public string email { get; set; }
13+
public string street { get; set; }
14+
public string city { get; set; }
15+
public string state { get; set; }
16+
public string zip_code { get; set; }
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace ExportExcel_in_ASPNetCore.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace ExportExcel_in_ASPNetCore.Models
4+
{
5+
public class myDataContext : DbContext
6+
{
7+
public myDataContext(DbContextOptions<myDataContext> options) : base(options)
8+
{
9+
}
10+
11+
public DbSet<Customer> customers { get; set; }
12+
}
13+
}

ExportExcel-in-ASPNetCore/Program.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace ExportExcel_in_ASPNetCore
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:32299",
7+
"sslPort": 44347
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development",
16+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
17+
}
18+
},
19+
"ExportExcel_in_ASPNetCore": {
20+
"commandName": "Project",
21+
"dotnetRunMessages": "true",
22+
"launchBrowser": true,
23+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
24+
"environmentVariables": {
25+
"ASPNETCORE_ENVIRONMENT": "Development",
26+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
27+
}
28+
}
29+
}
30+
}

ExportExcel-in-ASPNetCore/Startup.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using ExportExcel_in_ASPNetCore.Models;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.HttpsPolicy;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Threading.Tasks;
13+
14+
namespace ExportExcel_in_ASPNetCore
15+
{
16+
public class Startup
17+
{
18+
public Startup(IConfiguration configuration)
19+
{
20+
Configuration = configuration;
21+
}
22+
23+
public IConfiguration Configuration { get; }
24+
25+
// This method gets called by the runtime. Use this method to add services to the container.
26+
public void ConfigureServices(IServiceCollection services)
27+
{
28+
services.AddControllersWithViews();
29+
string conStr = this.Configuration.GetConnectionString("DBConnection");
30+
services.AddDbContext<myDataContext>(options => options.UseSqlServer(conStr));
31+
}
32+
33+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
34+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
35+
{
36+
if (env.IsDevelopment())
37+
{
38+
app.UseDeveloperExceptionPage();
39+
}
40+
else
41+
{
42+
app.UseExceptionHandler("/Home/Error");
43+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
44+
app.UseHsts();
45+
}
46+
app.UseHttpsRedirection();
47+
app.UseStaticFiles();
48+
49+
app.UseRouting();
50+
51+
app.UseAuthorization();
52+
53+
app.UseEndpoints(endpoints =>
54+
{
55+
endpoints.MapControllerRoute(
56+
name: "default",
57+
pattern: "{controller=Home}/{action=Index}/{id?}");
58+
});
59+
}
60+
}
61+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@using ExportExcel_in_ASPNetCore.Models;
2+
@model IEnumerable<Customer>
3+
@{
4+
ViewData["Title"] = "Home Page";
5+
}
6+
<form method="post" asp-controller="Home" asp-action="ExportExcel">
7+
<input type="submit" value="Export" class="btn btn-primary"/>
8+
</form>
9+
10+
<table class="table table-bordered">
11+
<tr>
12+
<th>First Name</th>
13+
<th>Last Name</th>
14+
<th>Phone</th>
15+
<th>Email</th>
16+
<th>Street</th>
17+
<th>City</th>
18+
<th>State</th>
19+
<th>Zip</th>
20+
</tr>
21+
@foreach (Customer customer in Model)
22+
{
23+
<tr>
24+
<td>@customer.first_name</td>
25+
<td>@customer.last_name</td>
26+
<td>@customer.phone</td>
27+
<td>@customer.email</td>
28+
<td>@customer.street</td>
29+
<td>@customer.city</td>
30+
<td>@customer.state</td>
31+
<td>@customer.zip_code</td>
32+
</tr>
33+
}
34+
</table>
35+
36+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>

0 commit comments

Comments
 (0)