你应该考虑这样的事情。
将 Excel 连接到 SQL Server
Devart Excel Add-in for SQL Server 允许您将 Excel 连接到 SQL Server 数据库,检索实时 SQL Server 数据并将其加载到 Excel,然后修改这些数据并将更改保存回 SQL Server。下面介绍了如何通过几个简单的步骤将 Excel 连接到 SQL Server 并将 SQL Server 数据加载到 Excel。
要开始将 Excel 链接到 SQL Server,请在功能区上单击 DEVART 选项卡,然后单击获取数据按钮。这将显示导入数据向导,您需要在其中创建 Excel SQL Server 连接并配置查询以将数据从 SQL Server 获取到 Excel:
Devart 功能区选项卡
- 指定连接参数
在连接编辑器对话框中,需要输入必要的连接参数:
Login details - select whether to use Windows authentication or SQL Server authentication. In the latter case you need to enter your SQL Server user name and password.
User name - your SQL Server user name. Required only for SQL Server authentication.
Password - your SQL Server password. Required only for SQL Server authentication.
Database - the name of SQL database to connect to Excel.
如果您需要更详细地配置 Excel SQL Server 连接,您可以选择单击“高级”按钮并配置高级连接参数。您可以在那里配置连接加密、弹性参数等。
要检查您是否已将 Excel 正确连接到 SQL Server,请单击“测试连接”按钮。
Excel SQL 连接设置
2.选择是否在Excel工作簿中存储连接
您可以选择更改连接和查询数据在 Excel 工作簿和 Excel 设置中的存储方式:
Allow saving add-in specific data in Excel worksheet - clear this check box in case you don't want to save any Excel add-in specific data in the Excel worksheet - connections, queries, etc. In this case, if you want to reload data from SQL Server to Excel or save modified data back to SQL Server, you will need to reenter both the connection settings and query.
Allow saving connection string in Excel worksheet - clear this check box if you want your SQL Server connection parameters not to be stored in the Excel. In this case you will need to reenter your connection settings each time you want to reload SQL Server data or modify and save them to SQL Server. However, you may share the Excel workbook, and nobody will be able to get any connection details from it.
Allow saving password - it is recommended to clear this check box. If you don't clear this check box, all the connection settings, including your SQL Server password, will be stored in the Excel workbook. And anyone having our Excel Add-in for SQL Server and the workbook will be able to link Excel to the SQL Server, get data from it, and modify them. But in this case you won't need to reenter anything when reloading data from SQL Server to Excel or saving them to SQL Server.
Allow reuse connection in Excel - select this check box if you want to save this connection on your computer and reuse it in other Excel workbooks. It does not affect saving connection parameters in the workbook itself. You need to specify the connection name, and after this you will be able to simply select this connection from the list
- 配置查询以获取数据
您可以使用 Visual Query Builder 对其进行可视化配置,或者切换到 SQL Query 选项卡并键入 SQL Query。要直观地配置查询,请执行以下操作:
In the Object list select the SQL Server table to load its data to Excel.
In the tree below clear check boxes for the columns you don't want to import data from.
Optionally expand the relation node and select check boxes for the columns from the tables referenced by the current table's foreign keys to add them to the query.
In the box on the right you may optionally configure the filter conditions and ordering of the imported data and specify the max number of rows to load from SQL Server to Excel. For more information on configuring the query you may refer to our documentation, installed with the Excel Add-ins.
指定查询后,您可以选择单击“下一步”并预览一些第一个返回的行。或者单击完成并开始数据加载。
将 SQL Server 数据检索到 Excel 的查询
编辑实时 SQL Server 数据
数据从 SQL Server 加载到 Excel 后,您可以像使用通常的 Excel 工作表一样处理这些数据。您可以通过单击功能区 Devart 选项卡上的刷新来立即刷新 SQL Server 中的数据,因此,您的工作簿中始终有来自 SQL Server 的最新实时数据。
如果要在 Excel 中编辑 SQL Server 数据并将其保存回 SQL Server,则需要先单击功能区 Devart 选项卡上的编辑模式。否则,您所做的更改将无法保存到 SQL Server。
启动编辑模式后,您可以像通常在 excel 中那样编辑数据 - 删除行,修改其单元格值。无法在 SQL Server 中编辑的列将具有斜体字体,并且您无法编辑这些列中的值。要添加新行,请在以绿色突出显示的表格的最后一行输入所需的值。
在 Excel 中编辑 SQL Server 数据
要将更改应用到数据库中的实际数据,请单击“提交”按钮。或单击回滚以回滚所有更改。请注意,在您单击“提交”之前,更改不会保存到数据库中,即使您保存了工作簿。
另外,请考虑这一点。
https://www.mssqltips.com/sqlservertip/3436/passing-dynamic-query-values-from-excel-to-sql-server/
我在我的办公室使用这些技术,我正在将近 900k 行和大约 24 列从 SQL Server 移动到 Excel。刷新时间不到 60 秒。