/
PictureActions.vb
132 lines (112 loc) · 6.1 KB
/
PictureActions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Imports System
Imports System.Drawing
Imports System.Globalization
Imports System.IO
Imports DevExpress.Export.Xl
Namespace XLExportExamples
Public NotInheritable Class PictureActions
Private Sub New()
End Sub
Private Shared imagesPath As String = Path.Combine(System.Windows.Forms.Application.StartupPath, "Images")
#Region "Actions"
Public Shared InsertPictureAction As Action(Of Stream, XlDocumentFormat) = AddressOf InsertPicture
Public Shared StretchPictureAction As Action(Of Stream, XlDocumentFormat) = AddressOf StretchPicture
Public Shared FitPictureAction As Action(Of Stream, XlDocumentFormat) = AddressOf FitPicture
Public Shared PictureHyperlinkClickAction As Action(Of Stream, XlDocumentFormat) = AddressOf PictureHyperlinkClick
#End Region
Private Shared Sub InsertPicture(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' #Region "#InsertPicture"
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Insert a picture from a file and anchor it to cells.
Using picture As IXlPicture = sheet.CreatePicture()
picture.Image = Image.FromFile(Path.Combine(imagesPath, "image1.jpg"))
picture.SetTwoCellAnchor(New XlAnchorPoint(1, 1, 0, 0), New XlAnchorPoint(6, 11, 2, 15), XlAnchorType.TwoCell)
End Using
End Using
' #End Region ' #InsertPicture
End Using
End Sub
Private Shared Sub StretchPicture(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
sheet.SkipColumns(1)
' Create the column "B" and set its width.
Using column As IXlColumn = sheet.CreateColumn()
column.WidthInPixels = 205
End Using
sheet.SkipRows(1)
' Create the second row and set its height.
Using row As IXlRow = sheet.CreateRow()
row.HeightInPixels = 154
End Using
' #Region "#StretchPicture"
' Insert a picture from a file and stretch it to fill the cell B2.
Using picture As IXlPicture = sheet.CreatePicture()
picture.Image = Image.FromFile(Path.Combine(imagesPath, "image1.jpg"))
picture.StretchToCell(New XlCellPosition(1, 1))
End Using
End Using
' #End Region ' #StretchPicture
End Using
End Sub
Private Shared Sub FitPicture(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
sheet.SkipColumns(1)
' Create the column "B" and set its width.
Using column As IXlColumn = sheet.CreateColumn()
column.WidthInPixels = 300
End Using
sheet.SkipRows(1)
' Create the second row and set its height.
Using row As IXlRow = sheet.CreateRow()
row.HeightInPixels = 154
End Using
' #Region "#FitPicture"
' Insert a picture from a file to fit in the cell B2.
Using picture As IXlPicture = sheet.CreatePicture()
picture.Image = Image.FromFile(Path.Combine(imagesPath, "image1.jpg"))
picture.FitToCell(New XlCellPosition(1, 1), 300, 154, True)
End Using
End Using
' #End Region ' #FitPicture
End Using
End Sub
Private Shared Sub PictureHyperlinkClick(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' #Region "#HyperlinkClick"
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Load a picture from a file and add a hyperlink to it.
Using picture As IXlPicture = sheet.CreatePicture()
picture.Image = Image.FromFile(Path.Combine(imagesPath, "DevExpress.png"))
picture.HyperlinkClick.TargetUri = "http://www.devexpress.com"
picture.HyperlinkClick.Tooltip = "Developer Express Inc."
picture.SetTwoCellAnchor(New XlAnchorPoint(1, 1, 0, 0), New XlAnchorPoint(10, 5, 2, 15), XlAnchorType.TwoCell)
End Using
End Using
End Using
' #End Region ' #HyperlinkClick
End Sub
End Class
End Namespace