Dim tb As Table = CurrentTable
Dim hdr As Integer = tb.HeaderRows '获得表头的层数
Dim Book As New XLS.Book '定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) '引用工作簿的第一个工作表
'Sheet(0,1).Value = tb.DataTable.Name
tb.CreateSheetHeader(Sheet) '生成表头
''=======定义导出Excel表新样式===========
Dim Style As XLS.Style = Book.NewStyle() '定义新样式
Style.BorderTop = XLS.LineStyleEnum.Thin
Style.BorderBottom = XLS.LineStyleEnum.Thin
Style.BorderLeft = XLS.LineStyleEnum.Thin
Style.BorderRight = XLS.LineStyleEnum.Thin
Style.BorderColorTop = Color.Black
Style.BorderColorBottom = Color.Black
Style.BorderColorLeft = Color.Black
Style.BorderColorRight = Color.Black
Style.AlignHorz = XLS.AlignHorzEnum.Center '水平居中
Style.WordWrap = True '自动换行
Style.AlignVert = XLS.AlignVertEnum.Center '垂直居中
Style.Font = New Font("宋书", 9) '字体大小为9磅
''定义日期列的样式
Dim st As XLS.Style = Book.NewStyle() '日期列的显示格式
st.Format = "yyyy-MM-dd"
st.BorderTop = XLS.LineStyleEnum.Thin
st.BorderBottom = XLS.LineStyleEnum.Thin
st.BorderLeft = XLS.LineStyleEnum.Thin
st.BorderRight = XLS.LineStyleEnum.Thin
st.BorderColorTop = Color.Black
st.BorderColorBottom = Color.Black
st.BorderColorLeft = Color.Black
st.BorderColorRight = Color.Black
st.AlignHorz = XLS.AlignHorzEnum.Center '水平居中
st.WordWrap = True '自动换行
st.AlignVert = XLS.AlignVertEnum.Center '垂直居中
st.Font = New Font("宋书", 9) '字体大小为9磅
'生成数据
Dim i As Integer = 0
For cn As Integer = 0 To tb.Cols.Count - 1
If tb.Cols(cn).Visible Then
For r As Integer = 0 To tb.Rows.Count - 1 + hdr
sheet(r,i).Style = Style '定义单元格线颜色
Next
For r As Integer = 0 To tb.Rows.Count - 1
If tb.Cols(cn).IsDate Then '如果是日期列
Sheet(r + hdr,i).Style = st
End If
sheet(r + hdr,i).Value = IIF(tb.rows(r)(tb.cols(cn).name) = Nothing, "", tb.rows(r)(tb.cols(cn).name))
Next
i += 1
End If
Next
'定义首行的样式
Sheet.Rows.Insert(0)
sheet(0,0).Value = tb.DataTable.Name
i = 0
For cn As Integer = 0 To tb.Cols.Count - 1
If tb.Cols(cn).Visible Then
sheet.MergeCell(0,0,1,cn + 1) '合并第一行单元格
i += 1
End If
Next
st = Book.NewStyle() '日期列的显示格式
st.Font = new font("黑体", 20)
st.AlignHorz = XLS.AlignHorzEnum.Center
sheet(0,0).style = st
Dim dlg As New SaveFileDialog '定义一个新的保存对话框SaveFileDialog
dlg.Filter = "Excel文件|*.xls" '设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then
Book.Save(dlg.FileName)
Dim Proc As New Process
Proc.File = dlg.FileName
Proc.Start()
End If