VSTO Excel 2007:在加载项中包含或嵌入工作簿/工作表

Sve*_*sen 5 excel vsto

我想在Excel加载项中包含/嵌入带有预定义布局的Excel工作表,但我无法将项目项"工作簿"添加到我的VSTO项目中,也无法添加对"Excel工作簿"项目的引用.那我该怎么做呢?

我的目标是构建一个Excel加载项,它将新工作表添加到现有工作簿(从SAP下载)以聚合数据.

斯文

AMi*_*ico 3

创建包含工作表的工作簿。如果需要,将工作簿另存为模板。将工作簿作为资源嵌入。如何将资源转换为工作簿/工作表将取决于文档格式。如果使用 SpreadSheet XML (XMLSS),则相当容易。如果是二进制(xls 或 xlt),那么您将必须操作资源。Excel 无法读取流。

使用 VSTO Excel 加载项项目和项目资源的示例

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

        ' Start of VSTO generated code

        Me.Application = CType(Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(GetType(Excel.Application), Me.Application), Excel.Application)

        ' End of VSTO generated code

        'setup a workbook and worksheet for sample code to work
        Dim oWB As Excel.Workbook = Me.Application.Workbooks.Add()
        Dim oWS As Excel.Worksheet = CType(oWB.Worksheets.Add, Excel.Worksheet)

        'create temporary template
        Dim sPath As String = My.Computer.FileSystem.GetTempFileName
        My.Computer.FileSystem.WriteAllBytes(sPath, My.Resources.Book1, False)

        'open with excel
        Dim oTemplate As Excel.Workbook = Me.Application.Workbooks.Add(sPath)

        'specify worksheet from a different workbook
        '   copies the template worksheet into destination workbook
        oTemplate.Worksheets.Copy(oWS)

        'no longer need template
        oTemplate.Close()

        'delete the temporary file
        My.Computer.FileSystem.DeleteFile(sPath)

        'get our worksheet
        Dim oReportWS As Excel.Worksheet = CType(oWB.Worksheets.Item("Template"), Excel.Worksheet)

        'write our data
        CType(oReportWS.Cells(1, 1), Excel.Range).Value2 = "Here I am!"

    End Sub

End Class
Run Code Online (Sandbox Code Playgroud)