I have an openFileDialog tool. I will choose a excel file from my computer and my programme read a column (for example A column) and write my listbox on GUI. How can i do it via OleDB? I am new at C#. If you explain detailed, I will be happy for that. Thank you for your help.
In order to use the OLEDB provider successfully we have to consider a few points.
The OLEDB provider for Excel 2003 files is different from the one used for Excel 2007/2010 files. So, the first thing we have to do is determining the Excel file format in order to select the correct provider. In the code example below I simply check the extension of the file to determine the Excel file format. Please note, that there are more elaborated methods to determine the Excel file format (e.g. via the magic bytes).
To select all rows of a Excel sheet we need to know the name of the Excel sheet. The standard sheet names are language dependent and could be renamed by the user. So, we need a way to determine the name of the sheets included in a Excel file to be language independent (and of course independent of renamed sheets). Fortunately, the
OleDbConnection
class provides a method calledGetOleDbSchemaTable
which allows us to get all the sheet names in an Excel file.The OLEDB provider for Excel supports an extended property called HDR. Setting
HDR
toYes
means that the first row of a sheet contains the column titles. So, if you use column titles you should setHDR=Yes
.
So, to summarize the code sample below does the following (on button click):
- Determines the Excel file type based on the file extension.
- Selects the correct OLEDB provider based on the excel file type to build the connection string.
- Determines the sheet names included in the Excel file.
- Selects the first sheet, selects all rows and stores the rows in a DataTable called
mytable
. - Displays all values of the first column in a listbox called
listbox1
.
Code sample:
private static bool IsExcelXmlFileFormat(string fileName)
{
return fileName.EndsWith("xlsx", StringComparison.OrdinalIgnoreCase);
}
private void button1_Click(object sender, EventArgs e)
{
// Open your FileOpenDialog and let the user select a file...
string fileName = "c:\\temp\\myexcelfile.xlsx";
OleDbConnectionStringBuilder connStringBuilder =
new OleDbConnectionStringBuilder();
connStringBuilder.DataSource = fileName;
if (IsExcelXmlFileFormat(fileName))
{
// Set HDR=Yes if first row contains column titles.
connStringBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
connStringBuilder.Add("Extended Properties", "Excel 8.0;HDR=NO;");
}
else
{
connStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
connStringBuilder.Add("Extended Properties", "Excel 8.0;");
}
DataSet data = new DataSet();
using (OleDbConnection dbConn = new OleDbConnection(connStringBuilder.ConnectionString))
{
dbConn.Open();
DataTable sheets = dbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
using (OleDbCommand selectCmd = new OleDbCommand(
String.Format("SELECT * FROM [{0}]", sheets.Rows[0]["TABLE_NAME"]), dbConn))
{
using (OleDbDataAdapter dbAdapter = new OleDbDataAdapter())
{
dbAdapter.SelectCommand = selectCmd;
dbAdapter.Fill(data, "mytable");
}
}
}
// To enumerate all rows use the following code.
// foreach (DataRow row in data.Tables["mytable"].Rows)
// {
// Console.Out.WriteLine(row[0]);
// }
// Display the values of column 0 in a listbox called listBox1.
listBox1.ValueMember = data.Tables["mytable"].Columns[0].ColumnName;
listBox1.DisplayMember = data.Tables["mytable"].Columns[0].ColumnName;
listBox1.DataSource = data.Tables["mytable"];
}
来源:https://stackoverflow.com/questions/10258141/how-to-import-data-from-one-column-of-excel-to-listbox-using-c-sharp