string a1 = FileUpload1.FileName;
string path = Server.MapPath(a1);//指定路径
// string path = this.File1.Value;//获取选择的路径
String strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;IMEX=1\"", path);
OleDbConnection oleDbConnection = new OleDbConnection(strConnectionString);
oleDbConnection.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter("Select * from [Sheet1$]", oleDbConnection);
oleDbDataAdapter.Fill(ds, "excel_data");
oleDbConnection.Close();
this.GridView1.DataSource = ds.Tables["excel_data"];
this.GridView1.DataBind();
看一看我们项目中的源代码: 这段代码 测试通过~~~
1 /// <summary> 2 /// 上传Execl文件并将数据显示在GridView 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void btnReadExcel_Click(object sender, EventArgs e) 7 { 8 #region 上传Execl文件并 9 if (!FileUpload1.HasFile) 10 { 11 lblMessage.Visible = true; 12 lblMessage.Text = "没有文件可以上传!"; 13 return; 14 } 15 //文件全名 16 string fileName = FileUpload1.FileName.Trim(); 17 //取得Execl文件MIME类型 18 string fileType = FileUpload1.PostedFile.ContentType.ToLower(); 19 20 //MIME类型是否符合标准 仅允许.xls或者.xlsx文件上传 21 if (!fileType.Equals("application/vnd.ms-excel") && !fileType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) 22 { 23 lblMessage.Visible = true; 24 lblMessage.Text = "只能上传Excel 2003或者2007(扩展名为 .xls或者.xlsx)的文件!"; 25 return; 26 } 27 28 string uploadPath = Server.MapPath("~/Files/upload/"); 29 if (!Directory.Exists(uploadPath)) 30 { 31 Directory.CreateDirectory(uploadPath); 32 } 33 34 FileUpload1.SaveAs(uploadPath + fileName); 35 #endregion 36 37 #region 读取Excel文件并显示在GridView 38 string xlsConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", uploadPath + fileName); 39 using (OleDbConnection conn = new OleDbConnection(xlsConn)) 40 { 41 OleDbDataAdapter oleda = new OleDbDataAdapter("select * from [Sheet1$]", conn); 42 DataSet ds = new DataSet(); 43 try 44 { 45 oleda.Fill(ds, "tb1"); 46 } 47 catch (OleDbException) 48 { 49 oleda.Dispose(); 50 conn.Close(); 51 conn.Dispose(); 52 // this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + oleex.Message + "')", true); 53 lblMessage.Visible = true; 54 lblMessage.Text = "请注意导入Execl文件的格式!"; 55 return; 56 } 57 catch (Exception) 58 { 59 lblMessage.Visible = true; 60 lblMessage.Text = "未知错误!请重新上传"; 61 return; 62 } 63 finally 64 { 65 //最后将新建的目录结构删除掉 66 if (File.Exists(uploadPath + fileName)) 67 { 68 File.Delete(uploadPath + fileName); 69 } 70 } 71 GridView1.DataSource = ds.Tables["tb1"]; 72 GridView1.DataBind(); 73 btnImportToKJCBS.Enabled = btnExportToExcel.Enabled = ds.Tables["tb1"].Rows.Count != 0 ? true : false; 74 lblMessage.Visible = false; 75 lblMessage.Text = ""; 76 } 77 #endregion 78 79 }
lblMessage/Directory是什么控件来的,能否再告诉我一下,好吗
lblMessage/Directory是什么控件来的,能否再告诉我一下,好吗
@NothingHave: 哦 lblMessage是一个Lable控件的ID, Directory是system.IO下面的一个文件操作类
你可以试试这样处理下
string filename = FileUpload1.FileName;
string savePath = Server.MapPath(("~\\upfiles\\") + filename); //upfiles这个是你应用程序所在的目录
FileUpload1.SaveAs(savePath);
我就是不想只能在运用程序上才能导入,我想在其他地方也能导入
@NothingHave: 请举例
@wvsy: 我想在電腦桌面導入EXCEL數據庫,不想在應用所在的目錄導入,
@NothingHave: 我给你的就是了,我就是用这个实现的