Vb.net 将datagridview导出到excel时插入额外标题
当我导出datagridview时,我需要一个带有额外标题的导出excel文件,如“报表报告”。我该怎么做?我正在成功获取导出的excel文件。但我需要额外的标题,如公司名称,地址,报告名称,在excel表的顶部等 提前谢谢 我的代码如下Vb.net 将datagridview导出到excel时插入额外标题,vb.net,Vb.net,当我导出datagridview时,我需要一个带有额外标题的导出excel文件,如“报表报告”。我该怎么做?我正在成功获取导出的excel文件。但我需要额外的标题,如公司名称,地址,报告名称,在excel表的顶部等 提前谢谢 我的代码如下 Private Sub ExportToExcel() Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlBook As Microsoft.Offic
Private Sub ExportToExcel()
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim oValue As Object = System.Reflection.Missing.Value
Dim sPath As String = String.Empty
Dim dlgSave As New SaveFileDialog
Dim dt As New DataTable
dlgSave.DefaultExt = "xls"
dlgSave.Filter = "Microsoft Excel|*.xls"
dlgSave.InitialDirectory = Application.StartupPath
If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
xlApp = New Microsoft.Office.Interop.Excel.Application
xlBook = xlApp.Workbooks.Add(oValue)
xlSheet = xlBook.Worksheets("sheet1")
Dim xlRow As Long = 2
Dim xlCol As Short = 1
For k As Integer = 0 To DGVStatement.ColumnCount - 1
xlSheet.Cells(1, xlCol) = DGVStatement(k, 0).Value
xlCol += 1
Next
Me.ProgressBar1.Visible = True
Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Maximum = DGVStatement.Rows.Count
For M = 0 To DGVStatement.RowCount - 2
For N = 0 To DGVStatement.ColumnCount - 1
For P As Integer = 1 To DGVStatement.Columns.Count
xlSheet.Cells(1, P) = DGVStatement.Columns(P - 1).HeaderText
xlSheet.Cells(M + 2, N + 1) = DGVStatement(N, M).Value.ToString()
Next
Next
Next
xlSheet.Columns.AutoFit()
Dim sFileName As String = Replace(dlgSave.FileName, ".xlsx", "xlx")
xlSheet.SaveAs(sFileName)
xlBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlBook)
releaseObject(xlSheet)
Me.ProgressBar1.Value = 0
Me.ProgressBar1.Visible = False
MsgBox("Data successfully exported.", MsgBoxStyle.Information, "PRMS/SOB Date Tagging")
Catch
MsgBox(ErrorToString)
Finally
End Try
End If
End Sub
如果这对你有效,试试看已编辑:
Private Sub ExportToExcel()
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim oValue As Object = System.Reflection.Missing.Value
Dim sPath As String = String.Empty
Dim dlgSave As New SaveFileDialog
Dim dt As New DataTable
dlgSave.DefaultExt = "xls"
dlgSave.Filter = "Microsoft Excel|*.xls"
dlgSave.InitialDirectory = Application.StartupPath
If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
xlApp = New Microsoft.Office.Interop.Excel.Application
xlBook = xlApp.Workbooks.Add(oValue)
xlSheet = xlBook.Worksheets("sheet1")
Dim xlRow As Long = 2
Dim xlCol As Short = 1
'Header information
xlSheet.Cells(1, 3) = "ABC EXPORTS AND IMPORTS"
xlSheet.Cells(2, 3) = "STATEMENT REPORT"
For k As Integer = 0 To DGVStatement.ColumnCount - 1
xlSheet.Cells(4, xlCol) = DGVStatement(k, 0).Value
xlCol += 1
Next
Me.ProgressBar1.Visible = True
Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Maximum = DGVStatement.Rows.Count
For M = 0 To DGVStatement.RowCount - 2
For N = 0 To DGVStatement.ColumnCount - 1
For P As Integer = 1 To DGVStatement.Columns.Count
xlSheet.Cells(4, P) = DGVStatement.Columns(P - 1).HeaderText
xlSheet.Cells(M + 5, N + 1) = DGVStatement(N, M).Value.ToString()
Next
Next
Next
xlSheet.Columns.AutoFit()
Dim sFileName As String = Replace(dlgSave.FileName, ".xlsx", "xlx")
xlSheet.SaveAs(sFileName)
xlBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlBook)
releaseObject(xlSheet)
Me.ProgressBar1.Value = 0
Me.ProgressBar1.Visible = False
MsgBox("Data successfully exported.", MsgBoxStyle.Information, "PRMS/SOB Date Tagging")
Catch
MsgBox(ErrorToString)
Finally
End Try
End If
End Sub
您应该在页面顶部保留一些空白行,然后编辑它们以显示所需内容