2014-02-26 96 views
0
Main Section Section Name Shorted (30 chars max) Attribute Name Shorted (30) 
header   header        versionId 
header   header        serviceId 
header   header        creationTime 
header   header        brandCode 
clientProfile retirementPlanningAnalysis   signifShortFall 
clientProfile retirementPlanningAnalysis   isProjIncAdeq 
clientProfile retirementPlanningAnalysis   howCloseRetire 
clientProfile retirementPlanningAnalysis   clientsView 
clientProfile regularSavingsAndCapitalInvest  suffCashResEmerg 

我把這張表保存在excel中,我需要從sql中創建多個表格。每個表格將由「主要部分名稱」加上「部分名稱」組成。示例:header_headerclientProfile_retirementPlanningAnalysis。循環併爲每個唯一節名創建新表的最佳方法是什麼?列將來自屬性名稱,所以我需要將它們設置爲查詢。從Excel電子表格創建sql表格c#

+0

你有什麼試過的?你需要運行這個過程一次嗎?幾次? Excel的結構會改變嗎? –

+0

這只是一次性的過程,所以我可以創建一個關係模型。我已經嘗試將數據添加到列表中,但是我不確定附加屬性的最佳方式,如果我只從一個主要部分行創建表。 –

+0

Excel文件是否複雜?你不能一次手動建立數據庫模型嗎? –

回答

0

考慮這個excel文件是一個數據源,並使用ADO.Net中的Oledb將電子表格作爲DataTable獲取。這樣您就可以將整個電子表格放入DataTable中。並在數據表上執行所需的處理。根據需求在SQL中創建表。爲了您的幫助,我添加了一些可能有用的代碼。

System.Data.DataTable table1 = new System.Data.DataTable(); 
      OleDbConnection dbConnection1 = 
        new OleDbConnection 
         (@"Provider=Microsoft.Jet.OLEDB.4.0;" 
         + @"Data Source=YourFileName.xls;" 
         + @"Extended Properties=Excel 8.0;HDR=Yes;"); 
      dbConnection1.Open(); 
      try 
      { 
       OleDbDataAdapter dbAdapter1 = 
        new OleDbDataAdapter 
         ("SELECT * FROM [Sheet1$]", dbConnection1); 
       dbAdapter1.Fill(table1); 
      } 
      finally 
      { 
       dbConnection1.Close(); 
      } 

這是讀取excel文件的最佳方法。現在你可以使用DataTable表1來隨意進行進一步的處理。