简体   繁体   English

Excel加载项将数据从工作表传输到数据库:文件路径问题

[英]Excel add-in transferring data from sheets to database: file path problem

I wish to create an addin for excel which can read the current sheets data and then insert it into database. 我希望为excel创建一个插件,该插件可以读取当前工作表数据,然后将其插入数据库。 The database connection requires the file location. 数据库连接需要文件位置。 Therefore that approach cant be applied as I have to send the file that I have currently opened and can't specify the file path. 因此,无法应用该方法,因为我必须发送当前打开的文件,并且无法指定文件路径。

And moreover I need the data of each column to be inserted into separate column of table in SQL Server database. 此外,我需要将每列的数据插入到SQL Server数据库中表的单独列中。 Any suggestions and code to help? 有什么建议和代码可以帮助您吗?

You could create an Excel add-in with Excel using VBA. 您可以使用VBA使用Excel创建Excel加载项。 You need to add a reference to Microsoft ActiveX Data Objects (ADO) in the VBA editor (using Tools -> References), and then you'll have full access to the Connection, Command, RecordSet, etc objects. 您需要在VBA编辑器中添加对Microsoft ActiveX数据对象(ADO)的引用(使用工具->引用),然后您将具有对Connection,Command,RecordSet等对象的完全访问权限。

If I was doing this, I'd have the user specify the database/table/columns for each import batch using a form. 如果这样做的话,我希望用户使用表格为每个导入批处理指定数据库/表/列。 Then I'd create a Connection object and loop through each row of the data to create and execute an insert Command based on the data in the row. 然后,我将创建一个Connection对象,并遍历数据的每一行,以基于行中的数据创建并执行插入命令。

When you are done, you have the option of saving the finished workbook as an Excel Addin file (xla) which can be distributed to others. 完成后,您可以选择将完成的工作簿另存为Excel加载项文件(xla),该文件可以分发给其他人。

Here is a sample of what the insert code may look like: 以下是插入代码的示例:

Dim conn As New Connection
Dim comm As New Command
conn.Open YourConnectionString
Set comm.ActiveConnection = conn
comm.CommandText = "insert <table> (<column1>, <column2>, <column3>, ..., <columnN>)  values (" + <value1> + ", '" + <value2> + "', " + <value3> + ", '" + <valueN>+ "')"
comm.Execute
conn.Close

Does it have to be done within Excel? 是否必须在Excel中完成? You can do this with a simple ADO.NET app, specifying an OleDb provider for Excel, and another for SQL. 您可以使用一个简单的ADO.NET应用程序执行此操作,为Excel指定一个OleDb提供程序,为SQL指定另一个。

Example

There are lots of articles on the technique. 关于该技术的文章很多 You can do the same thing with minimal code in SQL Server Integration Services (SSIS, previously known as DTS). 您可以在SQL Server集成服务(SSIS,以前称为DTS)中使用最少的代码来执行相同的操作。 HEre's an old article on that . 这是一篇老文章

You can also do this from within Excel. 您也可以在Excel中执行此操作。 With VS2005 you would use VSTO - Visual Studio Tools for Office; 在VS2005中,您将使用VSTO-Office的Visual Studio工具; I think the function of VSTO is included in VS2008 Professional. 我认为VSTO的功能包含在VS2008 Professional中。 VS+VSTO (or VS2008) allows you to create AddIns for Office apps includin Excel. VS + VSTO(或VS2008)允许您为Office应用程序(包括Excel)创建加载项。 Within the AddIn you can do whatever you like in code, including using a System.Data.SqlClient to insert data into SQL Server. 在AddIn中,您可以执行代码中喜欢的任何事情,包括使用System.Data.SqlClient将数据插入SQL Server。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM