C# 使用C将CSV导入DataGridView时更改分隔符

C# 使用C将CSV导入DataGridView时更改分隔符,c#,csv,datagridview,import,C#,Csv,Datagridview,Import,我正在试着换分隔符。 我们想要改变;对, 这是我们的进口代码: private void Import_Click(object sender, EventArgs e) { OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "Select file"; fdlg.InitialDirectory = @"c:\"; fdlg.FileName = txtFileName.Text; fdl

我正在试着换分隔符。 我们想要改变;对,

这是我们的进口代码:

private void Import_Click(object sender, EventArgs e)
{
    OpenFileDialog fdlg = new OpenFileDialog();

    fdlg.Title = "Select file";
    fdlg.InitialDirectory = @"c:\";
    fdlg.FileName = txtFileName.Text;
    fdlg.Filter = "Text and CSV Files(*.txt, *.csv)|*.txt;*.csv|Text Files(*.txt)|*.txt|CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";
    fdlg.FilterIndex = 1;
    fdlg.RestoreDirectory = true;
    if (fdlg.ShowDialog() == DialogResult.OK)
    {
        txtFileName.Text = fdlg.FileName;
        Import();
        Application.DoEvents();
    }
}
    public static DataTable GetDataTable(string strFileName)
        {   

            ADODB.Connection oConn = new ADODB.Connection();
            oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", "", "", 0); //We have tried to change this.
            string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
            ADODB.Recordset rs = new ADODB.Recordset();
            System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
            DataTable dt = new DataTable();
            rs.Open(strQuery, "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", //We have tried to change this.
            ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
            adapter.Fill(dt, rs);

            return dt;

}
private void Import()
{
if (txtFileName.Text.Trim() != string.Empty)
 {
try
{
    DataTable dt = GetDataTable(txtFileName.Text);
    dgvGv.DataSource = dt.DefaultView;
    dgvGv2.DataSource = dt.DefaultView;
    dgvGv3.DataSource = dt.DefaultView;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message.ToString());
}
该文件如下所示:

EAN       ;Product code ;Name
363738492 ;MT-01234     ;iphone case
153289234 ;MT-89854     ;samsung case
876253483 ;PO-43466     ;network cable
这是一个示例,而不是实际的文件

电流分离器为:; 但我们希望它是:

我们一直在查看此代码,但找不到分隔符。 我们也在谷歌上搜索了很多


如果您有任何问题,请随意评论

不确定为什么不使用,但在您的连接字符串中您已分隔\;您可以将其更改为,只需将delim连接的最后一部分更改为以下oConn.OpenProvider=Microsoft.Jet.OleDb.4.0;数据源=+System.IO.Path.GetDirectoryNamestrFileName+;扩展属性=\Text;HDR=是;FMT=分隔\@DJKRAZE首先,我是sjors的朋友,我们正在一起努力。我们已使用:FMT=分隔\;;;尝试了您的代码,这并没有给我们带来任何错误,但有一点是,实际的分隔符是:,因为如果我们用;没有work@DJKRAZE我们还想添加一个文本框,类似于FMT=Delimited\+txtdimiter.Text+;如果您在某个地方读到,可能使用64位给出错误。