Está en la página 1de 3

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Oracle.ManagedDataAccess.Client;
using Excel = ClosedXML.Excel;
using Microsoft.VisualBasic;
using System.IO;
namespace Informe_wpf_CS
{
/// <summary>
/// L�gica de interacci�n para MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

public DateTime GetDate()


{
return dtFecha.SelectedDate.Value;
}

public OracleConnection Conectar()


{

String cadena_conn;
cadena_conn = "Data
Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.21.201.7)
(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=CCPMAX)));User
Id=CCP;Password=ORACLE;";

OracleConnection conexion = new OracleConnection(cadena_conn);

try
{
conexion.Open();

catch
{
MessageBox.Show("No pude");
}
return conexion;
}

public string Cargar(String archivo)


{
String buf, item, linea;
int des = 0;
FileSystem.FileOpen(1, archivo, OpenMode.Binary, OpenAccess.Default);
buf =
Microsoft.VisualBasic.Strings.Space(Convert.ToInt32(FileSystem.FileLen(archivo)));
FileSystem.FileGet(1, ref buf);
FileSystem.FileClose(1);

StringReader lector = new StringReader(buf);


item = "";

while (lector.Peek() > -1)


{
linea = lector.ReadLine();
item = item + linea;
}

return item;
}

public System.Data.DataTable Query_to_datatable (string query)


{

OracleDataReader reader;
String cadena_cmd, fecha_str;
OracleConnection conexion;
OracleParameter tinicial, tfinal;
DateTime fecha;

fecha = GetDate();
conexion = Conectar();
cadena_cmd = Cargar(query);

tinicial = new OracleParameter("tinicial", OracleDbType.Varchar2);


tfinal = new OracleParameter("tfinal", OracleDbType.Varchar2);
fecha_str = Convert.ToString(fecha);
tinicial.Value = String.Format("{0:dd-MM-yyyy HH:mm:ss}",fecha);
tfinal.Value = String.Format("{0:dd-MM-yyyy 23:59:59}",fecha);

OracleCommand comando = new OracleCommand();


comando.Parameters.Add(tinicial);
comando.Parameters.Add(tfinal);
comando.Connection = conexion;
comando.CommandText = cadena_cmd;
comando.FetchSize = comando.FetchSize * 8;

MessageBox.Show("Voy a ejecutar la query");


System.Data.DataTable dt = new System.Data.DataTable();
OracleDataAdapter adap = new OracleDataAdapter(comando);
adap.Fill(dt);
// dt.Load(reader);

return dt;
}

public void ToExcel()


{
Excel.XLWorkbook libro;
Excel.IXLWorksheet hoja;
String nombre;
DateTime fecha;

System.Data.DataTable dt;
dt = Query_to_datatable("consulta.txt");
fecha = GetDate();
libro = new Excel.XLWorkbook("Plantilla_dia.xlsx");

hoja = libro.Worksheet(1);

for (int x = 0; x <= (dt.Rows.Count - 1); x++){


for (int y = 0; y<=(dt.Columns.Count - 1); y++)
{
hoja.Cell(x + 7, y + 1).Value =
Convert.ToString(dt.Rows[x].ItemArray[y]);
hoja.Cell(x + 7, y + 1).Style.Alignment.Horizontal =
Excel.XLAlignmentHorizontalValues.Center;
}
}

nombre = fecha.Year + String.Format("{0:00}", fecha) +


String.Format("{0:00}",fecha.Day) + ".xlsx";
libro.Worksheet(1).Name = String.Format("{0:00}", fecha.Day) +
fecha.ToString("MMM");

libro.SaveAs(nombre);
System.Diagnostics.Process.Start(nombre);

private void Button_Click(object sender, RoutedEventArgs e)


{
ToExcel();
}
}
}

También podría gustarte