-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
73 lines (67 loc) · 2.53 KB
/
Form1.cs
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
65
66
67
68
69
70
71
72
73
using DevExpress.Spreadsheet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DocServerExternalWorkbookSample
{
public partial class Form1 : Form
{
Workbook myWorkbook = new Workbook();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
#region #addexternalworkbook
Workbook externalWorkbook = new Workbook();
externalWorkbook.Options.Save.CurrentFileName = "ExternalDocument.xlsx";
// Check whether the external workbook is already referenced.
foreach (IWorkbook item in myWorkbook.ExternalWorkbooks)
{
if (item.Options.Save.CurrentFileName == externalWorkbook.Options.Save.CurrentFileName)
return;
}
externalWorkbook.Worksheets[0].Import(CreateDataTable(10), false, 0, 0);
externalWorkbook.SaveDocument("ExternalDocument.xlsx");
myWorkbook.ExternalWorkbooks.Add(externalWorkbook);
#endregion #addexternalworkbook
button1.Enabled = !button1.Enabled;
}
DataTable CreateDataTable(int rowCount)
{
DataTable someDT = new DataTable();
for (int i = 0; i < 5; i++)
{
someDT.Columns.Add("Value" + i.ToString(), typeof(int));
}
Random myRand = new Random();
for (int i = 0; i < rowCount; i++)
{
someDT.Rows.Add(myRand.Next(1, 100), myRand.Next(1, 100), myRand.Next(1, 100), myRand.Next(1, 100), myRand.Next(1, 100));
}
return someDT;
}
private void button2_Click(object sender, EventArgs e)
{
#region #insertexternalreference
if (myWorkbook.ExternalWorkbooks.Count == 0)
{
return;
}
IWorkbook extWorkbook = (IWorkbook)myWorkbook.ExternalWorkbooks[0];
string extWorkbookName = extWorkbook.Options.Save.CurrentFileName;
string sFormula = String.Format("=[{0}]Sheet1!A1", extWorkbookName);
myWorkbook.Worksheets[0].Cells["A1"].Formula = sFormula;
myWorkbook.SaveDocument("Test.xlsx");
System.Diagnostics.Process.Start("Test.xlsx");
#endregion #insertexternalreference
}
}
}