Commit 5dc498e7 authored by datdiep's avatar datdiep

Update Program.cs

parent cd3fa7af
using OfficeOpenXml; using OfficeOpenXml;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
namespace testImport namespace testImport
{ {
public class excel_template public class excel_template
{ {
public Guid id { get; set; } public Guid id { get; set; }
public string name { get; set; } public string name { get; set; }
public string code { get; set; } public string code { get; set; }
public string type { get; set; } public string type { get; set; }
} }
public class customer_template public class customer_template
{ {
public Guid id { get; set; } public Guid id { get; set; }
public Guid customer_id { get; set; } public Guid customer_id { get; set; }
public Guid id_excel_template { get; set; } public Guid id_excel_template { get; set; }
public int sheet { get; set; } public int sheet { get; set; }
public string coordinate { get; set; } public string coordinate { get; set; }
public int order { get; set; } public int order { get; set; }
public Guid id_sub { get; set; } public Guid id_sub { get; set; }
public int body { get; set; } public int body { get; set; }
public int merge { get; set; } public int merge { get; set; }
} }
public class subdetail public class subdetail
{ {
public Guid id { get; set; } public Guid id { get; set; }
public Guid id_excel_template { get; set; } public Guid id_excel_template { get; set; }
public int row { get; set; } public int row { get; set; }
} }
public class saved public class saved
{ {
public Guid id { get; set; } public Guid id { get; set; }
public Guid id_excel_template { get; set; } public Guid id_excel_template { get; set; }
public Guid user_id { get; set; } public Guid user_id { get; set; }
public string value { get; set; } public string value { get; set; }
public DateTime Time { get; set; } public DateTime Time { get; set; }
public DateTime created { get; set; } public DateTime created { get; set; }
public string tmp { get; set; } public string tmp { get; set; }
} }
public class Program public class Program
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
List<excel_template> excel_Templates; List<excel_template> excel_Templates;
List<customer_template> customer_Templates; List<customer_template> customer_Templates;
List<subdetail> Subdetails; List<subdetail> Subdetails;
Data x = new Data(); Data x = new Data();
excel_Templates = x.fakeData().Item1; excel_Templates = x.fakeData().Item1;
customer_Templates = x.fakeData().Item2; customer_Templates = x.fakeData().Item2;
Subdetails = x.fakeData().Item3; Subdetails = x.fakeData().Item3;
int BODY = customer_Templates.FirstOrDefault().body; int BODY = customer_Templates.FirstOrDefault().body;
int SEED = customer_Templates.FirstOrDefault().merge; int SEED = customer_Templates.FirstOrDefault().merge;
//count sheet //count sheet
var _MAXSHEET = customer_Templates.Max(c => c.sheet); var _MAXSHEET = customer_Templates.Max(c => c.sheet);
var firstSheet = 0; var firstSheet = 0;
//variable ? //variable ?
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var filePath = @"C:\Users\Admin\Downloads\HR_SK.xlsx"; var filePath = @"C:\Users\Admin\Downloads\HR_SK.xlsx";
byte[] file = File.ReadAllBytes(filePath); byte[] file = File.ReadAllBytes(filePath);
List<saved> XX = new List<saved>(); List<saved> XX = new List<saved>();
using (MemoryStream stream = new MemoryStream(file)) using (MemoryStream stream = new MemoryStream(file))
{ {
using (ExcelPackage excelPackage = new ExcelPackage(stream)) using (ExcelPackage excelPackage = new ExcelPackage(stream))
{ {
while (firstSheet == _MAXSHEET) while (firstSheet == _MAXSHEET)
{ {
var DataSheet = excelPackage.Workbook.Worksheets[firstSheet]; var DataSheet = excelPackage.Workbook.Worksheets[firstSheet];
for (int i = 108; i <= DataSheet.Dimension.End.Row - Convert.ToInt32(BODY); i+=SEED) //i+=seed for (int i = 108; i <= DataSheet.Dimension.End.Row - Convert.ToInt32(BODY); i+=SEED) //i+=seed
{ {
//check last: if STT is not number => stop //check last: if STT is not number => stop
var testLast = DataSheet.Cells["A" + (Convert.ToInt32(BODY) + i).ToString()].Value is null ? " " : DataSheet.Cells["A" + (Convert.ToInt32(BODY) + i).ToString()].Value.ToString(); var testLast = DataSheet.Cells["A" + (Convert.ToInt32(BODY) + i).ToString()].Value is null ? " " : DataSheet.Cells["A" + (Convert.ToInt32(BODY) + i).ToString()].Value.ToString();
if (!int.TryParse(testLast, out _)) //https://stackoverflow.com/questions/894263/identify-if-a-string-is-a-number if (!int.TryParse(testLast, out _)) //https://stackoverflow.com/questions/894263/identify-if-a-string-is-a-number
break; break;
foreach (var value in customer_Templates.Where(c => c.sheet == firstSheet).ToList()) foreach (var value in customer_Templates.Where(c => c.sheet == firstSheet).ToList())
{ {
var Address = value.coordinate as String + (Convert.ToInt32(BODY) + i).ToString(); var Address = value.coordinate as String + (Convert.ToInt32(BODY) + i).ToString();
if (!value.coordinate.Contains(":")) if (!value.coordinate.Contains(":"))
{ {
string xxxxx = DataSheet.Cells[Address].Value is null ? "" : DataSheet.Cells[Address].Value.ToString(); string xxxxx = DataSheet.Cells[Address].Value is null ? "" : DataSheet.Cells[Address].Value.ToString();
saved save = new saved(); saved save = new saved();
save.value = xxxxx; save.value = xxxxx;
save.id_excel_template = value.id_excel_template; save.id_excel_template = value.id_excel_template;
XX.Add(save); XX.Add(save);
} }
else else
{ {
var split = value.coordinate.Split(":"); var split = value.coordinate.Split(":");
int countsub = 4; //count list on db int countsub = 4; //count list on db
for(int ii = 0; ii < countsub; ii++) for(int ii = 0; ii < countsub; ii++)
{ {
Address = split[0] + (Convert.ToInt32(BODY) + i + ii).ToString() + ":" + split[1] + (Convert.ToInt32(BODY) + i + ii).ToString(); Address = split[0] + (Convert.ToInt32(BODY) + i + ii).ToString() + ":" + split[1] + (Convert.ToInt32(BODY) + i + ii).ToString();
var range = DataSheet.Cells[Address]; var range = DataSheet.Cells[Address];
foreach (var rangeBasse in range) foreach (var rangeBasse in range)
{ {
saved save = new saved(); saved save = new saved();
string xxx = rangeBasse.Value is null ? "" : rangeBasse.Value.ToString(); string xxx = rangeBasse.Value is null ? "" : rangeBasse.Value.ToString();
save.value = xxx; save.value = xxx;
save.id_excel_template = value.id_excel_template; save.id_excel_template = Subdetails[ii].id_excel_template;
var tmpx = new[] { "vào", "ra", "tăng ca", "tông công" }; var tmpx = new[] { "vào", "ra", "tăng ca", "tông công" };
save.tmp = tmpx[ii]; save.tmp = tmpx[ii];
XX.Add(save); XX.Add(save);
} }
} }
} }
} }
} }
firstSheet++; firstSheet++;
} }
} }
} }
Console.WriteLine("xxx"); Console.WriteLine("xxx");
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment