This commit is contained in:
JORGE LUIS CAUDILLO ESPARZA
2021-09-23 08:10:35 -07:00
parent 29d0e84352
commit dd0da844f7
12 changed files with 344 additions and 80 deletions

View File

@@ -1,6 +1,9 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
@@ -41,6 +44,16 @@ public partial class General_ExcelToBD : System.Web.UI.Page
set { ViewState["workBooks"] = value; }
}
private ConcurrentDictionary<string, string> ColumnsTitles
{
get
{
if (ViewState["ColumnsTitles"] == null) { return null; }
else { return (ConcurrentDictionary<string, string>)ViewState["ColumnsTitles"]; }
}
set { ViewState["ColumnsTitles"] = value; }
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
@@ -64,21 +77,21 @@ public partial class General_ExcelToBD : System.Web.UI.Page
workBooks.Add(new ADSWorksBooks() { Name = wokitem.Name, position = position++ });
}
}
DdlWorkBooks.DataSource = workBooks;
DdlWorkBooks.DataBind();
if (workBooks.Count > 0)
{
DdlWorkBooks.SelectedIndex = 0;
DdlWorkBooks_SelectedIndexChanged(null,null);
DdlWorkBooks_SelectedIndexChanged(null, null);
}
}
protected void DdlWorkBooks_SelectedIndexChanged(object sender, EventArgs e)
{
using (var workbook = new XLWorkbook(URLFile))
{
{
int Filas = 0;
int columnas = 0;
@@ -86,25 +99,151 @@ public partial class General_ExcelToBD : System.Web.UI.Page
Filas = workSheet.LastColumnUsed().CellCount();
columnas = workSheet.LastRowUsed().CellCount();
TxtFilas.Text = Filas.ToString();
TxtInicia.Text = "1";
TxtColumnas.Text = Filas.ToString();
TxtInicia.Text = "2";
TxtColumnas.Text = "62";// columnas.ToString();
}
}
protected void Lnkdoit_Click(object sender, EventArgs e)
{
GVDatos.DataSource = GetDatosExcel();
GVDatos.DataBind();
}
protected void LnkContinuar_Click(object sender, EventArgs e)
{
int Filas = 0;
int columnas = 0;
DataSet ds = new DataSet();
DataTable dtDatos = new DataTable();
DataTable dt = GetDatosExcel();
ds.DataSetName = "DSDatos";
dtDatos.TableName = "TCol";
dt.TableName = "TData";
dtDatos.Columns.Add(new DataColumn("Columnas", typeof(string)));
dtDatos.Columns.Add(new DataColumn("LEN", typeof(int)));
foreach (DataColumn item in dt.Columns)
{
DataRow drow = dtDatos.NewRow();
drow["Columnas"] = item.ColumnName;
int longitud = 0;
foreach (DataRow dr in dt.Rows)
{
int length = dr[item.ColumnName].ToString().Length;
longitud = length > longitud ? length : longitud;
}
drow["LEN"] = longitud;
dtDatos.Rows.Add(drow);
}
ds.Tables.Add(dtDatos);
ds.Tables.Add(dt);
string xml = ds.GetXml();
using (ClsNominas objbase = new ClsNominas())
{
if (objbase.ExcelToBD(xml))
{
this.Alert("Información capturada", ModExtensiones.TipoMensaje.MsgInformacion);
}
}
GVDatos.DataSource = dt;
GVDatos.DataBind();
//this.Alert(string.Format("<div style=\"width:700px;\"><pre><xmp>{0}</xmp>\n</pre></br></div>", xml), ModExtensiones.TipoMensaje.MsgInformacion);
}
public string validtitle(string Valor)
{
Valor = Valor.Replace(" ", "_");
Valor = Valor.Replace("$$$$", "_Money_");
Valor = Valor.Replace("$$$", "_Money_");
Valor = Valor.Replace("$$", "_Money_");
Valor = Valor.Replace("$", "_Money_");
Valor = Valor.Replace(".", string.Empty);
Regex reg = new Regex("[*'\",&#^@]");
Valor = reg.Replace(Valor, string.Empty);
if (Valor.Trim().Length == 0)
{
Valor = "Val";
}
return Valor;
}
public DataTable GetDatosExcel()
{
DataTable dtDatos = new DataTable();
ColumnsTitles = new ConcurrentDictionary<string, string>();
using (var workbook = new XLWorkbook(URLFile))
{
IXLWorksheet workSheet = workbook.Worksheet(DdlWorkBooks.SelectedValue.ToEntero());
DateTime d = new DateTime();
bool continuarFila = true;
string validaValor;
int contadorValida = 0;
bool haveTitles = chkcontieneTitulos.Checked;
int FilaInicial = TxtInicia.Text.ToEntero() > 0 ? TxtInicia.Text.ToEntero() : 1;
int ColumnaFinal = TxtColumnas.Text.ToEntero();
int cFila = FilaInicial;
int cCol = 1;
while (cCol <= ColumnaFinal)
{
string Valor = cCol.ToString();
if (haveTitles)
{
Valor = workSheet.Cell(cFila, cCol).Value != null && workSheet.Cell(cFila, cCol).Value.GetType() == d.GetType() ? string.Format("{0:yyyy/MM/dd}", workSheet.Cell(cFila, cCol).Value) : workSheet.Cell(cFila, cCol).Value.ToString();
Valor = validtitle(Valor);
}
if (!ColumnsTitles.TryAdd(Valor, Valor))
{
int x = 2;
while (!ColumnsTitles.TryAdd(string.Format("{0}_{1}", Valor, x), Valor)) { x++; }
Valor = string.Format("{0}_{1}", Valor, x);
}
dtDatos.Columns.Add(new DataColumn(Valor, typeof(string)));
cCol++;
}
if (haveTitles)
{
cFila++;
}
while (continuarFila)
{
DataRow drow = dtDatos.NewRow();
cCol = 1;
validaValor = "";
while (cCol <= ColumnaFinal)
{
string Valor = workSheet.Cell(cFila, cCol).Value != null && workSheet.Cell(cFila, cCol).Value.GetType() == d.GetType() ? string.Format("{0:yyyy/MM/dd}", workSheet.Cell(cFila, cCol).Value) : workSheet.Cell(cFila, cCol).Value.ToString();
validaValor = string.Format("{0}{1}", validaValor, Valor);
drow[cCol - 1] = Valor;
cCol++;
}
if (validaValor.Trim() == "")
{
contadorValida++;
if (contadorValida > 30)
{
continuarFila = false;
}
}
else
{
contadorValida = 0;
dtDatos.Rows.Add(drow);
}
cFila++;
if (cFila >= 1048576)
{
continuarFila = false;
}
}
}
return dtDatos;
}
}