惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Latest news
Latest news
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
V
V2EX
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
小众软件
小众软件
A
Arctic Wolf
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
G
GRAHAM CLULEY
罗磊的独立博客
T
Tor Project blog
C
Cisco Blogs
美团技术团队
博客园 - Franky
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
Cyberwarzone
Cyberwarzone
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Security Latest
Security Latest
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
Spread Privacy
Spread Privacy
J
Java Code Geeks
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Project Zero
Project Zero

博客园 - 古道飘零客

Easyui combogrid添加toolbar SQL导出到Excel 存储过程 [WPF]DataGrid行前景色根据单元格编辑值变色 [转]系统性能调优吐血总结分享 [转]C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件.... JS四舍五入BUG解决 IIRF(Ionics Isapi Rewrite Filt er)实现在IIS 5/6上重写Url Repeater控件实现左侧快捷菜单 - 古道飘零客 - 博客园 JS和CS互访(非常经典) [转]IList及泛型集合类转换DataTable - 古道飘零客 - 博客园 关于sqlserver2005中的bit数据类型 C#小写转大写 - 古道飘零客 - 博客园 URL、Session、Cookies、Server.Transfer、Application和跨页面传送详解 sql server 2005 安装提示COM+ 和性能监视器计数器要求错误 oracle数据分组排名次 [转cnblogs]网页静态 js如何判断小数点后有几位? - 古道飘零客 - 博客园 怎样读取本地Excel数据,并保存到时服务器 - 古道飘零客 - 博客园 C#项目打包,并自动安装SQL数据库 - 古道飘零客 - 博客园
[转]C#读取CSV,Excel,Txt文件,删除文件,拷贝文件
古道飘零客 · 2011-04-22 · via 博客园 - 古道飘零客

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