首页 新闻 会员 周边 捐助

请教一下,excel导入到GridView1中,如果excel在源文件,那么就可以导入成功,其他路径的EXCEL就导入不进去

0
悬赏园豆:5 [已解决问题] 解决于 2012-05-30 15:46

 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();
   

NothingHave的主页 NothingHave | 初学一级 | 园豆:6
提问于:2012-05-09 15:59
< >
分享
最佳答案
0

看一看我们项目中的源代码:  这段代码 测试通过~~~

 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         }
收获园豆:4
土豆屋 | 菜鸟二级 |园豆:354 | 2012-05-17 21:24

lblMessage/Directory是什么控件来的,能否再告诉我一下,好吗

NothingHave | 园豆:6 (初学一级) | 2012-05-21 13:17

lblMessage/Directory是什么控件来的,能否再告诉我一下,好吗

NothingHave | 园豆:6 (初学一级) | 2012-05-23 13:12

@NothingHave: 哦 lblMessage是一个Lable控件的ID,   Directory是system.IO下面的一个文件操作类

土豆屋 | 园豆:354 (菜鸟二级) | 2012-05-27 00:43
其他回答(1)
0

你可以试试这样处理下 

string filename = FileUpload1.FileName;
        string savePath = Server.MapPath(("~\\upfiles\\") + filename); //upfiles这个是你应用程序所在的目录
        FileUpload1.SaveAs(savePath);

收获园豆:1
wvsy | 园豆:297 (菜鸟二级) | 2012-05-10 16:26

我就是不想只能在运用程序上才能导入,我想在其他地方也能导入

支持(0) 反对(0) NothingHave | 园豆:6 (初学一级) | 2012-05-15 09:19

@NothingHave: 请举例

支持(0) 反对(0) wvsy | 园豆:297 (菜鸟二级) | 2012-05-16 15:08

@wvsy: 我想在電腦桌面導入EXCEL數據庫,不想在應用所在的目錄導入,

支持(0) 反对(0) NothingHave | 园豆:6 (初学一级) | 2012-05-17 09:24

@NothingHave: 我给你的就是了,我就是用这个实现的

支持(0) 反对(0) wvsy | 园豆:297 (菜鸟二级) | 2012-05-17 09:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册