247 lines
No EOL
8 KiB
C#
247 lines
No EOL
8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using Microsoft.Office.Interop.Excel;
|
|
using System.Diagnostics;
|
|
using ULA.Cathay.CreditoModel.Entidades;
|
|
using Ultimus.Utilitarios;
|
|
|
|
|
|
namespace ULA.Cathay.WCFMetodosCathay.Library
|
|
{
|
|
|
|
public class UExcel
|
|
{
|
|
UltimusLogs log = new UltimusLogs("WCFMetodosCathay.Excel");
|
|
|
|
Microsoft.Office.Interop.Excel.Application app_excel;
|
|
Microsoft.Office.Interop.Excel.Workbook wb_workbook;
|
|
Microsoft.Office.Interop.Excel.Worksheet ws_worksheet;
|
|
|
|
public UExcel()
|
|
{
|
|
#region Inicialización de variables del proceso
|
|
app_excel = null;
|
|
wb_workbook = null;
|
|
ws_worksheet = null;
|
|
#endregion
|
|
}
|
|
|
|
|
|
public List<UExcelSpreadSheet> ExtraccionExcel(string path, List<UExcelSpreadSheet> Hojas)
|
|
{
|
|
try
|
|
{
|
|
|
|
log.Trace("Iniciando ejecución metodo de Extraccion Excel");
|
|
|
|
List<UExcelSpreadSheet> HojasResult = new List<UExcelSpreadSheet>();
|
|
|
|
Abrir_Archivo_Excel(path);
|
|
|
|
foreach (UExcelSpreadSheet ItemHoja in Hojas)
|
|
{
|
|
HojasResult.Add(Leer_Archivo_Excel(ItemHoja));
|
|
}
|
|
|
|
log.Trace("Finalizando ejecución metodo de Extraccion Excel");
|
|
|
|
return HojasResult;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error(ex);
|
|
throw ex;
|
|
}
|
|
finally {
|
|
Cerrar_Archivo_Excel(false);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public List<UExcelSpreadSheet> CargaExcel(string path, List<UExcelSpreadSheet> Hojas)
|
|
{
|
|
try
|
|
{
|
|
|
|
log.Trace("Iniciando ejecución metodo de Carga Excel");
|
|
|
|
List<UExcelSpreadSheet> HojasResult = new List<UExcelSpreadSheet>();
|
|
|
|
Abrir_Archivo_Excel(path);
|
|
|
|
foreach (UExcelSpreadSheet ItemHoja in Hojas)
|
|
{
|
|
HojasResult.Add(Cargar_Archivo_Excel(ItemHoja));
|
|
}
|
|
|
|
log.Trace("Finalizando ejecución metodo de Carga Excel");
|
|
|
|
return HojasResult;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error(ex);
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
Cerrar_Archivo_Excel(true);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private void Abrir_Archivo_Excel(string str_path)
|
|
{
|
|
try
|
|
{
|
|
log.Trace("Iniciando apertura de archivo");
|
|
|
|
// Creación de la aplicación Excel
|
|
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
|
|
app_excel = new Microsoft.Office.Interop.Excel.Application();
|
|
app_excel.DisplayAlerts = false;
|
|
app_excel.AskToUpdateLinks = false;
|
|
|
|
|
|
if (!File.Exists(str_path))
|
|
throw new Exception(string.Format("El archivo {0} no existe.", str_path));
|
|
|
|
log.Trace(string.Format ("Abriendo archivo {0}", str_path));
|
|
|
|
// Creación del acceso al excel
|
|
wb_workbook = app_excel.Workbooks.Open(str_path, 0, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, true, System.Type.Missing, "\t", false, false, System.Type.Missing, System.Type.Missing, 1, 0);
|
|
|
|
|
|
log.Trace(string.Format ("Archivo {0} abierto exitosamente", str_path));
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//Excepcion_Logica.WriteLog(this.GetType().BaseType.FullName, MethodInfo.GetCurrentMethod().Name, ex.Message);
|
|
//throw new Excepcion_Logica(ConfigurationManager.AppSettings["AS-017"], ex.InnerException);
|
|
log.Error(ex);
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
|
|
private void Cerrar_Archivo_Excel(bool SaveChange)
|
|
{
|
|
try
|
|
{
|
|
GC.Collect();
|
|
GC.WaitForPendingFinalizers();
|
|
|
|
// Cerrar la hoja actual del excel
|
|
if (ws_worksheet != null) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ws_worksheet);
|
|
|
|
// Cerrar el libro actual del excel
|
|
if (wb_workbook != null)
|
|
{
|
|
wb_workbook.Close(SaveChange, Type.Missing, Type.Missing);
|
|
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wb_workbook);
|
|
}
|
|
|
|
// Cerrar la aplicación
|
|
if (app_excel != null)
|
|
{
|
|
app_excel.Quit();
|
|
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app_excel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error(ex);
|
|
throw ex;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private UExcelSpreadSheet Leer_Archivo_Excel(UExcelSpreadSheet Hoja)
|
|
{
|
|
CultureInfo ci_culture_info;
|
|
try
|
|
{
|
|
ci_culture_info = new CultureInfo("en-us");
|
|
|
|
log.Trace(string.Format("Abriendo Hoja #{0}", Hoja.NoItem));
|
|
|
|
ws_worksheet = (Microsoft.Office.Interop.Excel.Worksheet)wb_workbook.Worksheets.get_Item(Hoja.NoItem);
|
|
|
|
log.Trace(string.Format("Hoja #{0} abierta exitosamente", Hoja.NoItem));
|
|
|
|
foreach (var ItemCelda in Hoja.Celdas)
|
|
{
|
|
log.Trace(string.Format("Iniciando Lectura de Fila {0} - Columna {1}", ItemCelda.Celda.Fila, ItemCelda.Celda.Columna));
|
|
|
|
ItemCelda.Valor = (string)((Range)ws_worksheet.Cells[ItemCelda.Celda.Fila, ItemCelda.Celda.Columna]).Text;
|
|
|
|
log.Trace(string.Format("Lectura de Fila {0} - Columna {1} exitosa", ItemCelda.Celda.Fila, ItemCelda.Celda.Columna));
|
|
}
|
|
|
|
|
|
log.Trace(string.Format("Lectura de Hoja #{0} finalizada con Exito", Hoja.NoItem));
|
|
|
|
return Hoja;
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
log.Error(ex);
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private UExcelSpreadSheet Cargar_Archivo_Excel(UExcelSpreadSheet Hoja)
|
|
{
|
|
CultureInfo ci_culture_info;
|
|
try
|
|
{
|
|
ci_culture_info = new CultureInfo("en-us");
|
|
|
|
log.Trace(string.Format("Abriendo Hoja #{0}", Hoja.NoItem));
|
|
|
|
ws_worksheet = (Microsoft.Office.Interop.Excel.Worksheet)wb_workbook.Worksheets.get_Item(Hoja.NoItem);
|
|
|
|
log.Trace(string.Format("Hoja #{0} abierta exitosamente", Hoja.NoItem));
|
|
|
|
foreach (var ItemCelda in Hoja.Celdas)
|
|
{
|
|
log.Trace(string.Format("Iniciando Cargado de Fila {0} - Columna {1}", ItemCelda.Celda.Fila, ItemCelda.Celda.Columna));
|
|
|
|
|
|
((Range)ws_worksheet.Cells[ItemCelda.Celda.Fila, ItemCelda.Celda.Columna]).Value = ItemCelda.Valor;
|
|
ItemCelda.Result = true;
|
|
|
|
log.Trace(string.Format("Cargado de Fila {0} - Columna {1} exitosa", ItemCelda.Celda.Fila, ItemCelda.Celda.Columna));
|
|
}
|
|
|
|
|
|
log.Trace(string.Format("Cargado de Hoja #{0} finalizada con Exito", Hoja.NoItem));
|
|
|
|
return Hoja;
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
log.Error(ex);
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
}
|
|
} |