データグリービュー内のデータをExcelワークブックにエクスポートするボタンが付いたフォームがあります。DatagridViewをExcelにエクスポートする方法
私は動作させましたが、データを追加しません。各列のヘッダーのみ。また、ID列を無視するように指示する方法もわかりません。
私は100回のように、このコードの上に見てきたと私は障害に
UPDATEを見つけることができません:私は、データのいくつかの行を入力したときに動作するようですが、それはのDataGridViewにデータの最初の行を無視します。
コード:あなたはあなたのコードは、C#で** **ない
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles
PictureBox2.Click
ExportToExcel()
End Sub
Private Sub ExportToExcel()
' Creating a Excel object.
Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
Try
worksheet = workbook.ActiveSheet
worksheet.Name = "ExportedFromDatGrid"
Dim cellRowIndex As Integer = 1
Dim cellColumnIndex As Integer = 1
'Loop through each row and read value from each column.
For i As Integer = 0 To FleetDataGridView.Rows.Count - 2
For j As Integer = 0 To FleetDataGridView.Columns.Count - 1
' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
If cellRowIndex = 1 Then
worksheet.Cells(cellRowIndex, cellColumnIndex) = FleetDataGridView.Columns(j).HeaderText
Else
worksheet.Cells(cellRowIndex, cellColumnIndex) = FleetDataGridView.Rows(i).Cells(j).Value.ToString()
End If
cellColumnIndex += 1
Next
cellColumnIndex = 1
cellRowIndex += 1
Next
'Getting the location and file name of the excel to save from user.
Dim saveDialog As New SaveFileDialog()
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
saveDialog.FilterIndex = 2
If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
workbook.SaveAs(saveDialog.FileName)
MessageBox.Show("Export Successful")
End If
Catch ex As System.Exception
MessageBox.Show(ex.Message)
Finally
excel.Quit()
workbook = Nothing
excel = Nothing
End Try
End Sub
を書くのを知っていますか?これはC#とどのように関連していますか? –
私の講師は、これはビジュアルスタジオ内のC#だと教えてくれましたか?またはこのVB.netですか? – RedZ
私は間違いを犯した場合、直ちに質問を変更します。申し訳ありません@SirRufo – RedZ