217 lines
6.9 KiB
C#
217 lines
6.9 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 Nomina_CargaArchivosNomina : 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 string workname
|
|
{
|
|
get
|
|
{
|
|
if (ViewState["workname"] == null) { return ""; }
|
|
else { return ViewState["workname"].ToString(); }
|
|
}
|
|
|
|
set { ViewState["workname"] = 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))
|
|
{
|
|
foreach (IXLWorksheet wokitem in workbook.Worksheets)
|
|
{
|
|
workname = wokitem.Name;
|
|
//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);
|
|
//}
|
|
|
|
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();
|
|
|
|
}
|
|
public DataTable GetDatosExcel()
|
|
{
|
|
|
|
DataTable dtDatos = new DataTable();
|
|
ConcurrentDictionary<string, string> ColumnsTitles = new ConcurrentDictionary<string, string>();
|
|
using (var workbook = new XLWorkbook(URLFile))
|
|
{
|
|
IXLWorksheet workSheet = workbook.Worksheet(2);
|
|
DateTime d = new DateTime();
|
|
bool continuarFila = true;
|
|
string validaValor;
|
|
|
|
int contadorValida = 0;
|
|
bool haveTitles = true;//chkcontieneTitulos.Checked;
|
|
int FilaInicial = 2;// TxtInicia.Text.ToEntero() > 0 ? TxtInicia.Text.ToEntero() : 1;
|
|
int ColumnaFinal = 62; //48;//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;
|
|
}
|
|
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;
|
|
}
|
|
|
|
protected void LnkSubirArchivo_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
} |