C # Read the Csv file, Execl, and import the SQL Server database

Source: Internet
Author: User

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 ();
&

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.