2015-07-12 61 views
0

当我导出一个datagridview时,我需要一个带有额外标题的导出excel文件,如'语句报告'。我怎样才能做到这一点 ?我正在成功导出excel文件。但是我需要在Excel表格顶部添加诸如公司名称,地址,报告名称等标题。当我将一个datagridview导入excel时插入额外的标题

在此先感谢..

我的代码如下

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 

enter image description here

+0

你应该probaply留下一些空白行在页面的顶部,然后对其进行编辑,以显示你想要什么 – user3980820

回答

0

给出尝试,如果这对你的作品。 编辑:

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 
+0

其worked..Thank你 –