2017-06-16 33 views
1

理念:的Excel到SQL Server - C#

  • 閱讀XLS和上傳在Microsoft SQL數據

問題:

  • 只有第一列被上傳到DB

我的代碼:

private void button1_Click(object sender, EventArgs e) { 
    // string path = @"XXXX\xls_test\Book1.xlsx"; 
    string path = @ "XXXX\xls_test\Book1.xlsx"; 
    ImportDataFromExcel(path); 
    } 


    public void ImportDataFromExcel(string excelFilePath) { 
    //declare variables - edit these based on your particular situation 
    string ssqltable = "Table1"; 
    // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have different 
    string myexceldataquery = "select student from [Sheet1$]"; 
    try { 


    //create our connection strings 
    //string sexcelconnectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";" + "Extended Properties=" + "\"Excel 8.0; HDR=Yes; IMEX=1;\""; 
    string sexcelconnectionstring = @ "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= XXXX\xls_test\Book1.xlsx" + ";" + "Extended Properties=Excel 12.0"; 

    string ssqlconnectionstring = "XXXX"; 
    //execute a query to erase any previous data from our destination table 
    string sclearsql = "delete from " + ssqltable; 
    SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring); 
    SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn); 
    sqlconn.Open(); 
    sqlcmd.ExecuteNonQuery(); 
    sqlconn.Close(); 
    //series of commands to bulk copy data from the excel file into our sql table 
    OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring); 
    OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn); 

    oledbconn.Open(); 
    OleDbDataReader dr = oledbcmd.ExecuteReader(); 
    SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring); 
    bulkcopy.DestinationTableName = ssqltable; 
    while (dr.Read()) { 
    bulkcopy.WriteToServer(dr); 
    } 
    dr.Close(); 
    oledbconn.Close(); 
    MessageBox.Show("File imported into sql server."); 
    } catch (Exception ex) { 
    //handle exception 
    MessageBox.Show(ex.ToString()); 
    MessageBox.Show("Enter exception"); 
    } 
    } 

表上的DB:

CREATE TABLE [dbo].[Table1](
    [student] [varchar](50) NULL, 
    [rollno] [int] NULL, 
    [course] [varchar](50) NULL 
) ON [PRIMARY] 
GO 

如果你想訪問從和DB的XLS的例子2個圖像: https://drive.google.com/drive/folders/0B98UpTa2n4XbeHZtOHU2cVotUms?usp=sharing

最後,該代碼的大部分,我從了: https://code.msdn.microsoft.com/office/Import-Excel-Spreadsheet-2b7ca7cf#content 我改變了一點,但大多數想法都來自這些鏈接。

歡迎任何幫助!謝謝。

回答

5

猜測的一點,但你必須:

string myexceldataquery = "select student from [Sheet1$]"; 

然後你說

只有第一列被上傳到DB

這似乎如果您只在列上選擇,則完全可以預期。

也許選擇要以及其他領域(我猜他們!):

string myexceldataquery = "select student, rollno, course from [Sheet1$]"; 
+0

哇,那是我的青少年! 是的,你的猜測是這個問題。 謝謝! –

0

,如果你做了你的Excel文件另存爲,把它變成這將是對你很容易一個.csv文件。然後,您可以使用TextFieldParser並將分隔符設置爲逗號。

using (TextFieldParser parser = new TextFieldParser(csvFilePath)) 
{ 
    Dictionary<string, CustomRecord> csvDictionary = new Dictionary<string, CustomRecord>(); 

    parser.TextFieldType = FieldType.Delimited; 
    parser.SetDelimiters(","); 
    while (!parser.EndOfData) 
    { 
     //write to custom record 
    } 

稍後,將CustomRecord寫入數據庫。

foreach (KeyValuePair<string, CustomRecord> kvp in CustomRecord) 
{ 
    //sql insert statement here using the key values from the dictionary as your sql values) 
}