search
HomeSoftware TutorialOffice SoftwareSource code implementation of Excel importing into SQLServer

A source code that can import excel into sqlserver

Give you a code to upload data directly to SqlServer in Excel:

Can be modified appropriately,

FormName is the table in the database,

DBString is the database connection field,

Sub UploadData()

If FormName = "" Then Exit Sub

Dim a As New ADODB.Connection

Dim b As New ADODB.Recordset

a.Open DBString

RowCount = Application.CountA(Rows("1:1"))

For i = 2 To application.counta(range("a:a"))

sql = """

Set b = Nothing

For j = 1 To RowCount - 1

sql = sql & "'" & Cells(i, j) & "', "

Next

sql = "insert into " & FormName & "values(" & sql & "'" & Cells(i, RowCount) & "')"

b.Open Source:=sql, ActiveConnection:=a

Next

MsgBox "OK!"

End Sub

There are various methods, just find a suitable one,

To complete your task, you need to understand the connection properties of the database. The following are the connection fields, which can be modified according to your actual situation. You have to set the basic parameters yourself,

Provider=SQLOLEDB.1;Persist Security Info=False;User id=md-inspection;PWD=mdOL20!31)25;Initial Catalog=Online_Inspection;Data Source=Cduvmdb01,1433

In addition, relevant references need to be added to the VBE page:

Source code implementation of Excel importing into SQLServer

A C Excel source code for importing database

Environment: c#.2005 Access Sql

1. Insert the DataTable into the database

public static void DataTableToDB()

{

string _strExcelFileName = @"D:\example.xls";

DataTable dtExcel = ExcelToDataTable(_strExcelFileName,"Sheet1");

for (int i = 0; i

{

InsertDataToAccess(dtExcel.Rows[i][0].ToString(), float.Parse(dtExcel.Rows[i][1].ToString()));

}

}

2. Read Excel data into DataTable

public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)

{

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" "Data Source=" strExcelFileName ";" "Extended Properties=Excel 5.0;";

string strExcel = string.Format("select * from [{0}$]", strSheetName);

DataSet ds = new DataSet();

using (OleDbConnection conn = new OleDbConnection(strConn))

{

conn.Open();

OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);

adapter.Fill(ds, strSheetName);

conn.Close();

}

return ds.Tables[strSheetName];

}

3. Insert data into Access database table

public static void InsertDataToAccess(string _strPara,float _fPara)

{

OleDbConnection oleDbConn = new OleDbConnection();

oleDbConn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ExcelData.mdb;User Id=admin;Password=;";

oleDbConn.Open();

string strInsertString = "INSERT INTO tb_excelData (strCollumn1,fCollumn2) VALUES (@strCollumn1,@fCollumn2)";

OleDbCommand oComm = new OleDbCommand(strInsertString, oleDbConn);

oComm.Parameters.Add("@strCollumn1", OleDbType.Char , 50);

oComm.Parameters["@strCollumn1"].Value = _strPara;

oComm.Parameters.Add("@fCollumn2", OleDbType.Double);

oComm.Parameters["@fCollumn2"].Value = _fPara;

ocomm.ExecuteNonQuery();

oleDbConn.Close();

}

How to batch import multiple excel data files into another excel file table source code

code show as below:

Sub file merge()

Dim wb As Workbook, sh As Worksheet, pT As String, wb2 As Workbook, t

t = Timer

'Check whether any unrelated workbooks are opened

If Workbooks.Count > 1 Then

MsgBox "Close other open workbooks"

Exit Sub

End If

'Specify the folder where the file is located

With Application.FileDialog(msoFileDialogFolderPicker)

.Show

If .SelectedItems.Count = 0 Then Exit Sub 'If you press Cancel or X closes the dialog box, exit directly

pT = .SelectedItems(1)

End With

Application.ScreenUpdating = False 'Turn off screen refresh

Application.DisplayAlerts = False 'Close dialog box

'Create a new worksheet named New.xls

Set wb = Workbooks.Add

shJS = wb.Worksheets.Count

fn = Dir(pT & "\*.xls")

While fn """

If fn = wb.Name Then GoTo gg

i = i 1

If i > shJS Then

Set sh = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count))

End If

Set wb2 = Workbooks.Open(fn, ReadOnly = True)

wb2.Worksheets(1).Cells.Copy wb.Worksheets(i).Cells

wb.Worksheets(i).Name = Left(fn, Len(fn) - 4)

wb2.Close

gg:

fn = Dir

Wend

wb.SaveAs pT & "\new.xls"

wb.Close

Application.ScreenUpdating = True 'Turn on screen refresh

Application.DisplayAlerts = True 'Open dialog box

MsgBox "Shared Time" & Timer - t & "Seconds. Generate a new file new.xls"

End Sub

Complete code for importing data from excel to delphi

Table A in the database has fields A1, A2, A3, a4, a5

