-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1f0afaa
Showing
208 changed files
with
18,963 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using ExcelDataReader; | ||
using ExcelHandler.Api.Database; | ||
using ExcelHandler.Api.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using OfficeOpenXml; | ||
using OfficeOpenXml.Style; | ||
|
||
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 | ||
|
||
namespace ExcelHandler.Api.Controllers | ||
{ | ||
[Route("api/[controller]/[action]")] | ||
[ApiController] | ||
public class HomeController : ControllerBase | ||
{ | ||
private AppDbContext _context; | ||
|
||
public HomeController(AppDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> UploadFile(IFormFile file) | ||
{ | ||
var fileName = file.FileName; | ||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); | ||
|
||
using (var stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.Read)) | ||
{ | ||
|
||
IExcelDataReader reader; | ||
|
||
reader = ExcelReaderFactory.CreateReader(stream); | ||
|
||
var conf = new ExcelDataSetConfiguration | ||
{ | ||
ConfigureDataTable = _ => new ExcelDataTableConfiguration | ||
{ | ||
UseHeaderRow = true | ||
} | ||
}; | ||
|
||
var dataSet = reader.AsDataSet(conf); | ||
|
||
var dataTable = dataSet.Tables[0]; | ||
|
||
|
||
for (var i = 0; i < dataTable.Rows.Count; i++) | ||
{ | ||
Employee employee =new Employee | ||
{ | ||
FirstName = dataTable.Rows[i][0].ToString(), | ||
LastName = dataTable.Rows[i][1].ToString(), | ||
Salary = Decimal.Parse(dataTable.Rows[i][2].ToString()) | ||
}; | ||
|
||
_context.employees.Add(employee); | ||
} | ||
|
||
} | ||
|
||
await _context.SaveChangesAsync(); | ||
var result = Getemployees().Result.Value; | ||
return Ok(result); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> ExportExcel() | ||
{ | ||
byte[] fileContents; | ||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; | ||
using (var package = new ExcelPackage()) | ||
{ | ||
// Set author for excel file | ||
package.Workbook.Properties.Author = "Aspodel"; | ||
// Set title for excel file | ||
package.Workbook.Properties.Title = "Employee List"; | ||
// Add comment to excel file | ||
package.Workbook.Properties.Comments = "Hello (^_^)"; | ||
var worksheet = package.Workbook.Worksheets.Add("Sheet1"); | ||
|
||
worksheet.Cells[1, 1].Value = "No"; | ||
worksheet.Cells[1, 2].Value = "First Name"; | ||
worksheet.Cells[1, 3].Value = "Last Name"; | ||
worksheet.Cells[1, 4].Value = "Salary"; | ||
|
||
// Style for Excel | ||
worksheet.DefaultColWidth = 16; | ||
worksheet.Cells.Style.Font.Size = 16; | ||
|
||
|
||
//Export Data from Table employees | ||
var list = await _context.employees.ToListAsync(); | ||
for (int i = 0; i < list.Count; i++) | ||
{ | ||
var item = list[i]; | ||
worksheet.Cells[i + 2, 1].Value = i + 1; | ||
worksheet.Cells[i + 2, 2].Value = item.FirstName; | ||
worksheet.Cells[i + 2, 3].Value = item.LastName; | ||
worksheet.Cells[i + 2, 4].Value = item.Salary; | ||
} | ||
|
||
fileContents = package.GetAsByteArray(); | ||
} | ||
|
||
if(fileContents == null || fileContents.Length == 0) | ||
{ | ||
return NoContent(); | ||
} | ||
|
||
return File( | ||
fileContents: fileContents, | ||
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
fileDownloadName: "Employees.xlsx"); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<Employee>>> Getemployees() | ||
{ | ||
return await _context.employees.ToListAsync(); | ||
} | ||
|
||
|
||
[HttpDelete] | ||
public async Task<ActionResult<Employee>> DeleteAll() | ||
{ | ||
var all = from allEmployees in _context.employees select allEmployees; | ||
_context.employees.RemoveRange(all); | ||
await _context.SaveChangesAsync(); | ||
|
||
var result = Getemployees().Result.Value; | ||
return Ok(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace ExcelHandler.Api.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
var rng = new Random(); | ||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = rng.Next(-20, 55), | ||
Summary = Summaries[rng.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using ExcelHandler.Api.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ExcelHandler.Api.Database | ||
{ | ||
public class AppDbContext:DbContext | ||
{ | ||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } | ||
public DbSet<Employee> employees { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="EPPlus" Version="5.5.0" /> | ||
<PackageReference Include="ExcelDataReader" Version="3.6.0" /> | ||
<PackageReference Include="ExcelDataReader.DataSet" Version="3.6.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.10" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.10" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.1" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Controller_SelectedScaffolderID>ApiControllerWithActionsScaffolder</Controller_SelectedScaffolderID> | ||
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath> | ||
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth> | ||
</PropertyGroup> | ||
</Project> |
46 changes: 46 additions & 0 deletions
46
ExcelHandler.Api/Migrations/20201220074023_first.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
namespace ExcelHandler.Api.Migrations | ||
{ | ||
public partial class first : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "employees", | ||
columns: table => new | ||
{ | ||
Id = table.Column<int>(type: "int", nullable: false) | ||
.Annotation("SqlServer:Identity", "1, 1"), | ||
FirstName = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
LastName = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
Salary = table.Column<decimal>(type: "decimal(18,2)", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_employees", x => x.Id); | ||
}); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "employees"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// <auto-generated /> | ||
using ExcelHandler.Api.Database; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
namespace ExcelHandler.Api.Migrations | ||
{ | ||
[DbContext(typeof(AppDbContext))] | ||
partial class AppDbContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
#pragma warning disable 612, 618 | ||
modelBuilder | ||
.UseIdentityColumns() | ||
.HasAnnotation("Relational:MaxIdentifierLength", 128) | ||
.HasAnnotation("ProductVersion", "5.0.1"); | ||
|
||
modelBuilder.Entity("ExcelHandler.Api.Models.Employee", b => | ||
{ | ||
b.Property<int>("Id") | ||
.ValueGeneratedOnAdd() | ||
.HasColumnType("int") | ||
.UseIdentityColumn(); | ||
|
||
b.Property<string>("FirstName") | ||
.HasColumnType("nvarchar(max)"); | ||
|
||
b.Property<string>("LastName") | ||
.HasColumnType("nvarchar(max)"); | ||
|
||
b.Property<decimal>("Salary") | ||
.HasColumnType("decimal(18,2)"); | ||
|
||
b.HasKey("Id"); | ||
|
||
b.ToTable("employees"); | ||
}); | ||
#pragma warning restore 612, 618 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ExcelHandler.Api.Models | ||
{ | ||
public class Employee | ||
{ | ||
public int Id { get; set; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
[Column(TypeName = "decimal(18,2)")] | ||
public decimal Salary { get; set; } | ||
} | ||
} |
Oops, something went wrong.