0% found this document useful (0 votes)
143 views7 pages

Browser Con Excel 1.0

This document contains code for a C# application that logs into a website, scrapes data tables from the accessed pages, and saves the scraped data to an HTML file and text file. It navigates the website, enters login credentials, and checks for errors. When authentication is successful, it retrieves and parses HTML tables to extract cell data, which it writes to an array and external files.

Uploaded by

RoRs
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views7 pages

Browser Con Excel 1.0

This document contains code for a C# application that logs into a website, scrapes data tables from the accessed pages, and saves the scraped data to an HTML file and text file. It navigates the website, enters login credentials, and checks for errors. When authentication is successful, it retrieves and parses HTML tables to extract cell data, which it writes to an array and external files.

Uploaded by

RoRs
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using HtmlAgilityPack;
//using Excel = System.Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
//using Excel;
using System.Reflection;
using System.Diagnostics;

namespace Browser
{
public partial class Form1 : Form
{
int n = 0, año, mes;
string[,] dato = new string[500, 500];
string mesl;

DateTime fecha;

public Form1()
{
InitializeComponent();
user.Text = Properties.Settings.Default.user;
pass.Text = Properties.Settings.Default.pass;
num.Text = Properties.Settings.Default.num;
fecha = DateTime.Now;
año = fecha.Year;
mes = fecha.Month;
}

public bool FindAndKillProcess(string name)


{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses())
{
//now we're going to see if any of the running processes
//match the currently running processes by using the StartsWith
Method,
//this prevents us from incluing the .EXE for the process we're
looking for.
//. Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running
if (clsProcess.ProcessName.StartsWith(name))
{
//since we found the proccess we now need to use the
//Kill Method to kill the process. Remember, if you have
//the process running more than once, say IE open 4
//times the loop thr way it is now will close all 4,
//if you want it to just close the first one it finds
//then add a return; after the Kill
clsProcess.Kill();
//process killed, return true
return true;
}
}
//process not found, return false
return false;
}
private void button1_Click(object sender, EventArgs e)
{
n = 0;
webBrowser1.Navigate("https://www.voxip.com.mx:9443/acceso");

private void tbDireccionWeb_KeyPress(object sender, KeyPressEventArgs e)


{
if (e.KeyChar.Equals(Convert.ToChar(13)))
{
this.button1_Click(this.button1, null);
}
}

private void Acceder_Click(object sender, EventArgs e)


{

private void webBrowser1_DocumentCompleted(object sender,


WebBrowserDocumentCompletedEventArgs e)
{
n++;
if (n == 1)
{
if ((webBrowser1.Document.GetElementById("username") != null) &&
(webBrowser1.Document.GetElementById("password") != null) &&
(webBrowser1.Document.GetElementById("commit") != null))
{
webBrowser1.Document.GetElementById("username").InnerText =
user.Text;
webBrowser1.Document.GetElementById("password").InnerText =
pass.Text;

webBrowser1.Document.GetElementById("commit").InvokeMember("click");
}
else
{
label3.Text = "Error de conexión";
}
}

if (n == 2)
{
if ((webBrowser1.Document.GetElementById("username") == null) &&
(webBrowser1.Document.GetElementById("password") == null) &&
(webBrowser1.Document.GetElementById("commit") == null))
{
label3.Text = "Acceso correcto";

webBrowser1.Navigate("https://www.voxip.com.mx:9443/adicionales/llamadas");
}
if ((webBrowser1.Document.GetElementById("username") != null) &&
(webBrowser1.Document.GetElementById("password") != null) &&
(webBrowser1.Document.GetElementById("commit") != null))
label3.Text = "Contraseña Incorrecta";
}

if (n == 3)
{
#region fechas
if (mes == 1)
mesl = "Enero";
if (mes == 2)
mesl = "Febrero";
if (mes == 3)
mesl = "Marzo";
if (mes == 4)
mesl = "Abril";
if (mes == 5)
mesl = "Mayo";
if (mes == 6)
mesl = "Junio";
if (mes == 7)
mesl = "Julio";
if (mes == 8)
mesl = "Agosto";
if (mes == 9)
mesl = "Septiembre";
if (mes == 10)
mesl = "Octubre";
if (mes == 11)
mesl = "Noviembre";
if (mes == 12)
mesl = "Diciembre";
#endregion

webBrowser1.Document.GetElementById("ddMes").SetAttribute("value",
mesl);
webBrowser1.Document.GetElementById("ddAnio").SetAttribute("value",
año.ToString());
webBrowser1.Document.GetElementById("origen").SetAttribute("value",
"529991243052");
webBrowser1.Document.GetElementById("commit").InvokeMember("click");
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)


{
if (cb.Checked)
{
user.Enabled = false;
pass.Enabled = false;
num.Enabled = false;
}
else
{
user.Enabled = true;
pass.Enabled = true;
num.Enabled = true;
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)


{
Properties.Settings.Default.user = user.Text;
Properties.Settings.Default.pass = pass.Text;
Properties.Settings.Default.num = num.Text;
Properties.Settings.Default.Save();
FindAndKillProcess("EXCEL");

private void button2_Click(object sender, EventArgs e)


{
webBrowser1.Navigate("https://www.voxip.com.mx:9443/acceso/salida");
}

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)


{

webBrowser1.Navigate("https://www.voxip.com.mx/MiCuentaVox/verDetalle_voxip.asp");
/* WebClient client = new WebClient();

client.DownloadFile("https://www.voxip.com.mx/MiCuentaVox/verDetalle_voxip.asp",@"C:
\prueba.html");*/
e.Cancel = true;
}

private void button3_Click(object sender, EventArgs e)


{
string codigo;
//System.IO.FileStream fs = System.IO.File.OpenWrite("C:\\myfile.html");
//System.IO.FileStream documento = new
System.IO.FileStream(@"prueba.htm",System.IO.FileMode.Create);
//documento = this.webBrowser1.Document;
//StreamWriter sw = new StreamWriter(@"C:\p.html");
/*HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("https://www.voxip.com.mx/MiCuentaVox/verDetalle_v
oxip.asp");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
richTextBox1.Text = sr.ReadToEnd();
sr.Close();*/
codigo = webBrowser1.DocumentText;
richTextBox1.Text = codigo;
// HtmlDocument doc = webBrowser1.Document.OpenNew(true);*/
/*System.Windows.Forms.HtmlDocument documento =
webBrowser1.Document.OpenNew(true);
documento.Write(codigo);*/

FileStream fs = File.OpenWrite(@"C:\myfile.html");
StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
writer.Write(codigo);
writer.Close();

//documento contiene el html que me interesa...

HtmlWeb web = new HtmlWeb();


// HtmlAgilityPack.HtmlDocument doc new HtmlAgilityPack.HtmlDocument();
//doc =
HtmlAgilityPack.HtmlDocument.LoadHtml(richTextBox1.ToString());//web.Load("https://w
ww.voxip.com.mx/MiCuentaVox/verDetalle_voxip.asp");
StreamWriter sw = new StreamWriter("C:\\Test.txt");

// doc = doc.LoadHtml(richTextBox1.ToString());
HtmlAgilityPack.HtmlDocument htmlDoc = new
HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(codigo);

HtmlNodeCollection tables = htmlDoc.DocumentNode.SelectNodes("//table");

HtmlNodeCollection rows = tables[0].SelectNodes(".//tr");

//proteger contra null

for (int i = 0; i < rows.Count; i++)


{
HtmlNodeCollection cols = rows[i].SelectNodes(".//td");
for (int j = 0; j < cols.Count; j++)
{
string value = cols[j].InnerText;
sw.Write(value);
//dataGridView1.Rows[i].Cells[j].Value = value;
dato[i, j] = value;
}
}
sw.Close();
}

/* private void button4_Click(object sender, EventArgs e)


{
Microsoft.Office.Interop.Excel.Application ExApp;
ExApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook oWBook;
Microsoft.Office.Interop.Excel._Worksheet oSheet;
oWBook = ExApp.Workbooks.Open(@"C:\prueba.xls", Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
oSheet =
(Microsoft.Office.Interop.Excel._Worksheet)oWBook.ActiveSheet;

oSheet.Cells[2, 10] = "Columna 10 fila 2";

ExApp.Visible = false;
ExApp.UserControl = true;
oWBook.Save();
//ExApp.ActiveWorkbook.Close(true, ExcelFile, Type.Missing);
ExApp.ActiveWorkbook.Close(true, Type.Missing, Type.Missing);
ExApp.Quit();
ExApp = null;
}*/

private void button4_Click(object sender, System.EventArgs e)


{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell.

//Format A1:D1 as bold, vertical alignment = center.


oSheet.get_Range("A1", "G1").Font.Bold = true;
oSheet.get_Range("A1", "D1").VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
oSheet.get_Range("A1","G1").ColumnWidth = 20;

// Create an array to multiple values at once.


string[,] saNames = new string[5, 2];

saNames[0, 0] = "John";
saNames[0, 1] = "Smith";
saNames[1, 0] = "Tom";
saNames[1, 1] = "Brown";
saNames[2, 0] = "Sue";
saNames[2, 1] = "Thomas";
saNames[3, 0] = "Jane";
saNames[3, 1] = "Jones";
saNames[4, 0] = "Adam";
saNames[4, 1] = "Johnson";
//Fill A2:B6 with an array of values (First and Last Names).
oSheet.get_Range("A1", "G40").Value2 = dato;

oSheet.Cells[1, 1] = "Fecha Y Hora";


oSheet.Cells[1, 2] = "Origen";
oSheet.Cells[1, 3] = "Destino";
oSheet.Cells[1, 4] = "Zona";
oSheet.Cells[1, 5] = "Localidad";
oSheet.Cells[1, 6] = "Duración";
oSheet.Cells[1, 7] = "Costo";

//Fill C2:C6 with a relative formula (=A2 & " " & B2).
// oRng = oSheet.get_Range("C2", "C6");
//oRng.Formula = "=A2 & \" \" & B2";
//Fill D2:D6 with a formula(=RAND()*100000) and apply format.
//oRng = oSheet.get_Range("D2", "D6");
//oRng.Formula = "=RAND()*100000";
//oRng.NumberFormat = "$0.00";
//AutoFit columns A:D.
//oRng = oSheet.get_Range("A1", "D1");
//oRng.EntireColumn.AutoFit();
//Manipulate a variable number of columns for Quarterly Sales Data.
//DisplayQuarterlySales(oSheet);
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
}
}

You might also like