mark 多层表头
Dim dt As Table = Tables("表C")
Dim nms() As String = {"第一列_aa", "第一列_bb", "第一列_cc","第三列", "第四列_a_b", "第四列_a_c"} '要导出的列名
Dim caps() As String = {"第一列_aa", "第一列_bb", "第一列_cc", "第三列","第四列_a_b","第四列_a_c"} '对应的列标题
Dim hlvl As Integer = 3
Dim szs() As Integer = {100,100,80} '对应的列宽
Dim Book As New XLS.Book '定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) '引用工作簿的第一个工作表
Dim jz As xls.style = book.NewStyle
jz.AlignHorz = XLS.AlignHorzEnum.Center
jz.AlignVert = XLS.AlignVertEnum.Center
For c As Integer = 0 To nms.length -1
Dim ary() As String = caps(c).split("_")
For i As Integer = 0 To ary.length-1
sheet(i, c).value = ary(i)
sheet(i, c).Style = jz
Next
Next
For i As Integer = 0 To hlvl-1
Dim pi As Integer = 0
For c As Integer = 0 To nms.length -2
If sheet(i,c).text = sheet(i,c+1).text Then
Dim flag As Boolean = True
For k As Integer = i-1 To 0 Step -1
If sheet(k,c).value <> sheet(k,c+1).value Then
flag = False
End If
Next
If flag Then
pi += 1
Else
pi = 0
End If
Else
sheet.MergeCell(i, c-pi, 1, pi+1)
pi = 0
End If
Next
sheet.MergeCell(i, nms.length-pi-1, 1, pi+1)
Next
For c As Integer = 0 To nms.length -1
Dim pi As Integer = 0
For i As Integer = hlvl-1 To 0 Step -1
If sheet(i, c).text = "" Then
pi += 1
Else
sheet.MergeCell(i, c, pi+1, 1)
Exit For
End If
Next
next
For r As Integer = 0 To dt.Rows.Count - 1 '填入数据
For c As Integer = 0 To nms.length -1
Sheet(r+hlvl, c).Value = dt.rows(r)(nms(c))
Next
Next
book.save("d:\test.xls")