投稿日 | : 2003/01/23(Thu) 16:08 |
投稿者 | : A221 |
Eメール | : |
URL | : |
タイトル | : Re: 質問!エクセルデータをMSFlexGrid上に |
もともとはエクセルのシートを読んできてそのとおりに再現したいという
要望で作成したもので、何も考えずにべたっとはっています。
'------------------------------------------------------
'必要コントロール Text1 Msflexgrid1 Command1
'------------------------------------------------------
Option Explicit
'エクセルのシートを読む。色付きセル再生。
Private xlApp As New Excel.Application
Private xlbook As New Excel.Workbook
Private xlsheet As New Excel.Worksheet
Private RowRange As Long
Private Colrange As Long
Private fpath As String
Private tmpname As String
Private Sub Command1_Click()
'TEXTに入力された名前で読み込み
Dim xlRow As Long
Dim xlCol As Long
fpath = App.Path 'アプリケーションと同じパスなら。任意なら"C:\〜"
tmpname = Text1.Text 'コモンダイアログで拾うほうがいいかも
'事前バインド
Set xlApp = CreateObject("Excel.Application")
Set xlbook = xlApp.Workbooks.Open(fpath & tmpname)
'Set xlbook = xlApp.Workbooks.Open("C:\test.xls")
'シートの番号
Set xlsheet = xlbook.Worksheets(1)
'MSFLEXGRIDは(0,0)から始まるがEXCELは(1,1)から始まる
RowRange = xlsheet.UsedRange.Rows.Count - 1
Colrange = xlsheet.UsedRange.Columns.Count - 1
On Error Resume Next
'高速化処理・描画対象を消しとく
With MSFlexGrid1
.Visible = False
.Rows = RowRange + 1
.Cols = Colrange + 1
For xlRow = 1 To RowRange + 1
For xlCol = 1 To Colrange + 1
.TextMatrix(xlRow - 1, xlCol - 1) = xlsheet.Cells(xlRow, xlCol).Value
'色替えがなければ、これで可能。
' .Row = xlRow
' .Col = xlCol
' .CellBackColor = xlsheet.Cells(xlRow, xlCol).Interior.Color
Next xlCol
Next xlRow
.Visible = True
End With
End Sub
Private Sub Form_Terminate()
'フォームが閉じる時、開放
Set xlsheet = Nothing
Set xlbook = Nothing
Set xlApp = Nothing
End Sub