簡體   English   中英

如何使用特定模板創建Excel工作表?

[英]how to create excel sheet with specific template?

我不知道如何使用帶有特定模板的Office互操作程序集創建excel表,盡管這似乎並不困難。 任何幫助將不勝感激。

在此站點中有示例http://www.dotnetperls.com/excel-vbnet 您的項目中需要Office的Microsoft.Office.Interop引用。

從模板創建(請參閱更多https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.add.aspx

Imports Microsoft.Office.Interop.Excel

Module Module1
    Sub Main()
    ' Create new Application.
    Dim excel As Application = New Application

    Dim w As Workbook = excel.Workbooks.Add("c:\templateFileName")

    ' Close.
    w.Close()
    End Sub
End Module

從文件讀取:

Imports Microsoft.Office.Interop.Excel

Module Module1
    Sub Main()
    ' Create new Application.
    Dim excel As Application = New Application

    ' Open Excel spreadsheet.
    Dim w As Workbook = excel.Workbooks.Open("C:\file.xls")

    ' Loop over all sheets.
    For i As Integer = 1 To w.Sheets.Count

        ' Get sheet.
        Dim sheet As Worksheet = w.Sheets(i)

        ' Get range.
        Dim r As Range = sheet.UsedRange

        ' Load all cells into 2d array.
        Dim array(,) As Object = r.Value(XlRangeValueDataType.xlRangeValueDefault)

        ' Scan the cells.
        If array IsNot Nothing Then
        Console.WriteLine("Length: {0}", array.Length)

        ' Get bounds of the array.
        Dim bound0 As Integer = array.GetUpperBound(0)
        Dim bound1 As Integer = array.GetUpperBound(1)

        Console.WriteLine("Dimension 0: {0}", bound0)
        Console.WriteLine("Dimension 1: {0}", bound1)

        ' Loop over all elements.
        For j As Integer = 1 To bound0
            For x As Integer = 1 To bound1
            Dim s1 As String = array(j, x)
            Console.Write(s1)
            Console.Write(" "c)
            Next
            Console.WriteLine()
        Next
        End If
    Next

    ' Close.
    w.Close()
    End Sub
End Module

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM