2011-12-16 38 views
0

我試圖將.xlsx文件(MS Excel 2007)導入到數據庫(My SQL)中。 它的工作在我的本地PC而不是在應用程序IIS6將Excel導入到MySql中 - 外部表格未處於預期格式

這是從的try-catch子句

An external table is not in the expected format. 

我已經安裝了Microsoft Access數據庫引擎拋出的錯誤消息下運行的服務器PC 2010可再發行版 和2007 Office System Driver在服務器PC中。

下面是代碼

private void vIPToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     fiDatabase = null; 
     ofdDatabase.Title = "Select VIP list to input"; 
     ofdDatabase.Filter = "Excel Files|*.xlsx"; 
     if (ofdDatabase.ShowDialog() == DialogResult.OK) 
     { 
      fiDatabase = new FileInfo(ofdDatabase.FileName); 
      if (fiDatabase.Exists) 
      { 
       DeleteVIPList(); 

       lblDateCreated.Text = fiDatabase.CreationTime.ToShortDateString(); 
       lblDatabase.Text = fiDatabase.Name; 
       lblLocation.Text = fiDatabase.DirectoryName; 
       lblSize.Text = String.Format("{0:0,0.0}", fiDatabase.Length) + " byte"; 
       btnLoadDatabase.Visible = true; 
       file = new StreamReader(fiDatabase.FullName); 

       OleDbConnection theConnection = 
        new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\VIPLIST092211.xlsx;Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";"); 
       try 
       { 
        theConnection.Open(); 
        OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", theConnection); 
        DataSet theDS = new DataSet(); 
        DataTable dt = new DataTable(); 
        theDataAdapter.Fill(dt); 
        theConnection.Close(); 

        dgvCustomer.DataSource = dt.DefaultView; 

        lblRecord.Text = dgvCustomer.Rows.Count.ToString(); 

        update = new Update(); 
        update.connectionString = MyCon; 
        update.UpdateVIPList(dt); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
      else 
      { 
       MessageBox.Show("No Database Selected"); 
      } 
     } 
    } 

我,並在錯誤消息「theConnection.Open()」如果任何人有過這個問題,我 請分享解決方案。 謝謝。

+0

你可以發佈你的UpdateVIPList方法。 – BizApps 2011-12-16 04:48:18

回答

0

我的建議..

批量上傳數據到MySQL從Excel中您可以使用CSV格式,更方便,並且不需要任何驅動程序在服務器pc.Here安裝的步驟

1)使用保存Excel CSV格式另存爲選項

2)根據表格格式

3責令 列)運行以下查詢

負載 數據本地infile的 'E:\ FileName.csv' 到由 '「' 由 '\ N' (COL1,COL2,COL3,COL4)封端的線包圍表模板字段 通過終止 ''

但這取決於您的用例

0

您可以發佈您的UpdateVIPList方法嗎?

林不知道,但也許你需要循環在你的數據表中插入你的桌子上各行

例子:

 foreach(DataRow drow in dt.Rows) 
     { 
       string mycolumnName = drow["ColumName"].ToString(); 
       // Loop Per Row then Update 
       update = new Update(); 
       update.UpdateVIPList(mycolumnNam); 
     } 

其中

public bool UpdateVIPList(string mycolumnname) 
    { 

     //Your insert Logic Insert into Table (..) values (...) 
     //Do stuff 
     return true; 
    } 

問候

0

你說它只在本地主機上工作。問題可能出在您的連接字符串中,您指定'Data Source = c:\ VIPLIST092211.xlsx'爲當前主機上的確切路徑。但是這個文件也必須可以從服務器主機訪問。你應該爲你的客戶分享這個文件。

+0

非常感謝您的回答。我非常感謝你的幫助 – 2012-01-09 14:55:52

相關問題