如何将定界符从逗号更改为分号? [英] How do I change my delimiter from comma to a semicolon?
问题描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Reflection;
using CsvHelper.Configuration;
namespace ReadCsvFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public DataTable ReadCsv(string filename)
{
DataTable dt = new DataTable("Data");
using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
Path.GetDirectoryName(filename) + "\";Extended Properties='text;HDR=yes;FMT=Delimited(;)';"))
{
using (OleDbCommand cmd = new OleDbCommand(string.Format("select *from [{0}]", new FileInfo(filename).Name), cn))
{
cn.Open();
using (OleDbDataAdapter adapter = new OleDbDataAdapter(cmd))
{
adapter.Fill(dt);
}
}
}
return dt;
}
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV| *.csv", ValidateNames = true, Multiselect = true })
{
if (ofd.ShowDialog() == DialogResult.OK)
dataGridView.DataSource = ReadCsv(ofd.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
原来,我尝试读取的csv文件使用分号(;
)作为分隔符,而不是通常的逗号(,
).
我试图更改定界符,但由于某些原因无法更改.
我将不胜感激!
我会猜测您只需要更改以下内容即可:
using (OleDbConnection cn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
Path.GetDirectoryName(filename) +
"\";Extended Properties='text;HDR=yes;FMT=Delimited(;)';"))
对此:
using (OleDbConnection cn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
Path.GetDirectoryName(filename) +
"\";Extended Properties='text;HDR=yes;FMT=Delimited(,)',"))
将OleDb连接字符串中的FMT=Delimited(;)';
更改为FMT=Delimited(,)',
更新:抱歉,我误解了您的原始问题-您已经 在您的OleDbConnection
中将分隔符更改为;
-错过了那个.>
老实说:我不知道要获得此OleDbConnection
并命令正常工作的方式和方法(除了已经完成的操作).
我个人会使用出色的(免费的) CsvHelper 组件来读取任何CSV文件我可能需要导入的文件-不是OleDb的东西.....使用CsvHelper
,定义分隔符(以及导入的更多属性)非常容易,并且它就像一个符咒一样,从中返回List<YourEntity>
任何.csv
文件,随时可以使用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Reflection;
using CsvHelper.Configuration;
namespace ReadCsvFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public DataTable ReadCsv(string filename)
{
DataTable dt = new DataTable("Data");
using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
Path.GetDirectoryName(filename) + "\";Extended Properties='text;HDR=yes;FMT=Delimited(;)';"))
{
using (OleDbCommand cmd = new OleDbCommand(string.Format("select *from [{0}]", new FileInfo(filename).Name), cn))
{
cn.Open();
using (OleDbDataAdapter adapter = new OleDbDataAdapter(cmd))
{
adapter.Fill(dt);
}
}
}
return dt;
}
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV| *.csv", ValidateNames = true, Multiselect = true })
{
if (ofd.ShowDialog() == DialogResult.OK)
dataGridView.DataSource = ReadCsv(ofd.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
It turned out that the csv files I tried to read had semicolons (;
) as separators, instead of the usual commas (,
).
I tried to change the delimiter, but for some reason was not able to.
I would appreciate any help I could get!
I would guess that you just need to change this:
using (OleDbConnection cn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
Path.GetDirectoryName(filename) +
"\";Extended Properties='text;HDR=yes;FMT=Delimited(;)';"))
to this:
using (OleDbConnection cn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
Path.GetDirectoryName(filename) +
"\";Extended Properties='text;HDR=yes;FMT=Delimited(,)',"))
Change the FMT=Delimited(;)';
in the OleDb connection string to be FMT=Delimited(,)',
UPDATE: Sorry, I misunderstood your original question - you've already changed the delimiter to ;
in your OleDbConnection
- missed that one.
Quite honestly: I have no idea how and what you should do (in addition to what you've already done) to get this OleDbConnection
and command to work properly...
Personally, I would use the excellent (and free!) CsvHelper component to read any CSV files I might need to import - not that OleDb stuff..... with CsvHelper
, it's very easy to define delimiters (and many more properties of your import), and it works like a charm, returning a List<YourEntity>
from any .csv
file, ready to be used whichever way you want
这篇关于如何将定界符从逗号更改为分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!