Rss & SiteMap
Foxtable(狐表) http://www.foxtable.com
请看看,这是我的全部代码
Dim dt As Table = Tables("工资表")
Dim Book As New XLS.Book() '定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) '引用工作簿的第一个工作表
Dim Style As XLS.Style = Book.NewStyle() '定义新样式
Style.ForeColor = Color.Navy '设置样式的字体颜色
Style.AlignHorz = XLS.AlignHorzEnum.Center
Style.AlignVert = XLS.AlignVertEnum.Center
Sheet.DefaultColumnWidth = 100 '列宽
Sheet.DefaultRowHeight = 80 '行高
Style.BackColor = Color.Red '样式的背景颜色设为红色
For c As Integer = 0 To dt.Cols.Count -2 '添加列标题
Sheet(0, c).Value = dt.Cols(c).Name
Sheet(0, c).Style= Style
Next
For r As Integer = 0 To dt.Rows.Count - 2 '填入数据
For c As Integer = 0 To dt.Cols.Count -2
Sheet(r +1, c).Value = dt.rows(r)(c)
Next
If dt.rows(r)("实发工资") >= 4000 Then '如果实发工资大于等于4000
Sheet(r + 1,dt.Cols("实发工资").Index).Style = Style '设置实发工资单元格的样式
End If
Next
Dim Style1 As XLS.Style = Book.NewStyle() '定义新样
Style1.BorderTop = XLS.LineStyleEnum.Thin
Style1.BorderBottom = XLS.LineStyleEnum.Thin
Style1.BorderLeft = XLS.LineStyleEnum.Thin
Style1.BorderRight = XLS.LineStyleEnum.Thin
Style1.BorderColorTop = Color.Black
Style1.BorderColorBottom = Color.Black
Style1.BorderColorLeft = Color.Black
Style1.BorderColorRight = Color.Black
For r As Integer = 0 to dt.rows.Count -1
For c As Integer =0 To dt.Cols.Count -2
Sheet(r,c).Style = Style1
Next
Next
With Sheet.PrintSetting
.AutoScale = True '自动缩放
.FitPagesDown = 1 '垂直方向缩为1页
End With
With Sheet.PrintSetting
.PaperKind = 9 '设为A4纸
.LandScape = True '横向打印
.MarginLeft = 10 '左右边距设为20毫米
.MarginRight = 10
.MarginTop = 15 '上下边距设为15毫米
.MarginBottom = 10
End With
'打开工作簿
Book.Save("c:\123.xls")
Dim Proc As New Process
Proc.File = "c:\123.xls"
Proc.Start()
你前面设置了列标题垂直居中,结果后面又有:
For r As Integer = 0 to dt.rows.Count -1
For c As Integer =0 To dt.Cols.Count -2
Sheet(r,c).Style = Style1
Next
Next
等于所有单元格重新按照Style1的设置排列,而Style1设置了水平和垂直居中吗?
呵呵,这是正常的,因为行高等于模板中的实际行高。
可以这样:
For r As Integer = 0 to Sheet.rows.Count -1
sheet.rows(r).height = 80
next