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.IO;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
namespace LibAnswersImport
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog ofd = new OpenFileDialog();
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (ofd.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = ofd.SafeFileName;
txtFilePath.Text = ofd.FileName;
}
}
private void btnDestination_Click(object sender, EventArgs e)
{
if (ofd.ShowDialog() == DialogResult.OK)
{
txtDestination.Text = ofd.FileName;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnImport_Click(object sender, EventArgs e)
{
int counter = 0;
int rowIndex = 0;
int colIndex = 0;
string line;
string data = txtDestination.Text;
Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Open(data);
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(txtFilePath.Text);
while ((line = file.ReadLine()) != null)
{
Console.WriteLine(line);
//Does the incrementing
counter++;
rowIndex++;
colIndex++;
//Makes sure data stays in one column
if (colIndex > 1)
colIndex--;
//Writes data to the cell
excelApp.Cells[rowIndex, colIndex] = line;
}
file.Close();
excelApp.Visible = true;
rtbOutput.AppendText("File Import Successfull!\n\n" +
"File contained " + counter + " lines of text");
// Suspend the screen.
Console.ReadLine();
}
}
}