簡體   English   中英

將Excel電子表格導入SQL Server

[英]Importing an excel spreadsheet to SQL server

我在嘗試在SQL中導入名為Clean_info241 data.xlsx的文件時遇到問題

它位於:H:\\ New_folder並在服務器上

excel版本為2013,我不想使用導入向導。

目標是能夠一鍵執行在創建數據庫,使用電子表格中的數據並將其輸入到表中的位置。 我嘗試了很多方法,但沒有任何運氣,到目前為止只使用了導入向導。 編寫所有其他代碼。 數據位於excel的2個選項卡上,分別為johnKey $和davidcunliffe $

我認為您可以使用ac#應用來做到這一點。

像這樣說您的Excel工作表。

身份證名年齡
1撒罕28
2瓦蘭加26

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel; //You have to add it to reference and to add it to       reference you have to install Office package in your pc which runs the application or you can download this from Microsot

namespace ExcelImport
{
    class Program
    {
        static void Main(string[] args)
        {
            //string filePath = @"H:\New_folder\<YOUR FILE NAME>";
            string filePath = @"E:\Test.xlsx";

            Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

            var missing = System.Reflection.Missing.Value;

            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(filePath, false, true, missing, missing, missing, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, '\t', false, false, 0, false, true, 0);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //Here we say which tabs going to insert into database

            Microsoft.Office.Interop.Excel.Range xlRange = xlWorkSheet.UsedRange; //Get only worked range in excel sheet
            Array myValues = (Array)xlRange.Cells.Value2;

           int vertical = myValues.GetLength(0);
           int horizontal = myValues.GetLength(1);

           System.Data.DataTable dt = new System.Data.DataTable();

            // must start with index = 1
            // get header information
            for (int i = 1; i <= horizontal; i++)
            {
                dt.Columns.Add(new DataColumn(myValues.GetValue(1, i).ToString()));
            }


            // Get the row information
            for (int a = 2; a <= vertical; a++)
            {
                object val = myValues.GetValue(a, 1);
                if (val != null)
                {
                    object[] poop = new object[horizontal];
                    for (int b = 1; b <= horizontal; b++)
                    {
                        object colVal = null;
                        poop[b - 1] = colVal = myValues.GetValue(a, b);
                    }
                    DataRow row = dt.NewRow();
                    row.ItemArray = poop;
                    dt.Rows.Add(row);
                }
            }

            foreach (DataRow row in dt.Rows)
            {
                string id = row["ID"].ToString();
                string Name = row["Name"].ToString();
                string Age = row["Age"].ToString();

                string connectionString = "your connection string";

                SqlConnection con = new SqlConnection(connectionString);
                con.Open();

                StringBuilder insertQry = new StringBuilder();
                insertQry.AppendFormat("INSERT INTO YourTable(ID,Name,Age) VALUES({0},{1},{2})", id, Name, Age);

                SqlCommand cmd = new SqlCommand(insertQry.ToString(), con);
                cmd.ExecuteNonQuery();
            }
        }
    }
}

希望這將有助於您解決問題。

我將使用向導並另存為SSIS包(如果使用的是SQL Server的較舊版本,則為DTS)。 然后,我將修改程序包,以便XLS文件名和Sheet名稱成為變量。

之后,您可以根據需要包裝該程序包的執行。 是帶有按鈕,VB腳本,代理作業或其他任何內容的WinForms ...

暫無
暫無

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

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