给你一点代码:这里用打开文件的方式进行的,看看也许有帮助!!
string openfileName=null;
OpenFileDialog openfd = new OpenFileDialog();
openfd.Filter = "All Excel file (*.xls)|*.xls|All files (*.*)|*.*";
if (openfd.ShowDialog() == DialogResult.OK)
{
openfileName = openfd.FileName;
OpenFileName = openfileName;
}
else
{
MessageBox.Show("Please select a data file to import...", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
////////////////////////////////////////////////////////////
///////////////////得到文件名以后
////////////////////////////////////////////////////////////
string excelfilename = openfileName;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0';Data Source=" + excelfilename;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
DataTable tables = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for (int i = 0; i < tables.Rows.Count; i++)
{
listBox_sheet_Name.Items.Add(tables.Rows[i]["Table_Name"].ToString());
}
}
conn.Close();
conn.Dispose();