ReadExcelFiles
// Read the source code of the file;
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Data. OleDb;
Using System. Data;
Namespace CC
{
Public class ReadExcel
{
Private string FileName;
Private string FilsPath;
/// <Summary>
/// Path of the constructor File
/// </Summary>
/// <Param name = "Path"> file Path (Execl file is the complete Path) </param>
Public ReadExcel (string Path) // Structure
{
FileName = Path;
// FilsPath = Files;
}
/// <Summary>
/// Constructor: Path of the file
/// </Summary>
/// <Param name = "Path"> file Path (Path of the folder where the Csv file is located) </param>
/// <Param name = "Files"> file name when reading a Csv file </param>
Public ReadExcel (string Path, string Files) // Structure
{
FileName = Path;
FilsPath = Files;
}
OleDbConnection OleCon = new OleDbConnection ();
OleDbCommand OleCmd = new OleDbCommand ();
OleDbDataAdapter OleDa = new OleDbDataAdapter ();
# Region reads the Execl file and returns DataSet
/// <Summary>
/// Read the Execl file and return the DataSet
/// </Summary>
/// <Returns> return result set </returns>
Public DataSet Execl ()
{
DataSet Myds = new DataSet ();
OleCon. ConnectionString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + FileName + "; Extended Properties = Excel 8.0 ;";
OleCon. Open ();
OleCmd. CommandText = "select * from [sheet1 $]";
OleCmd. Connection = OleCon;
OleDa. SelectCommand = OleCmd;
Try
{
OleDa. Fill (Myds, "Execl ");
Return Myds;
}
Catch
{
Return Myds;
}
Finally
{
OleCon. Close ();
OleCmd. Dispose ();
OleDa. Dispose ();
OleCon. Dispose ();
}
}
# Endregion
# Region reads Csv files and returns DataSet
/// <Summary>
/// Read the Csv file and return DataSet
/// </Summary>
/// <Returns> Csv content </returns>
Public DataSet Csv ()
{
DataSet CsvData = new DataSet ();
OleCon. ConnectionString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + FileName + "; Extended Properties = Text; FMT = Delimited; HDR = YES ;";
OleCon. Open ();
OleCmd. Connection = OleCon;
OleCmd. CommandText = "select * From" + FilsPath;
OleDa. SelectCommand = OleCmd;
Try
{
OleDa. Fill (CsvData, "Csv ");
Return CsvData;
}
Catch
{
Return CsvData;
}
Finally
{
OleCon. Close ();
OleCmd. Dispose ();
OleDa. Dispose ();
&