大家好,我又一个叫做2009.3.xlsx的文件,里面有几个表 private DataSet LoadDataSetFromXLS(String strXLSPath)
{
DataSet ds = null;
if (File.Exists(strXLSPath))
{
try
{
String strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source="
+ strXLSPath + ";" + "Extended Properties='Excel 12.0;HDR=YES\'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open(); OleDbDataAdapter myCommand = null;
String strExcel = "select * from [卖出记录$]";
myCommand = new OleDbDataAdapter(strExcel, conn);
ds = new DataSet();
myCommand.Fill(ds); conn.Close();
conn = null;
}
catch (System.Exception e)
{
MessageBox.Show(e.Message.ToString());
return null;
}
}
return ds;
}我上面的代码只能读出一个表,如果我想把那个文件的表全部读到dataset,我应该向OleDbDataAdapter传送什么指令?
{
DataSet ds = null;
if (File.Exists(strXLSPath))
{
try
{
String strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source="
+ strXLSPath + ";" + "Extended Properties='Excel 12.0;HDR=YES\'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open(); OleDbDataAdapter myCommand = null;
String strExcel = "select * from [卖出记录$]";
myCommand = new OleDbDataAdapter(strExcel, conn);
ds = new DataSet();
myCommand.Fill(ds); conn.Close();
conn = null;
}
catch (System.Exception e)
{
MessageBox.Show(e.Message.ToString());
return null;
}
}
return ds;
}我上面的代码只能读出一个表,如果我想把那个文件的表全部读到dataset,我应该向OleDbDataAdapter传送什么指令?
for (int i = 0; i != _Table.Rows.Count; i++)
{
MessageBox.Show(_Table.Rows[i]["Table_Name"].ToString());
}
public static DataTable GetExcelTableName(string p_ExcelFile)
{
try
{
if (System.IO.File.Exists(p_ExcelFile))
{
OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + p_ExcelFile);
_ExcelConn.Open();
DataTable _Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
_ExcelConn.Close();
return _Table;
}
return null;
}
catch
{
return null;
}
}
谢谢你的回答,你这个方法的确可以得到文件中所有表的名字,但是我是希望把所有的表的内容都放到DataSet中。请问有没有比较快捷的办法?