Home  >  Article  >  Software Tutorial  >  Source code implementation of Excel importing into SQLServer

Source code implementation of Excel importing into SQLServer

WBOY
WBOYforward
2024-01-23 21:45:10953browse

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:docexcel.net. If there is any infringement, please contact admin@php.cn delete