Reputation: 431
i created a report which exports to excel. it exports just fine. what i want to do now is merge the consecutive cells in a column which have the same values. how do i do this? please help me.
this is code that generates the excel body:
Protected Sub generateExcelBody(ByVal xcelworksheet As Microsoft.Office.Interop.Excel.Worksheet, ByVal recarray As Array, ByVal numofrecords As Integer)
Dim chartrange As Microsoft.Office.Interop.Excel.Range
chartrange = Nothing
chartrange = xcelworksheet.Range("B5", "F5")
chartrange.MergeCells = True
chartrange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft
chartrange.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter
chartrange = Nothing
chartrange = xcelworksheet.Range("A8", System.Reflection.Missing.Value)
chartrange.FormulaR1C1 = "Record Series : " & hiddenrs.Value
chartrange = Nothing
chartrange = xcelworksheet.Range("A9", System.Reflection.Missing.Value)
chartrange.FormulaR1C1 = "Department : " & hiddendept.Value
chartrange = Nothing
chartrange = xcelworksheet.Range("A10", System.Reflection.Missing.Value)
chartrange.FormulaR1C1 = "Number of Records : " & numofrecords
chartrange = Nothing
chartrange = xcelworksheet.Range("A14", "F14")
chartrange.Resize(numofrecords, 6).Value2 = recarray
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
chartrange.Resize(numofrecords, 6).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium
chartrange.Resize(numofrecords, 6).WrapText = True
chartrange.Resize(numofrecords, 6).EntireRow.AutoFit()
chartrange.Resize(numofrecords, 6).Font.Size = 10
chartrange.Resize(numofrecords, 6).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
chartrange.Resize(numofrecords, 6).VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter
End Sub
Upvotes: 1
Views: 3299
Reputation: 431
I just received a notification that this question got 1000+ views so I thought I'd post what I did a year ago in solution to this problem. I hope you guys find it useful.
Public Sub mergeRows(ByVal grid As GridView)
For rowIndex As Integer = (grid.Rows.Count - 2) To 0 Step -1
Dim currRow As GridViewRow = grid.Rows(rowIndex)
Dim prevRow As GridViewRow = grid.Rows(rowIndex + 1)
For i As Integer = 0 To (currRow.Cells.Count - 1)
If currRow.Cells(0).Text = prevRow.Cells(i).Text Then
currRow.Cells(0).RowSpan = IIf(prevRow.Cells(0).RowSpan < 2, 2, prevRow.Cells(0).RowSpan + 1)
prevRow.Cells(0).Visible = False
If currRow.Cells(1).Text = prevRow.Cells(1).Text Then
currRow.Cells(1).RowSpan = IIf(prevRow.Cells(1).RowSpan < 2, 2, prevRow.Cells(1).RowSpan + 1)
prevRow.Cells(1).Visible = False
currRow.Cells(2).RowSpan = IIf(prevRow.Cells(1).RowSpan < 2, 2, prevRow.Cells(1).RowSpan + 1)
prevRow.Cells(2).Visible = False
End If
End If
Next
Next
End Sub
Protected Sub Gridview1_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Gridview1.PreRender
mergeRows(Gridview1)
End Sub
Upvotes: 0
Reputation: 2075
What you want to do is a if - else statement to check if the value of one column is duplicated or not. If yes -> merge the cells. You can either use built in merge cell function in Excel or "hide" the value of the duplicated cell + hide the border between the original and the duplicated cell. Found this while searching.
Sub FormatLikeDates()
Dim d As Date, r As Long, n As Integer, c As Range
For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Int(Cells(r, 1)) = Int(Cells(r + 1, 1)) Then
n = n + 1
End If
If Int(Cells(r, 1)) <> Int(Cells(r + 1, 1)) And n > 0 Then
For Each c In Range(Cells(r - n + 1, 1), Cells(r, 1))
c.Font.ColorIndex = 2
c.Interior.ColorIndex = 2
Next c
Range(Cells(r - n, 1), Cells(r, 1)).BorderAround ColorIndex:=3, Weight:=xlThin
n = 0
End If
Next r
End Sub
Source : http://www.ozgrid.com/forum/showthread.php?t=57537
Upvotes: 1