2017-08-30 48 views
0

我有工作表,它有數據由WinForms Application.Please輸出導出,以便更好地瞭解數據如何存在在工作表Here中。現在我想要的是合併具有Ex的公共值的單元格。我的一列中有InvoiceID,因此InvoicID內會有一些項目。我想合併那些等於項目的行,但它只能爲這些記錄顯示一次。爲了更好的理解檢查這個我手動執行的屏幕截圖Here。 有什麼辦法可以使用代碼執行相同的操作。 以下是我用來將數據導出到Excel的代碼。需要合併Excel文件的列數據,同時從vb.net導出應用程序

`昏暗excelConverter作爲新GridGroupingExcelConverterControl

昏暗exportingOptions作爲新ExcelExportingOptions excelConverter.ExportToExcel(GridGroupingControl1 「Sample.xlsx」,exportingOptions)

的Process.Start( 「Sample.xlsx」)

Dim xlApp As Excel.Application 
    Dim xlWorkBook As Excel.Workbook 
    Dim xlWorkSheet As Excel.Worksheet 
    Dim misValue As Object = System.Reflection.Missing.Value 
    Dim i As Integer 
    Dim j As Integer 

    xlApp = New Excel.Application 
    xlWorkBook = xlApp.Workbooks.Add(misValue) 
    xlWorkSheet = xlWorkBook.Sheets("sheet1") 

    For i = 0 To dgvSearch.RowCount - 2 
     For j = 0 To dgvSearch.ColumnCount - 1 
      xlWorkSheet.Cells(i + 1, j + 1) = _ 
       dgvSearch(j, i).Value.ToString() 
     Next 
    Next 
    xlWorkBook.SaveAs("\vbexcel.xlsx") 
    xlWorkBook.Close() 
    xlApp.Quit() 
    releaseObject(xlApp) 
    releaseObject(xlWorkBook) 
    releaseObject(xlWorkSheet) 
    MsgBox("You can find the file C:\vbexcel.xlsx")` 

我是新來的導出數據到Excel中,這將是非常有益的。 謝謝,

回答

0

您可以創建一個循環。所以通過每個單元格進行循環。我開始寫出來,但最終不得不編寫代碼。解釋的工作是比寫很難:)

繼承人的代碼,你需要添加到你的後你的For循環後,你的另存爲。對於我(x軸)的+1和-2,我有點困惑,我沒有改變你的任何代碼,我不想打擾它,如果這一切都正確。

希望這有助於

For i = 1 To dgvSearch.RowCount - 2 
     For j = 0 To dgvSearch.ColumnCount - 1 
      'I dont understand why +1 etc? But we need to check the current cell against the one above, to see if they are the same 
      Dim cellAbove As String = xlWorkSheet.Cells(i + 1, j) 
      Dim cellActive As String = xlWorkSheet.Cells(i + 1, j + 1) 
      Dim cellBelow As String = xlWorkSheet.Cells(i + 1, j + 2) 

      If cellActive = cellAbove Then 
       If cellBelow = cellActive Then 
        'There could be more below, so we leave for the moment.. 
       Else 
        'The cell above (and possibly more above that) are the same 
        'So we need to start itterating through the cells above to remove any duplicates 
        For y = j + 2 To 2 Step -1 
         xlWorkSheet.Cells(i + 1, y) = "" 
         'As we step up the column, we check the one above and two above - so we remove this one, 
         'if they both are the same, we carry on, if not - it means the one above is different 
         'to two above (hence the one above must be the one we need to keep.. :S - bear with me :) 
         If xlWorkSheet.Cells(i + 1, y - 1) = cellActive And xlWorkSheet.Cells(i + 1, y - 2) = cellActive Then 
         Else 
          'If they dont match, we exit - you can add a If NOT xlworksheet.... if you wanted, or keep the else here. 
          Exit For 
         End If 
        Next 
       End If 
      Else 
       'Its not the same as above, so we need to keep this cell! 
      End If 
     Next 
    Next 
+0

感謝豐富,會嘗試。 –

相關問題