Files
SIG/General/ExcelToBD.aspx.cs
JORGE LUIS CAUDILLO ESPARZA b4dd7ccd89
2021-09-29 06:00:59 -07:00

256 lines
8.2 KiB
C#

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;
using ClosedXML.Excel;
using Extensiones;
public partial class General_ExcelToBD : System.Web.UI.Page
{
#region propiedades
private string URLFile
{
get
{
if (ViewState["URLFile"] == null) { return ""; }
else { return ViewState["URLFile"].ToString(); }
}
set { ViewState["URLFile"] = value; }
}
private string extension
{
get
{
if (ViewState["extension"] == null) { return ""; }
else { return ViewState["extension"].ToString(); }
}
set { ViewState["extension"] = value; }
}
private List<ADSWorksBooks> workBooks
{
get
{
if (ViewState["workBooks"] == null) { return null; }
else { return (List<ADSWorksBooks>)ViewState["workBooks"]; }
}
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)
{
}
protected void LnkCargarArchivo_Click(object sender, EventArgs e)
{
List<Archivo> filesgral = TxtArchivoXls.getfiles().Archivos;
workBooks = new List<ADSWorksBooks>();
foreach (Archivo fileItem in filesgral)
{
extension = System.IO.Path.GetExtension(string.Format(@"{0}Temp\{1}", AppDomain.CurrentDomain.BaseDirectory, fileItem.filename)).Replace(".", "");
URLFile = fileItem.FileUrl;
}
using (var workbook = new XLWorkbook(URLFile))
{
int position = 1;
foreach (IXLWorksheet wokitem in workbook.Worksheets)
{
workBooks.Add(new ADSWorksBooks() { Name = wokitem.Name, position = position++ });
}
}
DdlWorkBooks.DataSource = workBooks;
DdlWorkBooks.DataBind();
if (workBooks.Count > 0)
{
DdlWorkBooks.SelectedIndex = 1;
DdlWorkBooks_SelectedIndexChanged(null, null);
}
LnkContinuar_Click(null, null);
}
protected void DdlWorkBooks_SelectedIndexChanged(object sender, EventArgs e)
{
using (var workbook = new XLWorkbook(URLFile))
{
int Filas = 0;
int columnas = 0;
IXLWorksheet workSheet = workbook.Worksheet(DdlWorkBooks.SelectedValue.ToEntero());
Filas = workSheet.LastColumnUsed().CellCount();
columnas = workSheet.LastRowUsed().CellCount();
TxtFilas.Text = Filas.ToString();
TxtInicia.Text = "2";
TxtColumnas.Text = "48";//"62";// columnas.ToString();
}
}
protected void Lnkdoit_Click(object sender, EventArgs e)
{
GVDatos.DataSource = GetDatosExcel();
GVDatos.DataBind();
}
protected void LnkContinuar_Click(object sender, EventArgs e)
{
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;
}
}
[Serializable]
public class ADSWorksBooks
{
public int position { get; set; }
public string Name { get; set; }
}