• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[转]C#读取CSV,Excel,Txt文件,删除文件,拷贝文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

#region 读取csv文件
/// <summary>
/// 读取CVS文件
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="name">文件名称</param>
/// <returns>DataTable</returns>
public static DataTable ReadCVS(string filepath, string filename)
{
//string cvsDir = filepath;//要读取的CVS路径
DataTable dt = new DataTable();
if (filename.Trim().ToUpper().EndsWith("CSV"))//判断所要读取的扩展名
{
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ filepath + ";Extended Properties='text;HDR=NO;FMT=Delimited'";//有列的读取
string commandText = "select * from [" + filename + "]";//SQL语句

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 读取xls文件
/// <summary>
/// 读取Excel文件
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="filename">文件名称</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 读取txt文件
/// <summary>
/// 读取Txt文本文件
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="filename">文件名称</param>
/// <returns>文本信息</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

#region 文件删除
/// <summary>
/// 删除文件操作
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="fileName">文件名称</param>
public static void DeleteFile(string filePath, string fileName)
{
string destinationFile = filePath + fileName;
//如果文件存在,删除文件
if (File.Exists(destinationFile))
{
FileInfo fi = new FileInfo(destinationFile);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
fi.Attributes = FileAttributes.Normal;

File.Delete(destinationFile);
}
}
#endregion

/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="fromFilePath">文件的路径</param>
/// <param name="toFilePath">文件要拷贝到的路径</param>
private bool CopyFile(string fromFilePath, string toFilePath)
{
try
{
if (File.Exists(fromFilePath))
{
if (File.Exists(toFilePath))
{
File.Delete(toFilePath);
}
File.Move(fromFilePath, toFilePath);
return true;
}
return false;
}
catch 
{
return false;
}
}

#region 读取csv文件
/// <summary>
/// 读取CVS文件
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="name">文件名称</param>
/// <returns>DataTable</returns>
public static DataTable ReadCVS(string filepath, string filename)
{
//string cvsDir = filepath;//要读取的CVS路径
DataTable dt = new DataTable();
if (filename.Trim().ToUpper().EndsWith("CSV"))//判断所要读取的扩展名
{
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ filepath + ";Extended Properties='text;HDR=NO;FMT=Delimited'";//有列的读取
string commandText = "select * from [" + filename + "]";//SQL语句

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 读取xls文件
/// <summary>
/// 读取Excel文件
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="filename">文件名称</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 读取txt文件
/// <summary>
/// 读取Txt文本文件
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="filename">文件名称</param>
/// <returns>文本信息</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

#region 文件删除
/// <summary>
/// 删除文件操作
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="fileName">文件名称</param>
public static void DeleteFile(string filePath, string fileName)
{
string destinationFile = filePath + fileName;
//如果文件存在,删除文件
if (File.Exists(destinationFile))
{
FileInfo fi = new FileInfo(destinationFile);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
fi.Attributes = FileAttributes.Normal;

File.Delete(destinationFile);
}
}
#endregion

/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="fromFilePath">文件的路径</param>
/// <param name="toFilePath">文件要拷贝到的路径</param>
private bool CopyFile(string fromFilePath, string toFilePath)
{
try
{
if (File.Exists(fromFilePath))
{
if (File.Exists(toFilePath))
{
File.Delete(toFilePath);
}
File.Move(fromFilePath, toFilePath);
return true;
}
return false;
}
catch 
{
return false;
}
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何使用C#调用非托管DLL函数发布时间:2022-07-13
下一篇:
WMI in C#[强类型操作]发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap