C # Reading CSV, Excel, and TXT files, deleting files, and copying files

Source: Internet
Author: User

# Region reading CSV files

/// <Summary>

 

/// Read the CVS file

 

/// </Summary>

 

/// <Param name = "path"> file path </param>

 

/// <Param name = "name"> file name </param>

 

/// <Returns> datatable </returns>

 

Public static datatable readcvs (string filepath, string filename)

 

{

 

// String cvsdir = filepath; // CVS path to be read

 

Datatable dt = new datatable ();

 

If (filename. Trim (). toupper (). endswith ("CSV") // determine the extension to be read

 

{

 

String connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source ="

 

+ Filepath + "; extended properties = 'text; HDR = no; FMt = delimited'"; // read with columns

 

String commandtext = "select * from [" + filename + "]"; // SQL statement
Oledbconnection olconn = new oledbconnection (connstr );

 

Olconn. open ();

 

Oledbdataadapter ODP = new oledbdataadapter (commandtext, olconn );

 

ODP. Fill (DT );

 

Olconn. Close ();

 

ODP. Dispose ();

 

Olconn. Dispose ();

 

}

 

Return DT;

 

}

 

# Endregion

 

# Region reading Excel files

 

/// <Summary>

 

/// Read the Excel file

 

/// </Summary>

 

/// <Param name = "filepath"> file path </param>

 

/// <Param name = "FILENAME"> file name </param>

 

/// <Returns> datatable </returns>

 

Public static datatable readexcel (string filepath, string filename)

 

{

 

System. Data. dataset itemds = new dataset ();

 

If (filename. Trim (). toupper (). endswith ("xls") | filename. Trim (). toupper (). endswith ("XLSX "))

 

{

 

String connstr = "provider = Microsoft. Ace. oledb.12.0; Data Source =" + filepath + filename + "; extended properties =/" Excel 12.0; HDR = no ;/"";

 

System. Data. oledb. oledbconnection conn = NULL;

 

System. Data. oledb. oledbcommand oledbcommd = NULL;

 

Try

 

{

 

Conn = new system. Data. oledb. oledbconnection ();

 

Conn. connectionstring = connstr;

 

Conn. open ();

 

Datatable dt = conn. getoledbschematable (oledbschemaguid. Tables, null );

 

// Determine the name of the sheet connecting to the Excel file

 

For (INT I = 0; I <DT. Rows. Count; I ++)

 

{

 

Datarow DR = DT. Rows [I];

 

String sqltext = "select * from [" + Dr ["table_name"] + "]";
Oledbcommd = new system. Data. oledb. oledbcommand (sqltext, Conn );

 

Ole dbcommd. commandtimeout = 100000;

 

// Execute

 

System. Data. oledb. oledbdataadapter oledbda = new system. Data. oledb. oledbdataadapter (oledbcommd );
Oledbda. Fill (itemds );

 

}

 

}

 

Catch

 

{}

 

Finally

 

{

 

// Release

 

Oledbcommd. Dispose ();

 

Conn. Close ();

 

}

 

// Create a connection
}

 

Return itemds. Tables [0];

 

}

 

# Endregion

 

# Region read TXT files
/// <Summary>
/// Read the TXT text file
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "FILENAME"> file name </param>
/// <Returns> Text Information </returns>
Public static string readtxt (string filepath, string filename)
{
Stringbuilder sb = new stringbuilder ("");
// Streamreader sr = new streamreader (filepath + filename );;
Streamreader sr = new streamreader (filepath + filename, encoding. getencoding ("gb2312 "));
String line;
While (line = Sr. Readline ())! = NULL)
{
SB. appendline (line );
}
Sr. Close ();
Sr. Dispose ();
Return sb. tostring ();
}
# Endregion

 

# Endregion

 

# Delete a region File
/// <Summary>
/// Delete a file
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "FILENAME"> file name </param>
Public static void deletefile (string filepath, string filename)
{
String destinationfile = filepath + filename;
// Delete an object if the object exists
If (file. exists (destinationfile ))
{
Fileinfo Fi = new fileinfo (destinationfile );
If (Fi. Attributes. tostring (). indexof ("readonly ")! =-1)
Fi. Attributes = fileattributes. normal;

 

File. Delete (destinationfile );
}
}
# Endregion

 

///


// copy a file
///
/// file path
// path to which the file is to be copied
private bool copyfile (string fromfilepath, string tofilepath)
{< br> try
{< br> If (file. exists (fromfilepath)
{< br> If (file. exists (tofilepath)
{< br> file. delete (tofilepath);
}< br> file. move (fromfilepath, tofilepath);
return true;
}< br> return false;
}< br> catch
{< br> return false;
}< BR >}

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.