Rss & SiteMap

Foxtable(狐表) http://www.foxtable.com

新一代数据库软件,完美融合Access、Foxpro、Excel、vb.net之优势,人人都能掌握的快速软件开发工具!
共8 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:请教高手EXCEL报表的行高及居中代码不起作用

1楼
yangming 发表于:2009/4/15 14:34:00
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 = 30         '行高
Style.BackColor = Color.Red '样式的背景颜色设为红色
For c As Integer = 0 To dt.Cols.Count -2 '添加列标题
    Sheet(0, c).Value = dt.Cols(c).Name
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


请教高手,为何我红色的部分不起作用呢?
2楼
狐狸爸爸 发表于:2009/4/15 14:41:00
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 = 30         '行高
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
3楼
yangming 发表于:2009/4/15 15:03:00
还是不行...
看图:下面的行高改了,我加大行高为100了,就是居中也没起作用,搞不明白原因
4楼
yangming 发表于:2009/4/15 15:07:00


请看看,这是我的全部代码

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()

5楼
狐狸爸爸 发表于:2009/4/15 15:20:00

你前面设置了列标题垂直居中,结果后面又有:

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设置了水平和垂直居中吗?

图片点击可在新窗口打开查看

6楼
yangming 发表于:2009/4/15 16:15:00
其它的都可以了,只是这个列宽和行高我无论放在何处,都只是列宽起作用,而行高却对我所选之外的行起作用,是什么原因呢?放在何处才对呢?

Sheet.DefaultColumnWidth = 100   '列宽
Sheet.DefaultRowHeight = 80         '行高
7楼
狐狸爸爸 发表于:2009/4/15 16:19:00

呵呵,这是正常的,因为行高等于模板中的实际行高。

可以这样:

For r As Integer = 0 to Sheet.rows.Count -1
   sheet.rows(r).height = 80

next

8楼
yangming 发表于:2009/4/15 16:44:00
好了,谢谢贺老师,我改了一下,第一行设为30,其它行设为25,这样就好看多了,呵呵
[此贴子已经被作者于2009-4-15 16:44:01编辑过]
共8 条记录, 每页显示 10 条, 页签: [1]

Copyright © 2000 - 2018 foxtable.com Tel: 4000-810-820 粤ICP备11091905号

Powered By Dvbbs Version 8.3.0
Processed in .03027 s, 2 queries.