簡體   English   中英

無法讀取上傳的Excel文件

[英]Not able to read the uploaded excel file

您好我正在嘗試上傳Excel文件並將數據從已上傳的Excel工作表讀取到另一個Excel工作表,但是我收到文件不可用錯誤。有人可以幫忙嗎

string filetest1;


protected void UploadButton_Click(object sender, EventArgs e)
    {
       if (FileUpload1.HasFile)
           SaveFile(FileUpload1.PostedFile);
        else
            UploadStatusLabel1.Text = "You did not specify a file to upload.";
    }
        void SaveFile(HttpPostedFile file)
        {
            string savePath = "c:\\Users\\*****\\Desktop\\";

        // Get the name of the file to upload.
        string fileName = file.FileName;

        // Create the path and file name to check for duplicates.
        string pathToCheck = savePath + fileName;

        // Create a temporary file name to use for checking duplicates.
        string tempfileName = "";

        // Check to see if a file already exists with the
        // same name as the file to upload.        
        if (System.IO.File.Exists(pathToCheck))
        {
            int counter = 2;
            while (System.IO.File.Exists(pathToCheck))
            {
                // if a file with this name already exists,
                // prefix the filename with a number.
                tempfileName = counter.ToString() + fileName;
                pathToCheck = savePath + tempfileName;
                counter++;
            }

            fileName = tempfileName;

            // Notify the user that the file name was changed.
            UploadStatusLabel1.Text = "A file with the same name already exists." +
                "<br />Your file was saved as " + fileName;
        }
        else
        {
            // Notify the user that the file was saved successfully.
            UploadStatusLabel1.Text = "Your file was uploaded successfully.";
        }

        // Append the name of the file to upload to the path.
        savePath += fileName;
        filetest1 = fileName;
        MessageBox.Show(filetest1);
        // Call the SaveAs method to save the uploaded
        // file to the specified directory.
        file.SaveAs(savePath);
   }

   protected void Compare_Click(object sender, EventArgs e)
    {

        string filetest1 = FileUpload1.PostedFile.FileName;
        **MessageBox.Show(filetest1);**
    -------------

    obook = oAPP.Workbooks.Open(filetest1); 
}

有2個消息框,第一個消息框返回上載文件的名稱,但是第二個消息框返回空的消息框

將文件名存儲在會話變量中,並在需要的地方使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM