[英]Importing excel file to datagridview using openFileDialog
I have a winform with a load button. 我有一个带有加载按钮的winform。 When I click the load, I pick a file I want to load into the datagridview.
单击加载时,我选择一个要加载到datagridview中的文件。 When I click the file, the right number of rows seem to populate , but no content shows.
当我单击该文件时,似乎填充了正确的行数,但未显示任何内容。 I'm not sure what I'm missing.
我不确定我缺少什么。 "Acronyms" is the name of my excel tab/sheet.
“首字母缩写”是我的excel标签/工作表的名称。
private void btnImport_Click_1(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XLS files (*.xls, *.xlt)|*.xls;*.xlt|XLSX files (*.xlsx, *.xlsm, *.xltx, *.xltm)|*.xlsx;*.xlsm;*.xltx;*.xltm|ODS files (*.ods, *.ots)|*.ods;*.ots|CSV files (*.csv, *.tsv)|*.csv;*.tsv|HTML files (*.html, *.htm)|*.html;*.htm";
openFileDialog.FilterIndex = 2;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet DtSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog.FileName + "';Extended Properties=Excel 8.0;");
MyConnection.Open();
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Acronyms$]", MyConnection);
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
dgvExcelList.DataSource = DtSet.Tables[0];
MyConnection.Close();
}
}
检查以确保您通过GUI为DataGridView创建的列已填充DataProperty值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.