C_读取CSV文件最好用的C_读取CSV文件最好用的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsApplication1
{
public partial clas...
C_读取CSV文件最好用的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsApplication1
{
public partial class CSVORXLS : Form
{
public CSVORXLS()
{
InitializeComponent();
string csvDir = @"C:?test1.xls";//要?取的CSV路径,
//string excelDir = @"C:?test1.xls";
if (csvDir.Trim().ToUpper().EndsWith("CSV"))//判断所要的?展名?型,
{
int ipos = csvDir.LastIndexOf("??");
string filePath = csvDir.Substring(0, ipos + 1);
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ filePath + ";Extended Properties='text;HDR=YES;FMT=Delimited;'";//有列?的
//string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" + filePath + ";Extended Properties='text;HDR=NO;FMT=Delimited;'";//没有
列?的
//HDR=No代
没有列名,如果?Yes?有列名,"IMEX=1;"告???器?是?混
合的数据,包括数字,日期,字符串等等,就像文本文档的,
//FMT=Delimited是否有分隔符如,|。
string commandText = "select * from " + csvDir.Replace(filePath,
"");//SQL?句,
OleDbConnection olconn = new OleDbConnection(connStr);
olconn.Open();
OleDbDataAdapter odp = new OleDbDataAdapter(commandText, olconn);
DataTable dt = new DataTable();
odp.Fill(dt);
dataGridView1.AutoGenerateColumns = true ;//有列?的
dataGridView1.DataSource = dt.DefaultView;//有列?的
//dataGridView2.DataSource = dt.DefaultView;//没有列?的
//dataGridView2.Columns[0].SortMode =
DataGridViewColumnSortMode.NotSortable;//没有列?的
//dataGridView2.Columns[0].HeaderText = "?号";//没有列?的
//dataGridView2.Columns[1].HeaderText = "高度";//没有列?的
//dataGridView2.Columns[2].HeaderText = "?度";//没有列?的
}
else
{
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvDir + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';";
//string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvDir + ";Extended Properties='text;HDR=YES;IMEX=1;'";//IMEX=1
string commandText = "select * from [test1$]";
OleDbConnection olconn = new OleDbConnection(connStr);
olconn.Open();
OleDbDataAdapter odp = new OleDbDataAdapter(commandText, olconn);
DataTable dt = new DataTable();
odp.Fill(dt);
dataGridView2.DataSource = dt.DefaultView;
}
}
}
}
本文档为【C_读取CSV文件最好用的】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。