繁体   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