2012-03-29 112 views
1

我創建了一個導出到excel的報表。它出口很好。我現在想要做的是合併具有相同值的列中的連續單元格。我該怎麼做呢?請幫幫我。在Microsoft Office Interop中合併具有相同值的單元格excel

生成excel的身體,這是代碼:

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 

回答

0

我剛剛收到通知,這個問題得到了1000+的意見,所以我想我會發布什麼,我做了一年前在解決這個問題。我希望你們覺得它有用。

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 
1

你想要做的是,如果 - else語句來檢查,如果一列的值複製或沒有。如果是 - >合併單元格。您可以使用Excel中的內置合併單元格函數或「隱藏」重複單元格的值,也可以隱藏原始單元格和重複單元格之間的邊界。搜索時發現這一點。

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 

末次

來源:http://www.ozgrid.com/forum/showthread.php?t=57537

相關問題