VB.NETであるプログラムを作成しています。
DBと接続してデータグリッドビューにテーブルの情報を表示しています。
そこで、出力ボタンを押下した際にデータグリッドビューに表示しているデータをExcelに入力できるようにしたいです。
'出力ボタン Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim app As Excel.Application = Nothing Dim book As Excel.Workbook = Nothing Dim sheet As Excel.Worksheet = Nothing Try app = New Excel.Application() app = CreateObject("Excel.Application") book = app.Workbooks.Open("C:\form3\Book1.xlsx") sheet = book.Worksheets(1) 'For i As Integer = 0 To DataGridView1.Rows.Count - 1 'For x As Integer = 0 To DataGridView1.ColumnCount - 1 sheet.Range("A2").Value = "aaa" sheet.Range("B2").Value = "ab" sheet.Range("C2").Value = "abc" sheet.Range("D2").Value = "a" sheet.Range("E2").Value = "a" 'Next 'Next app.Visible = True System.Threading.Thread.Sleep(5000) book.Save() app.Quit() Catch ex As Exception Throw ex Finally Marshal.ReleaseComObject(sheet) Marshal.ReleaseComObject(book) Marshal.ReleaseComObject(app) End Try End Sub
一応今はこれでExcelにベタ書きで文字を入力することはできています。
For i As Integer = 0 To DataGridView1.Rows.Count - 1 sheet.Range(1, i).Value = DataGridView1.Rows(i).Cells(1).Value sheet.Range(2, i).Value = DataGridView1.Rows(i).Cells(4).Value sheet.Range(3, i).Value = DataGridView1.Rows(i).Cells(5).Value sheet.Range(4, i).Value = DataGridView1.Rows(i).Cells(6).Value sheet.Range(5, i).Value = DataGridView1.Rows(i).Cells(7).Value Next
Excelの方に一行文だけ入力できました。
後はExcelの方でA2,A3,A4,A5...と各セルを下にずらしていきたいのですがそのやり方がわかりません。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/09 07:27