您的位置:首页 > 编程语言 > ASP

Asp.net中显示EXCEL中工作表的内容

2009-03-22 08:43 274 查看
protected void dispdata_Click(object sender, EventArgs e)
{
string file_path = FileUpload1.PostedFile.FileName.ToString().Trim();

string[] sheetNameString;
string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file_path.ToString() + ";" + "Extended Properties=Excel 8.0";
OleDbConnection objcon = new OleDbConnection(ConnectionString);
objcon.Open();
DataTable dt = null;

try
{
objConn = new OleDbConnection(ConnectionString);
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt != null)
{
sheetNameString = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)//遍历取出各个工作表的SHEET名称
{
sheetNameString[i] = row["TABLE_NAME"].ToString();
i++;
}

}
}
catch(Exception er)
{
Response.Write(er.ToString());
}

string str1="select * from ["+sheetNameString[0].ToString()+"]";//显示第一个工作表的内容,要显示多个工作表,请循环显示则行
OleDbDataAdapter olda = new OleDbDataAdapter(str1, objcon);
DataSet ds = new DataSet();
olda.Fill(ds,"studenttab");
GridView1.DataSource = ds.Tables["studenttab"].DefaultView;
GridView1.DataBind();
objcon.Close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: