OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "导出Excel (*.xls)|*.xls";
if (openFile.ShowDialog() != DialogResult.OK)
{
return;
}
string TSql = "SELECT * FROM [sheet1$]"; //这里固定写死了,如果用户改变了sheet1名字就找不到
DataTable dt1 = ExcelToDataSet(openFile.FileName, TSql);
/// <summary>
/// 返回Excel数据源
/// </summary>
/// <param name="filename">文件路径</param>
/// <param name="TSql">TSql</param>
/// <returns>DataSet</returns>
public static DataTable ExcelToDataSet(string filename, string TSql)
{ string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = TSql;
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
DataTable dt = new DataTable();
myCommand.Fill(dt);
myConn.Close();
return dt;
}
string TSql = "SELECT * FROM [sheet1$]"; //这里固定写死了,如果用户改变了sheet1名字就找不到、
现在想问下,如果得到Excel的表名,自动读取对应的数据
|
|