Spreadsheet d\:shuju.xls

Contains items b1, b2, b3, b4, b5

insert into dbo.A

(a1,a2,a3,a4,a5)

SELECT b1,b2,b3,b4,b5

FROM OPENDATASOURCE ('Microsoft.Jet.OLEDB.4.0',

'Data Source=d:\shuju.xls;Extended Properties=EXCEL 8.0' )...[Sheet1$] Rowset_1

In this way, you can import it. Of course, you can also use the control TXLSFile. If you need it, you can ask me for it

mchestnut@163.com

The above is the detailed content of Source code implementation of Excel importing into SQLServer. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
Your Calculator App Can Be Replaced By Microsoft ExcelYour Calculator App Can Be Replaced By Microsoft ExcelMar 06, 2025 am 06:01 AM

Ditch the Calculator: Why and How to Use Excel for All Your Calculations I haven't touched a calculator in ages. Why? Because Microsoft Excel handles all my calculations with ease, and it can do the same for you. Why Excel Trumps a Calculator While

Don't Create Tables in Word: Use Excel InsteadDon't Create Tables in Word: Use Excel InsteadMar 06, 2025 am 03:04 AM

Creating tables in Word, although improved, is still cumbersome and sometimes brings more problems. This is why you should always create tables in Microsoft Excel. Why is it better to create tables in Excel? In short, Word is a word processor, while Excel is a data processor. So Word is not built for the best table creation, but its similar product, Excel. Here are just some of the reasons why creating tables in Excel is better than using Microsoft Word: Although it is surprising that you can use many Excel-like features in Microsoft Word tables, in Excel you

How to Reduce the Gaps Between Bars and Columns in Excel Charts (And Why You Should)How to Reduce the Gaps Between Bars and Columns in Excel Charts (And Why You Should)Mar 08, 2025 am 03:01 AM

Enhance Your Excel Charts: Reducing Gaps Between Bars and Columns Presenting data visually in charts significantly improves spreadsheet readability. Excel excels at chart creation, but its extensive menus can obscure simple yet powerful features, suc

How to Use the AVERAGEIF and AVERAGEIFS Functions in ExcelHow to Use the AVERAGEIF and AVERAGEIFS Functions in ExcelMar 07, 2025 am 06:03 AM

Quick View of AVERAGEIF and AVERAGEIFS Functions in Excel Excel's AVERAGEIF and AVERAGEIFS functions can be used to calculate the average value of a dataset. However, unlike simpler AVERAGE functions, they are able to include or exclude specific values ​​in the calculation. How to use the AVERAGEIF function in Excel Excel's AVERAGEIF function allows you to calculate the average value of a filtered dataset based on a single condition set. AVERAGEIF function syntax The AVERAGEIF function contains three parameters: =AVERAGEIF(x,y,z)

5 Things You Can Do in Excel for the Web Today That You Couldn't 12 Months Ago5 Things You Can Do in Excel for the Web Today That You Couldn't 12 Months AgoMar 22, 2025 am 03:03 AM

Excel web version features enhancements to improve efficiency! While Excel desktop version is more powerful, the web version has also been significantly improved over the past year. This article will focus on five key improvements: Easily insert rows and columns: In Excel web, just hover over the row or column header and click the " " sign that appears to insert a new row or column. There is no need to use the confusing right-click menu "insert" function anymore. This method is faster, and newly inserted rows or columns inherit the format of adjacent cells. Export as CSV files: Excel now supports exporting worksheets as CSV files for easy data transfer and compatibility with other software. Click "File" > "Export"

Microsoft Excel Keyboard Shortcuts: Printable Cheat SheetMicrosoft Excel Keyboard Shortcuts: Printable Cheat SheetMar 14, 2025 am 12:06 AM

Master Microsoft Excel with these essential keyboard shortcuts! This cheat sheet provides quick access to the most frequently used commands, saving you valuable time and effort. It covers essential key combinations, Paste Special functions, workboo

How to Use LAMBDA in Excel to Create Your Own FunctionsHow to Use LAMBDA in Excel to Create Your Own FunctionsMar 21, 2025 am 03:08 AM

Excel's LAMBDA Functions: An easy guide to creating custom functions Before Excel introduced the LAMBDA function, creating a custom function requires VBA or macro. Now, with LAMBDA, you can easily implement it using the familiar Excel syntax. This guide will guide you step by step how to use the LAMBDA function. It is recommended that you read the parts of this guide in order, first understand the grammar and simple examples, and then learn practical applications. The LAMBDA function is available for Microsoft 365 (Windows and Mac), Excel 2024 (Windows and Mac), and Excel for the web. E

If You Don't Use Excel's Hidden Camera Tool, You're Missing a TrickIf You Don't Use Excel's Hidden Camera Tool, You're Missing a TrickMar 25, 2025 am 02:48 AM

Quick Links Why Use the Camera Tool?

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software