2013-11-20 112 views
0

在我的datagridview我有第一列visible = false。我需要此列不導出到Excel導出datagridview列excel

我有以下代碼導出到Excel:

If Sfd.ShowDialog() = DialogResult.OK Then 

      Dim App As New Excel.Application 
      Dim WB As Excel.Workbook 
      Dim WS As New Excel.Worksheet 

      WB = App.Workbooks.Add() 

      WS = WB.ActiveSheet 

      For i As Integer = 1 To DG.Columns.Count 
       WS.Cells(1, i) = DG.Columns(i - 1).HeaderText 
      Next 

      For i As Integer = 0 To DG.Rows.Count - 1 
       For j As Integer = 0 To DG.Columns.Count - 1 
        WS.Cells(i + 2, j + 1) = DG.Rows(i).Cells(j).Value.ToString() 
        WS.Cells(i + 2, 1).Font.Color = Color.Blue 
       Next 
      Next 

      With WS 
       With .Range(.Cells(1, 1), .Cells(1, DG.ColumnCount)).Font 
        .Color = Color.White 
        .Bold = 1 
        .Size = 12 
       End With 
       .Range(.Cells(1, 1), .Cells(1, DG.ColumnCount)).Interior.Color = Color.Black 
       .Columns.AutoFit() 
       .Columns.HorizontalAlignment = 2 
      End With 

      WB.SaveAs(Sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal) 

      WB.Close() 
      Process.Start(Sfd.FileName) 
     End If 

感謝

回答

0

您好我有同樣的問題試試下面的代碼在我的應用程序 的偉大工程,這是我抄我的代碼鏈接 Exporting displayed columns in dataGridView to Excel

' creating Excel Application 

     If ((dgSMS.Columns.Count = 0) Or (dgSMS.Rows.Count = 0)) Then Exit Sub 
     Dim XlApp = New Excel.Application With {.Visible = True} 
     XlApp.Workbooks.Add(Excel.XlSheetType.xlWorksheet) 
     Dim xlWS = XlApp.ActiveSheet 
     xlWS.Name = "Exported Data" 

     'Copy visible data from DGV to Excel 
     Dim columnCollection As DataGridViewColumnCollection = dgSMS.Columns 
     Dim currentVisibleColumn As DataGridViewColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible) 
     Dim lastColumnExported As DataGridViewColumn = currentVisibleColumn 
     Dim visibleColumnCount As Integer = columnCollection.GetColumnCount(DataGridViewElementStates.Visible) 

     'Finally export the data 
     For c = 1 To visibleColumnCount 
      xlWS.Cells(1, c) = currentVisibleColumn.HeaderText 
      currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None) 
      lastColumnExported = currentVisibleColumn 
     Next 

     'Only export visible cells 
     For r = 0 To dgSMS.Rows.Count - 1 
      'Reset values 
      currentVisibleColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible) 
      lastColumnExported = currentVisibleColumn 
      For c = 1 To visibleColumnCount 
       Dim value = dgSMS.Rows(r).Cells(currentVisibleColumn.Index).Value 
       If value <> vbNullString Then 
        xlWS.Cells(r + 2, c) = value.ToString() 
       End If 
       currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None) 
       lastColumnExported = currentVisibleColumn 
      Next 
     Next 

     'Autosize columns in excel 
     Dim columns = xlWS.UsedRange.Columns 
     columns.AutoFit() 


     ' save the application 

     'workbook.SaveAs("c:\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _ 
     ' Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing) 



     ' Exit from the application 

     ' app.Quit() 
0

你爲什麼不首先生成一個數據表,你可以建立不添加列你不想導出,那麼你可以應用相同的導出過程到這個數據表。

+0

如何用數據表更改此行? DtExport.Rows(i).Cells(j).Value.ToString() – WFgo

0

更改您的for循環如下:

For i As Integer = 1 To DG.Columns.Count-1 
       WS.Cells(1, i) = DG.Columns(i).HeaderText 
    Next 

For i As Integer = 0 To DG.Rows.Count - 1 
       For j As Integer = 1 To DG.Columns.Count - 1 
        WS.Cells(i + 2, j + 1) = DG.Rows(i).Cells(j).Value.ToString() 
        WS.Cells(i + 2, 1).Font.Color = Color.Blue 
       Next 
Next 

我只是改變了內部的循環開始從1是從第二column.so不會出口第一列

+0

是列(0)的行不顯示,但列的headertext是 – WFgo

+0

@WFgo現在我編輯了我的標題文本的答案也..檢查與 – Haji

+0

@WFgo你檢查我的代碼? – Haji

1
For i = 0 To DG.Columns.Count - 1 
    WS.Cells(1, i + 1) = DG.Columns(i).HeaderText 
    Next