无法使用OLEDB打开CSV文件

时间:2019-04-28 20:10:08

标签: c#

我创建了一个可以打开excel文件的程序,但是我使用的代码无法打开CSV文件。 完整的代码是:

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm; *.csv";

if (ofd.ShowDialog() == DialogResult.OK){
    txtPath.Text = ofd.FileName;
}

string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + txtPath.Text + "';Extended Properties= \"Excel 12.0; HDR=YES;\";";
OleDbConnection con = new OleDbConnection(constr);
con.Open();

cmbSheet.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
cmbSheet.DisplayMember = "TABLE_NAME";
cmbSheet.ValueMember = "TABLE_NAME";

OleDbDataAdapter sda = new OleDbDataAdapter("select * from [" + cmbSheet.SelectedValue + "]", con);

DataTable dt = new DataTable();
sda.Fill(dt);

foreach (DataRow row in dt.Rows){
    dgv.DataSource = dt;
}

0 个答案:

没有答案