using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
//great for using missing.value;
using System.Reflection;
using Microsoft.Office.Core;
using System.Threading;
using System.IO;
namespace DeannaProj2
{
public partial class Form1 : Form
{
//declaring and object Called AppExcel
private Excel.Application AppExcel = null;
ArrayManipulator AM = new ArrayManipulator();
string strSource = null;
string strRowCount = null;
public string [,] arrySingle = null;
public Form1()
{
InitializeComponent();
}
//to take care of opening and picking the excel file
private void btnExcel_Click(object sender, EventArgs e)
{
exceldata.ShowDialog();
strSource = exceldata.FileName;
lblSource.Text = strSource;
}
private void btnRow_Click(object sender, EventArgs e)
{
//to check to see if user has selected an excel file
if (strSource == "")
{
MessageBox.Show("Please Pick An Excel File", "Error");
}
strRowCount = txtbxRowCount.Text;
btnStart.Enabled = true;
btnLabels.Enabled = true;
}
private void btnStart_Click(object sender, EventArgs e)
{
//to check to see if user has selected an excel file
if ((strSource == "") || (strSource == null))
{
MessageBox.Show("Please Pick An Excel File", "Error");
}
//then creating and starting the application
InitializeComponent();
AppExcel = new Excel.Application();
if (AppExcel == null)
{
MessageBox.Show("Could not start Excel");
exceldata.ShowDialog();
}
//now declaring to start a new workbook readonly
Excel.Workbook WB1 = AppExcel.Workbooks.Open(strSource, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
0, true, 0, 0);
//to get the compile a list of worksheets
Excel.Sheets WBsheets = WB1.Worksheets;
//get the first and only worksheet the 1 if for the first sheet in the workbook
Excel.Worksheet sheet = (Excel.Worksheet)WBsheets.get_Item(1);
//prep the row count by subtracting one cause of 0 based counting
int intRowCount = System.Convert.ToInt32(strRowCount);
strRowCount = System.Convert.ToString(intRowCount);
//here to prep the rows by adding a Colum 5 ("E")
strRowCount = "E" + strRowCount;
//here you define your range
Excel.Range range = sheet.get_Range("A1", strRowCount);
//here you pull the data out
System.Array arryReturn = (System.Array)range.Cells.Value2;
//good old cleanup
arryReturn = null;
arrySource = null;
//end of dangerous code
#endregion
//to show excel
// AppExcel.Visible = true;
//going to kill excel so it removes the deadlock on the file
KillExcel();
}
private void btnKillExcel_Click(object sender, EventArgs e)
{
//will call KillExcel procedure
KillExcel();
}
private void btnLabels_Click(object sender, EventArgs e)
{ //will build the schema for the array
AM.arryTemp = new string[arrySingle.Length / 5, 5];
//this will then copy the contents of the array ____MAKE NOTE_____
Array.Copy(arrySingle, AM.arryTemp, arrySingle.Length);
//Will start the multiplying process
AM.Multiplexer();
//Start the Write To Excel Process
WriteToExcel();
}
public void WriteToExcel()
{
//then creating and starting the application
InitializeComponent();
AppExcel = new Excel.Application();
if (AppExcel == null)
{
MessageBox.Show("Could not start Excel");
exceldata.ShowDialog();
}
//now declaring to start a new workbook
Excel.Workbook WB1 = AppExcel.Workbooks.Open(strSource, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
0, true, 0, 0);
//to get the compile a list of worksheets
Excel.Sheets WBsheets = WB1.Worksheets;
//get the first and only worksheet the 2 if for the first sheet in the workbook
Excel.Worksheet sheet = (Excel.Worksheet)WBsheets.get_Item(2);
//will take the length of arryFinal and divide by five cause multi deimension array
int intLenght = AM.arryFinal.Length / 5;
//now that the array has then been fed and written to excel, the WorkBook must be saved to the hard drive
sheet.SaveAs(strSource, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
try
{
//messagebox to show the process is done
MessageBox.Show("The Multiplexing is complete.", "Completed");
}
catch
{
MessageBox.Show("Operation Cancled By User (Did Not Save Over)", "Error");
}
}
public void KillExcel()
{
//this will fish out all the process that have excel
//the secret is "process name w/o .exe"
Process[] process = Process.GetProcessesByName("Excel");
for (int i = 0; i < process.GetLength(0); i++)
{
//kills excel process
process[i].Kill();
}
}
private void Form1_Load(object sender, EventArgs e)
{
#region Licensing
//pulls the hostname (LBMISTECH1) all caps too
string strHostName = System.Environment.MachineName;
// add the "\r\n" so it will match the line we will pull out of the text file
strHostName = strHostName + "\r\n";
//will assign where the file needs to go
string strPath = @"c:\windows\system32\work.mul";
//see if file work.mul exists in sytem32 folder
if (File.Exists(strPath))
{
MessageBox.Show("Welcome To Multiplexer.", " Written By Lee Hicks");
}
else
{
//this will execute if file is not present
MessageBox.Show("Please reinstall application", "Missing reference");
//will close the form
this.Close();
}
//this will try to open the text file to see if anything exists
try
{
//create a streamreader to read the text file
StreamReader srWorkMul = File.OpenText(strPath);
//dumb the contents from file to the string
string strContents = srWorkMul.ReadToEnd();
// test to see if contents of text file match the hostname
if (strContents == strHostName)
{
//do nothing
}
else
{
MessageBox.Show("Please reinstall application", "Missing reference");
//once again kill sthe app
this.Close();
}
}
catch
{
MessageBox.Show("Please reinstall application", "Missing reference");
//if the applications can't find the file to open will close it again
this.Close();
}
#endregion
}
}// end class
}// end namespace