using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text.RegularExpressions; //split words from a line
namespace ReadExcelData
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = new StreamWriter(@"c:\text.txt");
string connString = "";
string fileExtension = "";
string fileName = "";
string ExcelPath = @"C:\Test.xlsx";
fileName = Path.GetFileName(ExcelPath);
fileExtension = Path.GetExtension(ExcelPath);
if (fileExtension == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
}
else if (fileExtension == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
}
OleDbConnection con = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text; // Declaration of cmd as text
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd); //Take data from source to data table
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); //Adding all sheet Names into tables
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
DataTable dtColumnName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, getExcelSheetName, null });
DataView dv = dtColumnName.DefaultView;
dv.Sort = "ORDINAL_POSITION";
dtColumnName = dv.ToTable();
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
con.Close();
List<string> listColumn = new List<string>();
foreach (DataRow row in dtColumnName.Rows)
{
listColumn.Add(row["Column_name"].ToString());
}
foreach (string prime in listColumn) // Loop through List with foreach
{
sw.Write(prime + '\t');
}
sw.WriteLine();
foreach (DataRow row in dtExcelRecords.Rows)
//for(int i=0 ;i<dtExcelRecords.Rows.Count;i++)
{
foreach (var item in row.ItemArray) // Loop over the items.
//for(int j=0;j<dtExcelRecords.Columns .Count ;j++)
{
//sw.Write(dtExcelRecords.Rows[i][j].ToString());
sw.Write(item); // Invokes ToString abstract method.
sw.Write('\t');
}
sw.WriteLine();
}
//Console.Write(getExcelSheetName);
sw.Close();
StreamReader sr = new StreamReader(@"c:\text.txt");
string[] columns;
string line;
while ((line = sr.ReadLine()) != null)
{
columns = line.Split('\t'); //using for single character as delimiter
//Regex.Split(line, "\t"); //using for string as delimiter
foreach (string part in columns)
{
Console.WriteLine(part);
}
}
Console.Read();
sr.Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text.RegularExpressions; //split words from a line
namespace ReadExcelData
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = new StreamWriter(@"c:\text.txt");
string connString = "";
string fileExtension = "";
string fileName = "";
string ExcelPath = @"C:\Test.xlsx";
fileName = Path.GetFileName(ExcelPath);
fileExtension = Path.GetExtension(ExcelPath);
if (fileExtension == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
}
else if (fileExtension == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
}
OleDbConnection con = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text; // Declaration of cmd as text
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd); //Take data from source to data table
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); //Adding all sheet Names into tables
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
DataTable dtColumnName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, getExcelSheetName, null });
DataView dv = dtColumnName.DefaultView;
dv.Sort = "ORDINAL_POSITION";
dtColumnName = dv.ToTable();
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
con.Close();
List<string> listColumn = new List<string>();
foreach (DataRow row in dtColumnName.Rows)
{
listColumn.Add(row["Column_name"].ToString());
}
foreach (string prime in listColumn) // Loop through List with foreach
{
sw.Write(prime + '\t');
}
sw.WriteLine();
foreach (DataRow row in dtExcelRecords.Rows)
//for(int i=0 ;i<dtExcelRecords.Rows.Count;i++)
{
foreach (var item in row.ItemArray) // Loop over the items.
//for(int j=0;j<dtExcelRecords.Columns .Count ;j++)
{
//sw.Write(dtExcelRecords.Rows[i][j].ToString());
sw.Write(item); // Invokes ToString abstract method.
sw.Write('\t');
}
sw.WriteLine();
}
//Console.Write(getExcelSheetName);
sw.Close();
StreamReader sr = new StreamReader(@"c:\text.txt");
string[] columns;
string line;
while ((line = sr.ReadLine()) != null)
{
columns = line.Split('\t'); //using for single character as delimiter
//Regex.Split(line, "\t"); //using for string as delimiter
foreach (string part in columns)
{
Console.WriteLine(part);
}
}
Console.Read();
sr.Close();
}
}
}
No comments:
Post a Comment