2017-06-13 74 views
0

我目前正在創建一個web應用程序來接收一個excel文件並將數據導入到數據庫中。我的當前連接字符串如下如何在連接字符串中使用文件路徑?

string connStringExcel = @"Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source=C:\users\myname\downloads\example.xlsx;Extended 
Properties=""Excel 
12.0;HDR=YES;"""; 

我試圖插入一個FileUpload控件,然後使用查找文件的路徑:

string filePath = 
System.IO.Path.GetFullPath(fileUpload.PostedFile.FileName); 

,然後替換完整的文件路徑我連接字符串只有filePath變量。我能做些什麼來選擇我自己的文件,以便它不必被硬編碼?

Errors

回答

2
string filePath = 
System.IO.Path.GetFullPath(fileUpload.PostedFile.FileName); 
string connStringExcel = $"Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source={filePath};Extended 
Properties='Excel 
12.0;HDR=YES;'"; 
+0

三分球,我已經試過你的答案,但我現在得到一個錯誤。我附上了它看起來像的圖像。任何想法? – nshull16

+0

此代碼使用String.Format的快捷方式,不適用於所有VS版本:( –

+0

您可能需要轉義內部引號或使用string.format。 – Scrobi

0
<asp:FileUpload ID="fuManual" Enabled="false" runat="server" /> 
    <asp:Button ID="btnUploadDoc" Text="Upload" runat="server" OnClick="UploadDocument" /> 



    public void UploadDocument(object sender, EventArgs e) 
     { 
     try 
     { 

      if (fuManual.HasFile) 
      { 

       string manual_filename = ddlStore.SelectedItem.Text + "_" + "Manual.xlsx"; 

       string extension = Path.GetExtension(fuManual.PostedFile.FileName); 

       // Import Excel Code for Manual Excel Data import.... 
       string FilePath = Server.MapPath("~/Temp/" + manual_filename); 
       fuManual.SaveAs(FilePath); 
       System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(
          "Provider=Microsoft.ACE.OLEDB.12.0; " + 
          "data source='" + FilePath + "';" + 
           "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" "); 
       myConnection.Open(); 

       DataTable mySheets = myConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); 
       DataSet ds_manualData = new DataSet(); 
       DataTable dt_manualData; 

       for (int i = 0; i < mySheets.Rows.Count; i++) 
       { 
        dt_manualData = makeDataTableFromSheetName(FilePath, mySheets.Rows[i]["TABLE_NAME"].ToString()); 
        ds_manualData.Tables.Add(dt_manualData); 
       } 


       ViewState["ManualData"] = ds_manualData; 
       DataTable dts = new DataTable(); 
       dts = ds_manualData.Tables[0]; 
       //here u have the whole excel file in the data table now send it to the database 
       //remember to send table u need SqlDbType-structured 

       myConnection.Close(); 
      } 
      else 
      { 
       lblError.Text = "please Select the file to be uploaded"; 
      } 
     } 
     catch (Exception ex) 
     { 
     }          
    } 
    private static DataTable makeDataTableFromSheetName(string FilePath, string sheetName) 
    { 
     System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(
     "Provider=Microsoft.ACE.OLEDB.12.0; " + 
     "data source='" + FilePath + "';" + 
     "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" "); 

     DataTable dtImport = new DataTable(); 
     System.Data.OleDb.OleDbDataAdapter myImportCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetName + "]", myConnection); 
     myImportCommand.Fill(dtImport); 
     return dtImport; 

    } 
+0

對於這個...... string FilePath = Server.MapPath(「〜/ Temp /」+ manual_filename);你可能需要創建一個黑色您的解決方案中名爲「Temp」的文件夾 –

相關問題