在以下代码上,我将DataGridViews导出到Excel工作表。但是我的一些专栏文章是DataGridViewComboBoxCell,并且具有该单元格的特定项目列表。我想定义它们并将它们写在excel上作为数据验证列表。
Public Shared Sub NewMethod(ByVal dgvPa As DataGridView, ByVal dgvHp As DataGridView, ByVal dgvMp As DataGridView, ByVal dgvFe As DataGridView)
Dim sfd As SaveFileDialog = frmMain.sfdSaveFile
sfd.Filter = "Excel |*.xlsx"
sfd.Title = "Save Info Sheet File"
sfd.ShowDialog()
If sfd.FileName IsNot "" Then
Dim xlApp As New Excel.Application
If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!")
Else
Dim xlBook As Excel.Workbook = xlApp.Workbooks.Add()
xlBook.SaveAs(sfd.FileName)
xlBook = xlApp.Workbooks.Open(sfd.FileName)
Dim xlSheetPa As Excel.Worksheet = CType(xlBook.ActiveSheet, Excel.Worksheet)
xlSheetPa.Name = My.Settings.str_infosheet__tabname_pa
For i = 1 To dgvPa.ColumnCount
xlSheetPa.Cells(1, i) = dgvPa.Columns(i - 1).HeaderText
Next
For i = 0 To dgvPa.ColumnCount - 1
For j = 0 To dgvPa.Rows.Count - 1
xlSheetPa.Cells(j + 2, i + 1) = dgvPa.Rows(j).Cells(i).Value
' Dim xlRng As Excel.Range = CType(xlSheetPa.Cells(j + 2, i + 1), Excel.Range)
' With xlRng.Validation
' .Add(Type:=Excel.XlDVType.xlValidateList,
'AlertStyle:=Excel.XlDVAlertStyle.xlValidAlertStop,
'Operator:=Excel.XlFormatConditionOperator.xlBetween,
'Formula1:="<items of combobox will listed>")
' .IgnoreBlank = True
' .InCellDropdown = True
' End With
' With xlRng
' .Value = dgvPa.Rows(j).Cells(i).Value
' End With
Next
Next
xlBook.Save()
xlBook.Close()
xlApp.Quit()
Marshal.ReleaseComObject(xlBook)
Marshal.ReleaseComObject(xlApp)
dgvPa.Rows.Clear()
dgvHp.Rows.Clear()
dgvMp.Rows.Clear()
dgvFe.Rows.Clear()
End If
End If
End Sub
在这里,我尝试使用以下代码添加此DataGridViewComboboxCell项目列表;
' Dim xlRng As Excel.Range = CType(xlSheetPa.Cells(j + 2, i + 1), Excel.Range)
' With xlRng.Validation
' .Add(Type:=Excel.XlDVType.xlValidateList,
'AlertStyle:=Excel.XlDVAlertStyle.xlValidAlertStop,
'Operator:=Excel.XlFormatConditionOperator.xlBetween,
'Formula1:="<items of combobox will listed>")
' .IgnoreBlank = True
' .InCellDropdown = True
' End With
' With xlRng
' .Value = dgvPa.Rows(j).Cells(i).Value
' End With
但是我的问题是我如何才能将comboboxcell项作为数组或其他格式获得?之后,如何在下面的部分中合并此代码。
For i = 0 To dgvPa.ColumnCount - 1
For j = 0 To dgvPa.Rows.Count - 1
xlSheetPa.Cells(j + 2, i + 1) = dgvPa.Rows(j).Cells(i).Value
Next
Next