原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

    using LumenWorks.Framework.IO.Csv;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks; /// <summary>
/// 获取路径下的.csv文件
/// </summary>
/// <param name="FilePath"></param>
/// <returns></returns>
public static DataTable GetStream(string FilePath)
{
FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
Stream stream = new MemoryStream(bytes);
return GetData(stream);
} /// <summary>
/// 将.csv文件转换成DataTable
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
private static DataTable GetData(Stream stream)
{
using (stream)
{
using (StreamReader input = new StreamReader(stream, Encoding.GetEncoding("GBK")))
{
using (CsvReader csv = new CsvReader(input, true))
{
DataTable dt = new DataTable();
//第一行字段数量
int columnCount = csv.FieldCount;
//标题数组
string[] headers = csv.GetFieldHeaders();
//循环添加标题行
for (int i = 0; i < columnCount; i++)
{
dt.Columns.Add(headers[i]);
}
//循环添加列数据
while (csv.ReadNextRecord())
{
DataRow dr = dt.NewRow();
for (int i = 0; i < columnCount; i++)
{
if (!string.IsNullOrWhiteSpace(csv[i]))
{
dr[i] = csv[i];
}
}
dt.Rows.Add(dr);
}
return dt;
}
}
}
}

 

我只是参考原文里面的实例,根据我项目的需求换了一种写法,换汤不换药(先看明白简单的CsvReader对.csv标题和列的操作然后根据自身的需求去改写,主要改也是简单的循环或者根据解析格式不同换一种格式其它的看需求调整。),希望对大家有所帮助,共同学习!

C#:CsvReader读取.CSV文件(转换成DataTable)的更多相关文章

  1. C# CSV 文件转换成DataTable

    { DataTable dt = new DataTable(); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess ...

  2. C#:CsvReader读取.CSV文件并转换成DataTable

    原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv; ...

  3. xls/csv文件转换成dbf文件

    转至:https://blog.csdn.net/linhai1028/article/details/80211252 编写的一个小脚本,主要是利用python中的pandas,xlrd,dbfpy ...

  4. python csv文件转换成xml, 构建新xml文件

    csv文件 code from xml.etree.ElementTree import Element,ElementTree,tostring import json,csv def csvtox ...

  5. Java读取Excel文件转换成JSON并转成List——(七)

    Jar包

  6. C#:将.csv格式文件转换成.xlsx格式文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

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

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

  8. 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法

    1. 转换成CSV文件: http://www.dotnetgallery.com/lab/resource93-Export-to-CSV-file-from-Data-Table-in-Aspne ...

  9. python读取csv文件、excel文件并封装成dict类型的list,直接看代码

    # coding=UTF-8import csvimport xlrd class ReaderFile(): """ 读取csv文件 filePath:文件路径 &qu ...

随机推荐

  1. HDU 2175 汉诺塔IX (递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2175 1,2,...,n表示n个盘子.数字大盘子就大.n个盘子放在第1根柱子上.大盘不能放在小盘上.  ...

  2. 爬虫学习06用selenium爬取空间

    用selenium爬取空间 from selenium import webdriver from lxml import etree import time pro = webdriver.Chro ...

  3. 树莓派安装cobbler,自动化安装CentOS

    安装python.相关python模块.apache sudo apt-get install python python2.7 python-django python-netaddr python ...

  4. glibc 2.x release note

    glibc 2.x release note,参见: https://sourceware.org/glibc/wiki/Glibc%20Timeline https://www.gnu.org/so ...

  5. 我是这样做APP的:击中用户的痛点(转)

    击中用户的痛点 点评,感觉取名叫做“用户痛点的取舍”更加合适.很多公司.项目的失败完全取决于决策人取舍的失败,一味地追求大而全.迎合上级领导,专断而没有和团队做客观的分析.本文虽然以一个应该来说并不复 ...

  6. Linear Regression with PyTorch

    Linear Regression with PyTorch Problem Description 初始化一组数据 \((x,y)\),使其满足这样的线性关系 \(y = w x + b\) .然后 ...

  7. Springbooot +Mybaties 配置数据库多数据源

    前言 在实际项目中,我们可能会碰到在一个项目中会访问多个数据库的情况.针对这种情况,我们就需要配置动态的数据源了.一般按照以下步骤即可 一.在启动类上添加注解 二.在application.prope ...

  8. alloc_skb申请函数分析

    alloc_skb()用于分配缓冲区的函数.由于"数据缓冲区"和"缓冲区的描述结构"(sk_buff结构)是两种不同的实体,这就意味着,在分配一个缓冲区时,需要 ...

  9. 正则表达式验证HTTP地址是否合法

    转载:https://blog.csdn.net/fsdad/article/details/52637426 判断url是否合法 const std::regex urlpattern(" ...

  10. C# 文件与二进制之间的转换

    /// <summary> /// 工具类:文件与二进制流间的转换 /// </summary> public class FileBinaryConvertHelper { ...