AutofitColumnWidth メソッドを高速に実行する
対象製品
El Tabelle for .NET
詳細
シートの列の幅を自動調整する AutofitColumnWidth メソッドは、行数に比例して実行速度が低下します。特に、数千件を超えるデータを扱う場合、自動調整のための計算・オブジェクト生成も膨大なものとなり、著しいパフォーマンスの低下を確認できます。
これは文字列の描画範囲を厳密に計算する AutofitColumnWidth メソッドの仕様に基づく結果ですが、次のように文字幅の精度を緩和することで高速に同様の処理を実行できます。
[Visual Basic]
これは文字列の描画範囲を厳密に計算する AutofitColumnWidth メソッドの仕様に基づく結果ですが、次のように文字幅の精度を緩和することで高速に同様の処理を実行できます。
[Visual Basic]
'Sheet.AutofitColumnWidth()の高速化版
Public Sub AutofitColumnWidthFast(ByRef Target As GrapeCity.Win.ElTabelle.Sheet)
'制限事項: フォントサイズは常にシートのフォントが基準となります。(セル単位のフォントは無視されます)
' また、バイト数で長さを比較するため、データによっては若干の誤差が生じます。
Dim strDataArray()() As String
Dim intMaxLength As Integer
Dim intMaxRow As Integer
Const intCellMergin As Integer = 2 ''セルのマージンの補正値
'シートと同じサイズの多次元配列を作成する
strDataArray = New String(Target.MaxRows - 1)() {}
'対象列を選択する
Target.CellRange = New GrapeCity.Views.ElTabelle.Range("*:*")
'セルのテキストを一括取得する
strDataArray = Target.TextBlock
For j As Integer = 0 To Target.MaxColumns - 1
'ヘッダの文字サイズを格納する
intMaxLength = GetTextLengthAsByte(Target.ColumnHeaders(j).Caption)
intMaxRow = -1
For i As Integer = 0 To Target.MaxRows - 1
If GetTextLengthAsByte(strDataArray(i)(j)) > intMaxLength Then
intMaxLength = GetTextLengthAsByte(strDataArray(i)(j))
intMaxRow = i
End If
Next
If intMaxRow = -1 Then
'ヘッダのキャプションが最大だった場合
Target.Columns(j).Width = GetTextWidth(Target.ColumnHeaders(j).Caption, Target.Font) + intCellMergin
Else
Target.Columns(j).Width = GetTextWidth(Target(j, intMaxRow).Text, Target.Font) + intCellMergin
End If
Next
End Sub
'Sheet.AutofitColumnWidth(ByVal columnIndex As Integer)の高速化版
Public Sub AutofitColumnWidthFast(ByRef Target As GrapeCity.Win.ElTabelle.Sheet, ByVal columnIndex As Integer)
'制限事項: フォントサイズは常にシートのフォントが基準となります。(セル単位のフォントは無視されます)
' また、バイト数で長さを比較するため、データによっては若干の誤差が生じます。
Dim strDataArray()() As String
Dim intMaxLength As Integer
Dim intMaxRow As Integer
Const intCellMergin As Integer = 2 ''セルのマージンの補正値
'ヘッダの文字サイズを格納する
intMaxLength = GetTextLengthAsByte(Target.ColumnHeaders(columnIndex).Caption)
intMaxRow = -1
'シートと同じサイズの多次元配列を作成する
strDataArray = New String(Target.MaxRows - 1)() {}
'対象列を選択する
Target.CellRange = New GrapeCity.Views.ElTabelle.Range(columnIndex, 0, columnIndex, 0, GrapeCity.Views.ElTabelle.RangeStyle.Column)
'セルのテキストを一括取得する
strDataArray = Target.TextBlock
For i As Integer = 0 To Target.MaxRows - 1
If GetTextLengthAsByte(strDataArray(i)(0)) > intMaxLength Then
intMaxLength = GetTextLengthAsByte(strDataArray(i)(0))
intMaxRow = i
End If
Next
If intMaxRow = -1 Then
'ヘッダのキャプションが最大だった場合
Target.Columns(columnIndex).Width = GetTextWidth(Target.ColumnHeaders(columnIndex).Caption, Target.Font) + intCellMergin
Else
Target.Columns(columnIndex).Width = GetTextWidth(Target(columnIndex, intMaxRow).Text, Target.Font) + intCellMergin
End If
End Sub
'文字列の画面上の高さを取得する
Public Function GetTextHeight(ByVal Text As String, ByVal objFont As Font) As Integer
Return Graphics.FromImage(New Bitmap(1, 1)).MeasureString(Text, objFont).ToSize.Height()
End Function
'文字列の画面上の幅を取得する
Public Function GetTextWidth(ByVal Text As String, ByVal objFont As Font) As Integer
Return Graphics.FromImage(New Bitmap(1, 1)).MeasureString(Text, objFont).ToSize.Width()
End Function
'文字列のバイト数を取得する(文字数ではない)
Public Function GetTextLengthAsByte(ByVal Text As String) As Integer
Return System.Text.Encoding.GetEncoding("Shift-JIS").GetByteCount(Text)
End Function
Public Sub AutofitColumnWidthFast(ByRef Target As GrapeCity.Win.ElTabelle.Sheet)
'制限事項: フォントサイズは常にシートのフォントが基準となります。(セル単位のフォントは無視されます)
' また、バイト数で長さを比較するため、データによっては若干の誤差が生じます。
Dim strDataArray()() As String
Dim intMaxLength As Integer
Dim intMaxRow As Integer
Const intCellMergin As Integer = 2 ''セルのマージンの補正値
'シートと同じサイズの多次元配列を作成する
strDataArray = New String(Target.MaxRows - 1)() {}
'対象列を選択する
Target.CellRange = New GrapeCity.Views.ElTabelle.Range("*:*")
'セルのテキストを一括取得する
strDataArray = Target.TextBlock
For j As Integer = 0 To Target.MaxColumns - 1
'ヘッダの文字サイズを格納する
intMaxLength = GetTextLengthAsByte(Target.ColumnHeaders(j).Caption)
intMaxRow = -1
For i As Integer = 0 To Target.MaxRows - 1
If GetTextLengthAsByte(strDataArray(i)(j)) > intMaxLength Then
intMaxLength = GetTextLengthAsByte(strDataArray(i)(j))
intMaxRow = i
End If
Next
If intMaxRow = -1 Then
'ヘッダのキャプションが最大だった場合
Target.Columns(j).Width = GetTextWidth(Target.ColumnHeaders(j).Caption, Target.Font) + intCellMergin
Else
Target.Columns(j).Width = GetTextWidth(Target(j, intMaxRow).Text, Target.Font) + intCellMergin
End If
Next
End Sub
'Sheet.AutofitColumnWidth(ByVal columnIndex As Integer)の高速化版
Public Sub AutofitColumnWidthFast(ByRef Target As GrapeCity.Win.ElTabelle.Sheet, ByVal columnIndex As Integer)
'制限事項: フォントサイズは常にシートのフォントが基準となります。(セル単位のフォントは無視されます)
' また、バイト数で長さを比較するため、データによっては若干の誤差が生じます。
Dim strDataArray()() As String
Dim intMaxLength As Integer
Dim intMaxRow As Integer
Const intCellMergin As Integer = 2 ''セルのマージンの補正値
'ヘッダの文字サイズを格納する
intMaxLength = GetTextLengthAsByte(Target.ColumnHeaders(columnIndex).Caption)
intMaxRow = -1
'シートと同じサイズの多次元配列を作成する
strDataArray = New String(Target.MaxRows - 1)() {}
'対象列を選択する
Target.CellRange = New GrapeCity.Views.ElTabelle.Range(columnIndex, 0, columnIndex, 0, GrapeCity.Views.ElTabelle.RangeStyle.Column)
'セルのテキストを一括取得する
strDataArray = Target.TextBlock
For i As Integer = 0 To Target.MaxRows - 1
If GetTextLengthAsByte(strDataArray(i)(0)) > intMaxLength Then
intMaxLength = GetTextLengthAsByte(strDataArray(i)(0))
intMaxRow = i
End If
Next
If intMaxRow = -1 Then
'ヘッダのキャプションが最大だった場合
Target.Columns(columnIndex).Width = GetTextWidth(Target.ColumnHeaders(columnIndex).Caption, Target.Font) + intCellMergin
Else
Target.Columns(columnIndex).Width = GetTextWidth(Target(columnIndex, intMaxRow).Text, Target.Font) + intCellMergin
End If
End Sub
'文字列の画面上の高さを取得する
Public Function GetTextHeight(ByVal Text As String, ByVal objFont As Font) As Integer
Return Graphics.FromImage(New Bitmap(1, 1)).MeasureString(Text, objFont).ToSize.Height()
End Function
'文字列の画面上の幅を取得する
Public Function GetTextWidth(ByVal Text As String, ByVal objFont As Font) As Integer
Return Graphics.FromImage(New Bitmap(1, 1)).MeasureString(Text, objFont).ToSize.Width()
End Function
'文字列のバイト数を取得する(文字数ではない)
Public Function GetTextLengthAsByte(ByVal Text As String) As Integer
Return System.Text.Encoding.GetEncoding("Shift-JIS").GetByteCount(Text)
End Function
キーワード
HowTo
この文書は、以前は次のFAQ IDで公開されていました : 4483