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 XLS 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)
{
Datatable dt = new datatable ();
If (filename. Trim (). toupper (). endswith ("xls "))
{
String connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source ="
+ Filepath + "; extended properties = 'excel 8.0; HDR = no; IMEX = 1 '";
String commandtext = "select * from [" + filename + "]";

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 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 >}

# 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 XLS 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)
{
Datatable dt = new datatable ();
If (filename. Trim (). toupper (). endswith ("xls "))
{
String connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source ="
+ Filepath + "; extended properties = 'excel 8.0; HDR = no; IMEX = 1 '";
String commandtext = "select * from [" + filename + "]";

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 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 >}

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.