如何在服务器中正确地将Excel文件上传到网格视图?

时间:2018-08-27 05:50:44

标签: c# asp.net excel

正在开发一个系统,该系统应将Excel文件上载到GridView,然后单击按钮即可将GridView项保存到SQL表中。我做了整个事情,并在localhost中执行,没有任何错误。但是,当我将系统上传到服务器时,将Excel文件上传到GridView时出现错误。以下是我在尝试捕获异常中遇到的错误。

  

FileShare共享,Int32 bufferSize,FileOptions选项,System.IO.FileStream..ctor(字符串路径,FileMode模式)(位于System.Web.HttpPostedFile.SaveAs(字符串文件名),位于StyleOperations.Operations处的String msgPath,布尔bFromProxy) .uploadLinkBut​​ton_Click(Object sender,EventArgs e)

我在服务器中创建了文件夹StyleFiles,并尝试了。但是,我仍然遇到相同的错误。下面我提到了我的代码和Excel File格式,有人可以帮助我。预先谢谢你。

   protected void uploadLinkButton_Click(object sender, EventArgs e)
    {
        try
        {
            string UpPath = ConfigurationManager.AppSettings["UploadPath"].ToString();

            if (StyleOperationsFileUpload.HasFile)
            {

                string Extension = Path.GetExtension(StyleOperationsFileUpload.PostedFile.FileName);
                string FileName = Path.GetFileName(StyleOperationsFileUpload.PostedFile.FileName);

                string FolderPath = Server.MapPath("~/StyleFiles/" + FileName);
                string UpFilePath = Path.Combine(UpPath, FileName);
                StyleOperationsFileUpload.SaveAs(FolderPath);
                DataTable operationsDT = dba.Import_To_Grid(FolderPath, Extension, "Yes");
                UploadGridView.DataSource = operationsDT;
                UploadGridView.DataBind();
                SaveLinkButton.Enabled = true;
            }
            else
            {
                Response.Write("<script>alert('You Have Not Chosen a File To Upload')</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Please Restart the system')</script>" + ex);
        }
  }

Excel文件格式如下。相同的格式将是GridView的格式。

| 1 |DESCRIPTION|  SMV   |SEQ NO
| 2 |   Des1    |  1.2   | 1
| 3 |   Des2    |  2.5   | 2
| 4 |   Des3    |  5.8   | 3
| 5 |   Des4    |  4.2   | 4

这是@Tetsuya提出的完整错误

Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom 
error settings for this application prevent the details of the application error 
from being viewed remotely (for security reasons). It could, however, be viewed 
by browsers running on the local server machine. 

Details: To enable the details of this specific error message to be viewable on 
remote machines, please create a <customErrors> tag within a "web.config" 
configuration file located in the root directory of the current web application. 
This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
    <customErrors mode="Off"/>
</system.web>
</configuration>

Notes: The current error page you are seeing can be replaced by a custom error 
page by modifying the "defaultRedirect" attribute of the application's     
<customErrors> configuration tag to point to a custom error page URL.


 <!-- Web.Config Configuration File -->
<configuration>
<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>

0 个答案:

没有答案