Skip to content

Instantly share code, notes, and snippets.

@hemache
Created January 28, 2016 18:32
Show Gist options
  • Save hemache/30a15c9387da47cdf615 to your computer and use it in GitHub Desktop.
Save hemache/30a15c9387da47cdf615 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Data.SqlClient;
using System.IO;
using System.Xml.Linq;
using ClosedXML.Excel;
namespace Yoozer
{
public partial class FormSettings : Form
{
public FormSettings()
{
InitializeComponent();
notifyIconMain.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
this.ShowInTaskbar = false;
}
private void toolStripMenuItemExit_Click(object sender, EventArgs e)
{
this.Close();
base.OnFormClosing(new FormClosingEventArgs(CloseReason.ApplicationExitCall, false));
Application.Exit();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Show();
}
private void buttonChooseOutputDirectory_Click(object sender, EventArgs e)
{
folderBrowserDialogOutputDirectory.ShowDialog();
}
private void buttonSave_Click(object sender, EventArgs e)
{
Properties.Settings settings = Properties.Settings.Default;
settings.Frequency = dateTimePickerFrequency.Value;
settings.OutputDirectory = folderBrowserDialogOutputDirectory.SelectedPath;
settings.Save();
settings.Reload();
MessageBox.Show("Settings have been saved successfully", "Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void FormSettings_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
private void ToolStripMenuItemRunNow_Click(object sender, EventArgs e)
{
backgroundWorkerScrape.RunWorkerAsync();
}
private void backgroundWorkerScrape_DoWork(object sender, DoWorkEventArgs e)
{
var webClient = new WebClient();
var workbook = new XLWorkbook();
var filename = Path.Combine(Properties.Settings.Default.OutputDirectory, String.Format("{0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmss")));
foreach(string url in Properties.Settings.Default.XmlDataFilesUrls){
var xmlData = webClient.DownloadData(url);
var xmlStream = new MemoryStream(xmlData);
var xdocument = XDocument.Load(xmlStream);
var dataSet = new DataSet();
dataSet.ReadXml(
new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xdocument.Descendants("LIST_G_SQL1").First().ToString()))
);
var dataTable = dataSet.Tables[0];
workbook.AddWorksheet(dataTable, xdocument.Root.Name.LocalName);
}
workbook.SaveAs(filename);
e.Result = filename;
}
private void backgroundWorkerScrape_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
notifyIconMain.ShowBalloonTip(2000, "Updated", e.Result.ToString(), ToolTipIcon.Info);
}
private void notifyIconMain_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
}
private void timerMain_Tick(object sender, EventArgs e)
{
var desiredTime = Properties.Settings.Default.Frequency;
if(DateTime.Now.TimeOfDay.Hours == desiredTime.TimeOfDay.Hours
&& DateTime.Now.TimeOfDay.Minutes == desiredTime.TimeOfDay.Minutes)
{
notifyIconMain.ShowBalloonTip(1000, "Scheduled", "Updating...", ToolTipIcon.Info);
backgroundWorkerScrape.RunWorkerAsync();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment