-
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.
Merge pull request #68 from marvinkobit/dev
Dev
- Loading branch information
Showing
12 changed files
with
390 additions
and
68 deletions.
There are no files selected for viewing
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
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,176 @@ | ||
using ExcelDataReader; | ||
using Labtracker.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
|
||
namespace Labtracker.Controllers | ||
{ | ||
public class DnaExtractExcelController : Controller | ||
{ | ||
// GET: DnaExtractExcel | ||
public ActionResult Index() | ||
{ | ||
DataTable dt = new DataTable(); | ||
try | ||
{ | ||
dt = (DataTable)Session["tmpdata"]; | ||
} | ||
catch (Exception) | ||
{ | ||
throw; | ||
} | ||
|
||
return View(dt); | ||
} | ||
|
||
|
||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public ActionResult Index(HttpPostedFileBase upload) | ||
{ | ||
|
||
if (ModelState.IsValid) | ||
{ | ||
|
||
if (upload != null && upload.ContentLength > 0) | ||
{ | ||
// ExcelDataReader works with the binary Excel file, so it needs a FileStream | ||
// to get started. This is how we avoid dependencies on ACE or Interop: | ||
Stream stream = upload.InputStream; | ||
|
||
// We return the interface, so that | ||
IExcelDataReader reader = null; | ||
|
||
|
||
if (upload.FileName.EndsWith(".xls")) | ||
{ | ||
reader = ExcelReaderFactory.CreateBinaryReader(stream); | ||
} | ||
else if (upload.FileName.EndsWith(".xlsx")) | ||
{ | ||
reader = ExcelReaderFactory.CreateOpenXmlReader(stream); | ||
} | ||
else | ||
{ | ||
ModelState.AddModelError("File", "This file format is not supported"); | ||
return View(); | ||
} | ||
int fieldcount = reader.FieldCount; | ||
int rowcount = reader.RowCount; | ||
|
||
|
||
DataTable dt = new DataTable(); | ||
//dt.Columns.Add("UserName"); | ||
//dt.Columns.Add("Adddress"); | ||
DataRow row; | ||
|
||
|
||
DataTable dt_ = new DataTable(); | ||
string err1 = "", err2 = ""; | ||
try | ||
{ | ||
|
||
dt_ = reader.AsDataSet().Tables[0]; | ||
|
||
string ret = ""; | ||
|
||
for (int i = 0; i < dt_.Columns.Count; i++) | ||
{ | ||
dt.Columns.Add(dt_.Rows[0][i].ToString()); | ||
} | ||
|
||
int rowcounter = 0; | ||
for (int row_ = 1; row_ < dt_.Rows.Count; row_++) | ||
{ | ||
row = dt.NewRow(); | ||
|
||
for (int col = 0; col < dt_.Columns.Count; col++) | ||
{ | ||
|
||
row[col] = dt_.Rows[row_][col].ToString(); | ||
rowcounter++; | ||
} | ||
dt.Rows.Add(row); | ||
|
||
err1 = row[9].ToString(); | ||
err2 = row[10].ToString(); | ||
//db insert | ||
try | ||
{ | ||
using (var context = new SampleContext()) | ||
|
||
{ | ||
|
||
|
||
|
||
var tbgdnaextractupdates = new Dnaextract(); | ||
|
||
tbgdnaextractupdates.PatientId = row[0].ToString(); | ||
tbgdnaextractupdates.TubeLabel = row[1].ToString(); | ||
tbgdnaextractupdates.GoodQQ = row[2].ToString(); | ||
tbgdnaextractupdates.NDConc = string.IsNullOrWhiteSpace(row[3].ToString()) ? (decimal?)null : Convert.ToDecimal(row[3].ToString()); | ||
tbgdnaextractupdates.Purity = string.IsNullOrWhiteSpace(row[5].ToString()) ? (decimal?)null : Convert.ToDecimal(row[5].ToString()); | ||
tbgdnaextractupdates.SampleType = row[6].ToString(); | ||
tbgdnaextractupdates.QubitConc = row[7].ToString(); | ||
|
||
tbgdnaextractupdates.AssayReagent = row[9].ToString(); | ||
tbgdnaextractupdates.ExtractDate = string.IsNullOrEmpty(row[10].ToString()) ? (DateTime?)null : Convert.ToDateTime(row[10].ToString()); | ||
|
||
//tbgdnaextractupdates.Initial = labInitial; | ||
//tbgdnaextractupdates.Remark = remark; | ||
|
||
context.Dnaextracts.Add(tbgdnaextractupdates); | ||
context.SaveChanges(); | ||
|
||
|
||
} | ||
} | ||
catch (Exception ex1) | ||
{ | ||
throw ex1; | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
|
||
ModelState.AddModelError("File", ex.ToString() + err1 + err2 + "Unable to Upload file! Info: Data Validation Error, Invalid data entry."); | ||
|
||
return View(); | ||
} | ||
|
||
DataSet result = new DataSet();//reader.AsDataSet(); | ||
result.Tables.Add(dt); | ||
string minutes_ID = ""; | ||
|
||
reader.Close(); | ||
reader.Dispose(); | ||
// return View(); | ||
// return View(result.Tables[0]); | ||
|
||
DataTable ddd = result.Tables[0]; | ||
|
||
Session["tmpdata"] = ddd; | ||
|
||
return RedirectToAction("Index"); | ||
|
||
} | ||
else | ||
{ | ||
ModelState.AddModelError("File", "Please Choose Your file to Upload"); | ||
} | ||
} | ||
return View(); | ||
} | ||
|
||
} | ||
} |
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
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
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,119 @@ | ||
|
||
|
||
@{ | ||
ViewBag.Title = "DNA Extract Excel Upload"; | ||
} | ||
|
||
@model System.Data.DataTable | ||
@using System.Data; | ||
|
||
|
||
|
||
|
||
<script src="~/Scripts/jquery-3.3.1.js"></script> | ||
|
||
<script> | ||
function bs_input_file() { | ||
$(".input-file").before( | ||
function () { | ||
if (!$(this).prev().hasClass('input-ghost')) { | ||
var element = $("<input type='file' id='dataFile' name='upload' class='input-ghost' style='visibility:visible; height:20;'>"); | ||
element.attr("name", $(this).attr("name")); | ||
element.change(function () { | ||
element.next(element).find('input').val((element.val()).split('\\').pop()); | ||
}); | ||
$(this).find("button.btn-choose").click(function () { | ||
element.click(); | ||
}); | ||
$(this).find("button.btn-reset").click(function () { | ||
element.val(null); | ||
$(this).parents(".input-file").find('input').val(''); | ||
}); | ||
$(this).find('input').css("cursor", "pointer"); | ||
$(this).find('input').mousedown(function () { | ||
$(this).parents('.input-file').prev().click(); | ||
return false; | ||
}); | ||
return element; | ||
} | ||
} | ||
); | ||
} | ||
function clear() { | ||
var input = $("#dataFile").val(''); | ||
}; | ||
$(function () { | ||
clear(); | ||
bs_input_file(); | ||
}); | ||
</script> | ||
|
||
@using (Html.BeginForm("Index", "DnaExtractExcel", null, FormMethod.Post, new { enctype = "multipart/form-data" })) | ||
{ | ||
<div class="info2"> | ||
<h2>DnaExtract Excel File Upload</h2> | ||
@Html.AntiForgeryToken() | ||
|
||
<div class="form-group" name="Fichier1"> | ||
<!-- COMPONENT START --> | ||
<div class="form-group"> | ||
<div class="input-group input-file"> | ||
<span class="input-group-btn"> | ||
@*<button class="btn btn-default btn-choose" type="button">Choose</button>*@ | ||
</span> | ||
@*<input type="text" class="form-control" placeholder='Choose a file...' />*@ | ||
|
||
</div> | ||
</div> | ||
@*<input type="file" id="dataFile" name="upload"/>*@ | ||
|
||
</div> | ||
|
||
<div class="form-group"> | ||
<input type="submit" value="Import from Excel" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm" /> | ||
@*<button class="btn btn-default" style="background-color:Highlight" >Save</button>*@ | ||
</div> | ||
|
||
|
||
<div class="alert alert-danger"> | ||
@Html.ValidationSummary() | ||
@*<strong></strong> asdasd*@ | ||
</div> | ||
|
||
|
||
|
||
</div> | ||
if (Model != null) | ||
{ | ||
<table id="table1" class="table table-bordered table-hover"> | ||
<thead> | ||
<tr> | ||
@foreach (DataColumn col in Model.Columns) | ||
{ | ||
<th>@col.ColumnName</th> | ||
} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
@foreach (DataRow row in Model.Rows) | ||
{ | ||
<tr> | ||
@foreach (DataColumn col in Model.Columns) | ||
{ | ||
<td>@row[col.ColumnName]</td> | ||
} | ||
</tr> | ||
} | ||
</tbody> | ||
</table> | ||
} | ||
} | ||
|
||
<script> | ||
$(document).ready(function () { | ||
$("table1").DataTable(); | ||
}); | ||
</script> | ||
|
||
|
Oops, something went wrong.