Excel VSTO SQL Server 浏览器






4.25/5 (4投票s)
使用 C# 和 VSTO 从 SQL Server 直接提取数据到 Excel。
引言
有时需要使用 Excel 自动化来从特定数据源获取一些信息,并直接将其放入 Excel 中。目标是更好地控制你需要执行的操作,为最终用户创建自动流程。本文就是关于此的。让我们使用自定义任务窗格来实现。
背景
我正在使用基于 Visual Studio 2008 的文档级加载项,并测试了从 SQL Server 2008 提取数据。它已使用大量数据进行了测试。
你必须在文本框中写入或粘贴你的连接字符串。一个被接受的示例是:“Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;”,然后你必须在富文本框中编写你的 SQL。
为了创建一个自定义任务窗格,你必须在其中添加一个 UserControl;我们将在下一步中了解如何操作。
使用代码
- 根据需要停靠自定义任务窗格“
msoCTPDockPositionBottom;
”。 - 根据需要修改“
throw new Exception(ex.ToString());
”。
加载项代码
在 ThisAddIn.cs 代码文件中提供的 ThisAddIn
类中编写你的代码。项目模板代码中有两个事件处理程序。要在加载加载项时运行代码,请将代码添加到 ThisAddIn_Startup
事件处理程序。要在卸载加载项之前运行代码,请将代码添加到 ThisAddIn_Shutdown
事件处理程序。
要将你的 UserControl 添加到任务窗格,我们必须将其声明为私有
private TpSqlEdit _tpSqlEdit;
要使用自定义任务窗格,你必须声明它。我将其声明为公共,因为我想从另一个类中获取它。
public Microsoft.Office.Tools.CustomTaskPane TpSqlEditCustomTaskPane;
初始化 User Control 并将其添加到自定义任务窗格
_tpSqlEdit = new TpSqlEdit();
TpSqlEditCustomTaskPane = CustomTaskPanes.Add(_tpSqlEdit, "SQL Editor");
停靠任务窗格。我默认将其停靠到底部。
TpSqlEditCustomTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionBottom;
下一步,使任务窗格可见。
//Show TaskPane
TpSqlEditCustomTaskPane.Visible = true;
User Control 代码
首先,我们将创建并填充我们的 DataTable
// DataTable Construction with Adapter and Connection
var conn = new SqlConnection(textBoxCS.Text);
var strSql = richTextBoxSQLEdit.Text;
conn.Open();
var da = new SqlDataAdapter(strSql, conn);
ar dt = new System.Data.DataTable();
da.Fill(dt);
定义活动工作表
var sht = Globals.ThisAddIn.Application.ActiveSheet as Worksheet;
使用 DataTable 并将其全部放入活动工作表中
下一步,让我们循环遍历 DataTable
的 DataRow
,并递增 i
,以递增标题的 Excel 列,以及对应于每个标题的单元格中的值。
// Loop thrue the Datatable and add it to Excel
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (var i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
// Add the Columns using the foreach i++ to get the cell references
if (sht != null) sht.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
// Increment value in the Progress Bar
progressBarGetData.Value = rowCount;
// Add the Columns using the foreach i++ to get the cell references
if (sht != null) sht.Cells[rowCount, i] = dr[i - 1].ToString();
// Refresh the Progress Bar
progressBarGetData.Refresh();
}
}
完整的 UserControl 代码
private void PopulateFromSql()
{
try
{
// DataTable Construction with Adapter and Connection
var conn = new SqlConnection(textBoxCS.Text);
var strSql = richTextBoxSQLEdit.Text;
conn.Open();
var da = new SqlDataAdapter(strSql, conn);
var dt = new System.Data.DataTable();
da.Fill(dt);
// Define the active Worksheet
var sht = Globals.ThisAddIn.Application.ActiveSheet as Worksheet;
var rowCount = 0;
progressBarGetData.Minimum = 1;
progressBarGetData.Maximum = dt.Rows.Count;
// Loop thrue the Datatable and add it to Excel
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (var i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
// Add the Columns using the foreach i++ to get the cell references
if (sht != null) sht.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
// Increment value in the Progress Bar
progressBarGetData.Value = rowCount;
// Add the Columns using the foreach i++ to get the cell references
if (sht != null) sht.Cells[rowCount, i] = dr[i - 1].ToString();
// Refresh the Progress Bar
progressBarGetData.Refresh();
}
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
关注点
连接字符串参考:http://www.connectionstrings.com/。
Deborah's Developer MindScape:http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx。
历史
- 版本 1.0。