-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.vb
64 lines (53 loc) · 2.49 KB
/
Form1.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Imports DevExpress.Spreadsheet
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Namespace DocServerExternalWorkbookSample
Public Partial Class Form1
Inherits Form
Private myWorkbook As Workbook = New Workbook()
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
#Region "#addexternalworkbook"
Dim externalWorkbook As Workbook = New Workbook()
externalWorkbook.Options.Save.CurrentFileName = "ExternalDocument.xlsx"
' Check whether the external workbook is already referenced.
For Each item As IWorkbook In myWorkbook.ExternalWorkbooks
If Equals(item.Options.Save.CurrentFileName, externalWorkbook.Options.Save.CurrentFileName) Then Return
Next
externalWorkbook.Worksheets(0).Import(CreateDataTable(10), False, 0, 0)
externalWorkbook.SaveDocument("ExternalDocument.xlsx")
myWorkbook.ExternalWorkbooks.Add(externalWorkbook)
#End Region ' #addexternalworkbook
button1.Enabled = Not button1.Enabled
End Sub
Private Function CreateDataTable(ByVal rowCount As Integer) As DataTable
Dim someDT As DataTable = New DataTable()
For i As Integer = 0 To 5 - 1
someDT.Columns.Add("Value" & i.ToString(), GetType(Integer))
Next
Dim myRand As Random = New Random()
For i As Integer = 0 To rowCount - 1
someDT.Rows.Add(myRand.Next(1, 100), myRand.Next(1, 100), myRand.Next(1, 100), myRand.Next(1, 100), myRand.Next(1, 100))
Next
Return someDT
End Function
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
#Region "#insertexternalreference"
If myWorkbook.ExternalWorkbooks.Count = 0 Then
Return
End If
Dim extWorkbook As IWorkbook = CType(myWorkbook.ExternalWorkbooks(0), IWorkbook)
Dim extWorkbookName As String = extWorkbook.Options.Save.CurrentFileName
Dim sFormula As String = String.Format("=[{0}]Sheet1!A1", extWorkbookName)
myWorkbook.Worksheets(0).Cells("A1").Formula = sFormula
myWorkbook.SaveDocument("Test.xlsx")
System.Diagnostics.Process.Start("Test.xlsx")
#End Region ' #insertexternalreference
End Sub
End Class
End Namespace