我在匯出串列時遇到問題。我想將 DataGridView 中的多個選定行匯出到 Excel。我在 DataGridView 中添加了一列 chkCheck,另一列是 columncheckbox 我只使用 SQL Server

這是圖片。我可能有 10 行資料,但我只想選擇其中幾行進行匯出
這是我的代碼:
Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As New Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim excelLocation As String = ""
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
excelLocation = FolderBrowserDialog1.SelectedPath & "\Export-" & Format(Now(), "MMMM-dd-yyyy_hh-mm-ss") & ".xlsx"
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets(1)
'Export Header Names Start
Dim columnsCount As Integer = DataGridView1.Columns.Count - 6
For k As Integer = 1 To DataGridView1.Columns.Count - 6
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
Next
'Export Header Name End
'Export Each Row Start
Dim i As Integer = 0
For Each selectedrow In DataGridView1.SelectedRows
Dim columnIndex As Integer = 0
Do Until columnIndex = columnsCount
xlWorkSheet.Cells(i 2, columnIndex 1).Value = selectedrow.Cells(columnIndex).Value.ToString
columnIndex = 1
Loop
i = 1
Next
'Export Each Row End
xlWorkSheet.SaveAs(excelLocation)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Else
MsgBox("Save have been canceled", MsgBoxStyle.Critical, "Warning")
End If
End Sub
uj5u.com熱心網友回復:
添加參考 Microsoft.office.Interop.Excel
Private Sub Export_Click(sender As Object, e As EventArgs) 處理Button1.Click
Using sfd As SaveFileDialog = New SaveFileDialog()
sfd.Filter = "Excel Workbook|*.xls"
sfd.ValidateNames = True
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim rowstotal, colstotal, i, j, k As Integer
Dim excelapp As New Excel.Application
Try
Dim excelbook As Excel.Workbook = excelapp.Workbooks.Add
Dim excelworksheet As Excel.Worksheet = CType(excelbook.Worksheets(1), Excel.Worksheet)
rowstotal = DataGridView1.RowCount
colstotal = DataGridView1.Columns.Count - 1
With excelworksheet
For k = 0 To colstotal
.Cells(1, k 1).value = DataGridView1.Columns(k).HeaderText
Next
For i = 0 To rowstotal - 1 ' rows details
Dim selectcl As Integer = Convert.ToInt16(DataGridView1.Rows(i).Cells("chkcolumn").Value)
If selectcl = 1 Then
For j = 0 To colstotal
.Cells(i 2, j 1).value = DataGridView1.Rows(i).Cells(j).Value
Next j
End If
Next i
.Rows("1:1").font.fontstyle = "Bold"
.Rows("1:1").font.size = 12
End With
excelbook.SaveAs(sfd.FileName)
excelapp.Quit()
MessageBox.Show("Exported Sucessfully")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Using
End Sub
uj5u.com熱心網友回復:
'Export Each Row Start
Dim i As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
Dim checkselect As Integer = Convert.ToInt16(row.Cells("chkCheck").Value)
If checkselect = 1 Then
For columnIndex As Integer = 1 To columnsCount - 1
xlWorkSheet.Cells(i 2, columnIndex 1).Value = row.Cells(columnIndex).Value.ToString
Next
i = 1
End If
Next
'Export Each Row End
這是我嘗試匯出每一行部分的代碼..感謝您的幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323636.html
上一篇:根據多個標準回傳值
