c++ 读取csv文件

#include "stdafx.h"

#include <iostream> 

#include <string> 

#include<fstream> 

 

using namespace std;

 

int main(int argc, char* argv[])

{

       ifstream file ( "D:\\test.csv" ); // declare file stream: 					http://www.cplusplus.com/reference/iostream/ifstream/

  string value;

  while ( file.good() )

  {

     getline ( file, value, ',' ); // read a string until next comma: 		http://www.cplusplus.com/reference/string/getline/

     cout << string( value, 0, value.length() )<<","; // display value removing the first and the last character from it

 

  }

 

}

读取CSV文件C#

C# 读取CSV文件2009年06月25日 星期四 19:03方法一,纯文本方法,即把该文件当做文本文件读取

 

int intColCount = 0; 

bool blnFlag = true; 

DataTable mydt = new DataTable("myTableName");

 

DataColumn mydc; 

DataRow mydr;

 

string strpath = ""; //cvs文件路径

string strline; 

string [] aryline;

 

System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);

 

while((strline = mysr.ReadLine()) != null) 

{ 

aryline = strline.Split(new char[]{','});

 

if (blnFlag) 

{ 

blnFlag = false; 

intColCount = aryline.Length; 

for (int i = 0; i < aryline.Length; i++) 

{ 

mydc = new DataColumn(aryline[i]); 

mydt.Columns.Add(mydc); 

} 

}

 

mydr = mydt.NewRow(); 

for (int i = 0; i < intColCount; i++) 

{ 

mydr[i] = aryline[i]; 

} 

mydt.Rows.Add(mydr); 

}

  mydt.Rows.RemoveAt(0);

            dataGridView1.DataSource = mydt.DefaultView;

            dataGridView1.Columns[0].HeaderText = "编号";

 

 

方法二、当做一个数据源读取,常用的sql语句都能执行的

 

using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\csv\;Extended Properties='Text;'"))   

{   

    DataTable dtTable = new DataTable();   

 

    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Test.csv]", conn);   

    try 

    {   

        adapter.Fill(dtTable);   

    }   

       

    catch (Exception ex)   

    {   

        dtTable = new DataTable();   

    }   

    this.GridView1.DataSource = dtTable;   

    this.GridView1.DataBind();   

} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值