/// <summary>
/// 传入Excel路径,然后把Excel文件内容转换为dataset
/// </summary>
/// <param name="Path">string Path</param>
/// <returns>DataSet</returns>
public DataSet ExcelToDS(string Path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Path + ";" +
"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcelPSID = "";
string strExcelBox = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcelPSID="select * from [Sheet1$]"; //Excel表Sheet1
myCommand = new OleDbDataAdapter(strExcelPSID, strConn);
ds = new DataSet();
myCommand.Fill(ds,"Sheet1");
strExcelBox="select * from [Sheet2$]"; //Excel表Sheet1
myCommand = new OleDbDataAdapter(strExcelBox, strConn);
conn.Close();
myCommand.Fill(ds,"Sheet2");
return ds;
}
本文介绍了一种通过指定路径将Excel文件内容转换为DataSet的方法。该方法使用Microsoft.Jet.OLEDB.4.0提供者连接到Excel文件,并通过OleDbDataAdapter填充DataSet。文章详细展示了如何读取不同工作表的内容。
1166

被折叠的 条评论
为什么被折叠?



