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

ASP.NET2.0上传EXCEL文件到gridview中显示

2006-11-22 22:28 260 查看
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;
string size = FileUpload1.PostedFile.ContentLength.ToString();
string type = FileUpload1.PostedFile.ContentType;
string type2 = name.Substring(name.LastIndexOf(".") + 1);
string fpath = Server.MapPath("upfile") + "\\" + name;
if (type2 == "xls")
{
FileUpload1.SaveAs(fpath);
Response.Write("文件类型:" + type + "<br>文件名:" + name + "<br>文件类型2:" + type2 + "<br>");
Response.Write("文件路径:" + fpath);
string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + fpath + ";Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [sheet1$]", strConn);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
else
{
Response.Write("只能上传excel文件!");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: