My goal here is to read data from excel and show the data to the following html table.
In other words, I will pull the data from the Model and put it in the List from this model, and then I will draw the data to the table. In this case, how should the class construction be?
I shared my codes. Can you make an example accordingly?
Also, how should I research to learn such structures?
Classes
public class Dusumler : BaseEntity
{
public string BeyannameNO { get; set; }
public string StokKodu { get; set; }
public int KoliAdet { get; set; }
}
public class DusumDto
{
public string BeyannameNO { get; set; }
public string StokKodu { get; set; }
public int KoliAdet { get; set; }
}
Web Project Models
public class VM_Dusum
{
public List<Dusumlers> Dusumler { get; set; }
}
Controller
[HttpPost]
public IActionResult Index(IFormFile file)
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\Files");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
FileInfo fileInfo = new FileInfo(file.FileName);
string fileName = file.FileName;
string fileNameWithPath = Path.Combine(path, fileName);
//var stream = new FileStream(fileNameWithPath, FileMode.Create);
using (var stream = new FileStream(fileNameWithPath, FileMode.Create))
{
file.CopyToAsync(stream);
}
WorkBook workbook = WorkBook.Load(System.IO.File.OpenRead(fileNameWithPath));
WorkSheet sheet = workbook.WorkSheets.First();
string stok = sheet["A2"].StringValue;
string artikeladi = sheet["B2"].StringValue;
string bno = sheet["C2"].StringValue;
string ticaritanim = sheet["D2"].StringValue;
string karisim = sheet["E2"].StringValue;
decimal agirlik = sheet["F2"].DecimalValue;
string mensei = sheet["G2"].StringValue;
string gtip = sheet["H2"].StringValue;
string marka = sheet["I2"].StringValue;
string artikel = sheet["J2"].StringValue;
string kumaskodu = sheet["K2"].StringValue;
decimal birimfiyat = sheet["L2"].DecimalValue;
decimal toplamfiyat = sheet["M2"].DecimalValue;
int adet = sheet["N2"].IntValue;
int koliadet = sheet["O2"].IntValue;
Dusumlers dusum = new Dusumlers();
dusum.StokKodu = stok;
dusum.ArtikelName = artikeladi;
dusum.BeyannameNO = bno;
dusum.TicariTanim = ticaritanim;
dusum.Karisim = karisim;
dusum.Agirlik = agirlik;
dusum.Mensei = mensei;
dusum.GtipNo = gtip;
dusum.Marka = marka;
dusum.Artikel = artikel;
dusum.KumasKodu = kumaskodu;
dusum.BirimFiyat = birimfiyat;
dusum.ToplamFiyat = toplamfiyat;
dusum.Adet = adet;
dusum.KoliAdet = koliadet;
VM_Dusum dusums = new VM_Dusum();
dusums.Dusumler.Add(dusum);
return View();
}
View
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="content-page">
<div class="content">
<div class="container-fluid card-box table-responsive">
<div class="col-md-6">
<form asp-controller="Dusum" asp-action="Index" enctype="multipart/form-data">
<div class="form-group">
<p>Düşüm ekle</p>
<input type="file" name="file" class="filestyle" id="filestyleicon">
<button type="submit" style="margin-left:10px;" class="text-right btn btn-primary">Önizleme</button>
</div>
</form>
</div>
<div class="col-md-12">
<div class="table-responsive">
<div class="table-responsive">
<table class="table table-bordered mb-0">
<thead>
<tr>
<th>Stok Kodu</th>
<th>Artikel Adı</th>
<th>Beyanname No</th>
<th>Ticari Tanım</th>
<th>Karışım</th>
<th>Ağırlık</th>
<th>Menşei</th>
<th>Gtip No</th>
<th>Marka</th>
<th>Artikel</th>
<th>Kumaş Kodu</th>
<th>Birim Fiyat</th>
<th>Toplam Fiyat</th>
<th>Adet</th>
<th>Koli Adet</th>
</tr>
</thead>
<tbody>
@if (ViewBag.Dusum != null)
{
@foreach (var item in ViewBag.Dusum)
{
<tr class="bg-dark text-white">
<td></td>
<td></td>
<td></td>
<td>Gtip</td>
<td>KalemSayisi</td>
<td>Adet</td>
</tr>
}
}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>