我创建了一个可以打开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;
}