V4 V5

ReoGrid One は VB.NET からそのまま使えます。専用のパッケージはありません。 C# 向けのサンプルを VB に読み替えるときのポイントをまとめます。

プロジェクトを用意する

.vbproj に NuGet パッケージを追加します(インストール)。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net10.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <RootNamespace>MyApp</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="unvell.ReoGrid.One" Version="5.0.0" />
  </ItemGroup>
</Project>

WPF なら unvell.ReoGrid.One.Wpf、Avalonia なら unvell.ReoGrid.One.Avalonia、 画面なしなら unvell.ReoGrid.One.Core です。

Option Strict On でも問題なく使えます。以下のサンプルはすべて Option Strict On で通ることを確認しています。

初期化する(WinForms)

Imports System.Windows.Forms
Imports unvell.ReoGrid.Core
Imports unvell.ReoGrid.WinForms

Public Class MainForm
    Inherits Form

    Private WithEvents grid As ReoGridControl

    Public Sub New()
        Text = "ReoGrid One - VB.NET"
        Width = 1000
        Height = 700

        grid = New ReoGridControl() With {.Dock = DockStyle.Fill}
        Controls.Add(grid)
    End Sub

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' new ReoGridControl() は Sheet1 を 1 枚持った状態で始まる
        Dim ws As Worksheet = grid.ActiveWorksheet

        ws.SetText(0, 0, "商品")
        ws.SetText(0, 1, "単価")
        ws.SetText(0, 2, "数量")
        ws.SetText(0, 3, "金額")

        ws.SetText(1, 0, "ノート")
        ws.SetNumber(1, 1, 180)
        ws.SetNumber(1, 2, 12)
        ws.SetFormula(1, 3, "B2*C2")

        grid.SetFreeze(rows:=1, cols:=0)
    End Sub

    ' WithEvents + Handles でイベントを受けられる
    Private Sub grid_SelectionChanged(sender As Object, e As EventArgs) _
            Handles grid.SelectionChanged
        Text = $"{grid.ActiveCell.Row},{grid.ActiveCell.Col}"
    End Sub
End Class

Private WithEvents で宣言しておくと Handles が使えます。 デザイナを使う場合は、ツールボックスに ReoGridControl を追加して貼り付けても同じです。

画面を持たない処理(バッチ・サーバー)なら、コントロールは要りません。

Imports unvell.ReoGrid.Core

Dim wb As New Workbook()
Dim ws = wb.AddWorksheet("Sheet1")
ws.SetText(0, 0, "支店")

C# サンプルを VB へ読み替える

ドキュメントのサンプルは C# で書かれています。VB との違いは 5 か所だけです。

1. recordwith

C# の with に相当する構文が VB にはありません。MergeOver を使います。

' C#: ws.RootStyle = ws.RootStyle with { FontFamily = "Meiryo UI", FontSize = 11f };
ws.RootStyle = ws.RootStyle.MergeOver(
    New StyleRecord With {.FontFamily = "Meiryo UI", .FontSize = 11.0F})

MergeOver は「指定したものが勝ち、指定していないものは元を残す」なので、 with と同じ結果になります(スタイルの継承)。

新規に作る場合はオブジェクト初期化子がそのまま使えます。 StyleRecord のプロパティは init ですが、VB からも初期化子で設定できます。

ws.SetColumnStyle(1, New StyleRecord With {.TextAlign = HAlign.Right})

2. コレクション式

C# の ["A", "B"] は VB では配列リテラルです。

' C#: new DropdownCellType(["未着手", "作業中", "完了"], editable: false)
ws.SetCellType(1, 0, New DropdownCellType(
    New String() {"未着手", "作業中", "完了"}, editable:=False))

3. 名前付き引数

: ではなく := です。

ws.Cell("C3").SetCellType(New ProgressCellType(max:=100))
Dim small = wb.AddWorksheet("Log", rows:=5000, cols:=32)

4. ラムダ

値を返さないラムダは Sub()、返すものは Function() です。

' 履歴に載せる
ws.RecordCell("入力", 0, 0, Sub() ws.SetNumber(0, 0, 100))

' 実在するセルだけを走査する
ws.ReadWindow(0, 0, 10, 10,
    Sub(row As Integer, col As Integer, v As CellValue, styleId As Integer)
        Console.WriteLine(v.AsNumber)
    End Sub)

' 選択範囲の書式を変える(コントロール側)
grid.MutateSelectionStyle(Function(st) st.MergeOver(New StyleRecord With {.Bold = True}))

イベントを動的に足す場合は AddHandler です。

AddHandler grid.CellButtonClicked,
    Sub(s, e) MessageBox.Show($"{e.Cell.Row},{e.Cell.Col}")

5. out 引数

VB では変数を宣言してそのまま渡します。

' C#: if (ws.TryGetUsedRange(out var used)) { ... }
Dim used As RangePosition = Nothing
If ws.TryGetUsedRange(used) Then
    Console.WriteLine(used.Rows)
End If

' C#: string text = ws.GetFormattedText(0, 0, out uint? color);
Dim color As UInteger? = Nothing
Dim text = ws.GetFormattedText(0, 0, color)

インデクサ

C# の wb[0] / ws[2, 1] は、VB では括弧です。

Dim first As Worksheet = wb(0)
Dim named As Worksheet = wb("Sheet1")     ' 見つからなければ Nothing
Dim cur As CellCursor = ws(2, 1)

必要な Imports

名前空間主な型
unvell.ReoGrid.CoreWorkbook Worksheet RangePosition CellPosition
unvell.ReoGrid.Core.DataCellValue
unvell.ReoGrid.Core.StyleStyleRecord HAlign VAlign NumberFormatter
unvell.ReoGrid.Core.CellTypesCheckboxCellType ほか
unvell.ReoGrid.Core.IOReoGridJsonIO CsvIO
unvell.ReoGrid.Core.PrintingPrintSettings
unvell.ReoGrid.IO.ExcelXlsxReader XlsxWriter
unvell.ReoGrid.IO.PdfPdfExporter
unvell.ReoGrid.WinForms / .Wpf / .AvaloniaReoGridControl

日付を扱う

VB の DateDateTime と同じ型です。そのまま渡せます。

ws.SetObjectValue(3, 0, New Date(2026, 8, 19))
ws.SetNumberFormat(3, 0, "yyyy/mm/dd")

' 読み戻すときはシリアル値から変換する
Dim d As Date = Date.FromOADate(ws.GetValue(3, 0).AsNumber)

次に読む

ページの内容は役に立ちましたか?