GrapeCity.Documents.Excel が非常に便利だったので覚え書き
マップのテキストボックスに背景色とハイパーリンクを付ける12345678910111213141516171819202122232425262728293031323334353637383940414243444546 Imports GrapeCity.Documents.ExcelImports GrapeCity.Documents.Excel.DrawingImports System.Drawing Module Module1 Sub Main() ' Excelワークブックを作成 Dim workbook As New Workbook() ' 既存のExcelファイルを開く workbook.Open("f:\エクセルテスト\maptest.xlsx") ' アクティブシートを取得 Dim sheet As IWorksheet = workbook.Worksheets(0) ' すべての図形を取得 Dim shapes = sheet.Shapes ' 特定のテキストボックスの名前を指定 Dim targetBochiKukaku As String = "あ1" ' テキストボックスを検索⇒テキストボックスのNameで検索 For Each shape As IShape In sheet.Shapes If shape.Name = targetBochiKukaku Then ' 背景色を赤に設定 shape.Fill.Solid() shape.Fill.Color.RGB = Color.Red 'ハイパーリンクを設定 ' If shape.AutoShapeType = AutoShapeType.TextBox Then Dim hyperlinkUrl As String = $"https://xxx.com/?xxx={shape.Name}" sheet.Hyperlinks.Add(shape, hyperlinkUrl) ' End If End If Next ' PDFとして保存 Dim pdfFilePath As String = "f:\エクセルテスト\output.pdf" workbook.Save(pdfFilePath, SaveFileFormat.Pdf) ' PDFファイルを開く Process.Start(New ProcessStartInfo(pdfFilePath) With {.UseShellExecute = True}) 'Console.WriteLine("テキストボックスの背景色を赤に変更して保存しました。") End SubEnd Module
テキストボックスのNameではなく、中身(TextRange.text)で検索
123456789101112 '' テキストボックスを検索⇒テキストボックスの入力されている値を検索 'For Each shape As IShape In shapes ' If shape.TextFrame IsNot Nothing Then ' Dim textContent As String = shape.TextFrame.TextRange.Text ' If textContent.Contains(targetBochiKukaku) Then ' ' 背景色を赤に設定 ' shape.Fill.Solid() ' shape.Fill.Color.RGB = Color.Red ' End If ' End If 'Next
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 | Imports GrapeCity.Documents.Excel Imports GrapeCity.Documents.Excel.Drawing Imports System.Drawing Module Module1 Sub Main() ' Excelワークブックを作成 Dim workbook As New Workbook() ' 既存のExcelファイルを開く workbook.Open("f:\エクセルテスト\maptest.xlsx") ' アクティブシートを取得 Dim sheet As IWorksheet = workbook.Worksheets(0) ' すべての図形を取得 Dim shapes = sheet.Shapes ' 特定のテキストボックスの名前を指定 Dim targetBochiKukaku As String = "あ1" ' テキストボックスを検索⇒テキストボックスのNameで検索 For Each shape As IShape In sheet.Shapes If shape.Name = targetBochiKukaku Then ' 背景色を赤に設定 shape.Fill.Solid() shape.Fill.Color.RGB = Color.Red 'ハイパーリンクを設定 ' If shape.AutoShapeType = AutoShapeType.TextBox Then Dim hyperlinkUrl As String = $"https://xxx.com/?xxx={shape.Name}" sheet.Hyperlinks.Add(shape, hyperlinkUrl) ' End If End If Next ' PDFとして保存 Dim pdfFilePath As String = "f:\エクセルテスト\output.pdf" workbook.Save(pdfFilePath, SaveFileFormat.Pdf) ' PDFファイルを開く Process.Start(New ProcessStartInfo(pdfFilePath) With {.UseShellExecute = True}) 'Console.WriteLine("テキストボックスの背景色を赤に変更して保存しました。") End Sub End Module |
テキストボックスのNameではなく、中身(TextRange.text)で検索
1 2 3 4 5 6 7 8 9 10 11 12 | '' テキストボックスを検索⇒テキストボックスの入力されている値を検索 'For Each shape As IShape In shapes ' If shape.TextFrame IsNot Nothing Then ' Dim textContent As String = shape.TextFrame.TextRange.Text ' If textContent.Contains(targetBochiKukaku) Then ' ' 背景色を赤に設定 ' shape.Fill.Solid() ' shape.Fill.Color.RGB = Color.Red ' End If ' End If 'Next |