2012-03-29 116 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

相关问题