C# 读取Excel文件里面的内容到DataSet

摘要:读取Excel文件里面的内容到DataSet

代码:

 /// <summary>
/// 读取Excel文件里面的内容到DataSet
/// </summary>
/// <param name="Path"></param>
/// <param name="ds"></param>
private void ExcelToDS(string Path,ref DataSet ds)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open(); //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //包含excel中表名的字符串数组
string[] strTableNames = new string[dtSheetName.Rows.Count];
for (int k = ; k < dtSheetName.Rows.Count; k++)
{
strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
} string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [" + strTableNames[] + "]";//strTableNames[0]取得第一个Sheet中的数据
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds);
conn.Close();
conn.Dispose();
}
上一篇:task CancellationTokenSource


下一篇:Codeforces 841 D - Leha and another game about graph