1
在Web應用程序中,我試圖從Excel中的數據,並綁定到GridView控件,它工作正常,但在Excel中包含whcih是一種在細胞只有單一的價值和remaing細胞該行是空則顯示默認字母等[F,G,H ..]像下面在GridView header.The的頂部是實際的excel表 Excel數據到gridview?在Asp.net中?
下面在excel表格式
這是我的代碼在頁面加載甚至
System.Data.DataTable dt = null;
string excelFile = Server.MapPath("./performances.xls");
DataSet ds = new DataSet();
// Connection String.
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
// Create connection.
oledbConn = new OleDbConnection(connString);
// Opens connection with the database.
oledbConn.Open();
// Get the data table containing the schema guid, and also sheet names.
dt = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
// And respective data will be put into dataset table
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
oleda.Fill(ds, "TABLE");
i++;
}
// Bind the data to the GridView
grdMygrd .DataSource = ds.Tables[0].DefaultView;
grdMygrd.DataBind();
Session["Table"] = ds.Tables[0];
請幫幫我,解決這個問題可以 – 2012-02-09 11:02:17
粘貼一些代碼,因爲它是一個有點難以明白你用什麼方法,使約束力。你是否將它綁定爲數據集? – Igarioshka 2012-02-09 17:08:06
你肯定lgarioshka,我會把代碼 – 2012-02-10 03:42:41