using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SanJuSFTP
{
public class Common
{ public static DataTable CsvToDt(string path)
{
OleDbConnection OleCon = new OleDbConnection();
try
{
DataSet dsCsvData = new DataSet();
OleDbCommand OleCmd = new OleDbCommand();
OleDbDataAdapter OleDa = new OleDbDataAdapter(); OleCon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path.Substring(0, path.LastIndexOf("\\")) + ";Extended Properties='Text;FMT=Delimited(,);HDR=YES;IMEX=1';"; OleCon.Open(); OleCmd.Connection = OleCon;
OleCmd.CommandText = "select * from [" + path.Substring(path.LastIndexOf("\\") + 1) + "] where 1=1";
OleDa.SelectCommand = OleCmd;
OleDa.Fill(dsCsvData, "Csv");
return dsCsvData.Tables[0];
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message);
return null;
}
finally
{
OleCon.Close();
}
} /// <summary>
/// datatable转csv
/// </summary>
/// <param name="dt"></param>
/// <param name="fileName"></param>
public static void DtToCsv(DataTable dt, string fileName)
{
FileStream fs = new FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
try
{
string data = ""; //写出列名称
for (int i = 0; i < dt.Columns.Count; i++)
{
data += dt.Columns[i].ColumnName.ToString();
if (i < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data); //写出各行数据
for (int i = 0; i < dt.Rows.Count; i++)
{
data = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
data += "" + dt.Rows[i][j].ToString();
if (j < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data);
}
}
catch (Exception ex)
{
}
finally
{
sw.Close();
fs.Close();
} } /// <summary>
/// 新文件名
/// </summary>
/// <param name="oldFilePath">文件路径</param>
/// <param name="oldFileName">文件名称</param>
/// <returns></returns>
public static string GetNewFilePath(string oldFilePath, string oldFileName)
{
string newFilePath = oldFilePath + oldFileName.Substring(0, oldFileName.IndexOf("."))
+ DateTime.UtcNow.ToString("yyyyMMdd") +
oldFileName.Substring(oldFileName.IndexOf("."));
return newFilePath;
} /// <summary>
/// 修改本地文件名
/// </summary>
/// <param name="oldStr">旧文件名</param>
/// <param name="newStr">新文件名</param>
public static void Rename_Local(string oldStr, string newStr)
{
// 改名方法
FileInfo fi = new FileInfo(oldStr);
FileInfo fi_new = new FileInfo(newStr);
if (fi_new.Exists)
{
fi_new.Delete();
}
fi.MoveTo(Path.Combine(newStr));
}
}
}

读取CSV到DataTable的更多相关文章

  1. C#:StreamReader读取.CSV文件(转换成DataTable)

    using System.Data; using System.IO; /// <summary> /// Stream读取.csv文件 /// </summary> /// ...

  2. C# 读取 CSV 文件

    最近做一个C#项目要导入CSV文件中的数据到Oracle中,使用Aspose.Cells读取中文字段标题却乱码,表的最后多出几行null记录,而且不是免费的,后来找到了NPOI,顾名思义,就是POI的 ...

  3. c# winForm使用Aspose.Cells读取CSV文件中文乱码问题

    不废话直接上代码 主要注意是 红色代码部分 Aspose.Cells.TxtLoadOptions lo = new TxtLoadOptions();                      lo ...

  4. C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库

    第一步:首先需要将csv先装换成datatable,这样我们就容易进行对datatable进行遍历: /// 将CSV文件的数据读取到DataTable中 /// CSV文件路径 /// 返回读取了C ...

  5. EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类

    大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...

  6. asp.net读取CSV

    原文:asp.net读取CSV 用Excel导了两天数据,各种问题,折磨客户也折磨了自己,以前没发现的问题一下子都暴露出来了 特意收集两篇Excel跟CSV读取相关的两篇文章 asp.net读取exc ...

  7. C#读取CSV

    public class CSVFileHelper { /// <summary> /// 将DataTable中数据写入到CSV文件中 /// </summary> /// ...

  8. C# 读取CSV和EXCEL文件示例

    我们习惯了直接连到数据库上面读取数据表的数据内容: 如果有一天我们需要读取CSV,EXCEL文件的内容的时候,可不可以也像读数据表的方式一样呢?当然可以,使用OleDB ADO.NET是很简单的事情 ...

  9. C#完美读取CSV

    /// <summary>         /// 将DataTable中数据写入到CSV文件中         /// </summary>         /// < ...

随机推荐

  1. JVM加载类冲突,导致Mybatis查数据库返回NULL的一个小问题

    今天碰到个bug,虽然小,但是有点意思 背景是SpringMVC + Mybatis的一个项目,mapper文件里写了一条sql 大概相当于 select a from tableA where b ...

  2. Python学习第十篇——函数初步

    def make_album(name,album_name,song_nums = 1): dict_album = {name:[album_name]} if int(song_nums) &g ...

  3. Codeforces Round #534 (Div. 2)D. Game with modulo-1104-D(交互+二分+构造)

    D. Game with modulo time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Echarts x轴文本内容太长的几种解决方案

    Echarts 标签中文本内容太长的时候怎么办 ? - 1对文本进行倾斜 在xAxis.axisLabe中修改rotate的值 xAxis: { data: ["衬衫11111", ...

  5. iOS数据存储-钥匙串存储

    2017.11.20 14:41* 字数 227 阅读 678评论 0喜欢 0 钥匙串介绍   1. 表示设备唯一号的标识,在IOS7中要么被禁止使用,要么重新安装程序后两次获取的标识符不一样. 2. ...

  6. Java参数是值传递还是引用传递?

    先来看看参数是如何传递的. 一.参数的传递 1.基本类型的参数传递 public static void main(String[] args) { int a = 1; fun(a); } priv ...

  7. KubeCon CloudNativeCon China 2019

    KubeCon CloudNativeCon China 2019 - LF Asia, LLChttps://events.linuxfoundation.cn/events/kubecon-clo ...

  8. 关于spring的源码的理解

    从最基础的Hello World开始. spring的Hello World就三行代码: public void test() { ApplicationContext context = new C ...

  9. mysql sql执行计划

    查看Mysql执行计划 使用navicat查看mysql执行计划: 打开profile分析工具: 查看是否生效:show variable like ‘%profil%’; 查看进程:show pro ...

  10. [转帖]HTTP 头部解释

    HTTP 头部解释 https://www.cnblogs.com/poissonnotes/p/4844014.html 之前看的太粗了 同事闻起来 referer 才知道自己所知甚少.. ==== ...