以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  请教高手EXCEL报表的行高及居中代码不起作用  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=2455)

--  作者:yangming
--  发布时间:2009/4/15 14:34:00
--  请教高手EXCEL报表的行高及居中代码不起作用
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


请教高手,为何我红色的部分不起作用呢?

--  作者:狐狸爸爸
--  发布时间: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
--  作者:yangming
--  发布时间:2009/4/15 15:03:00
--  
还是不行...
看图:下面的行高改了,我加大行高为100了,就是居中也没起作用,搞不明白原因
--  作者: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()


--  作者:狐狸爸爸
--  发布时间: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设置了水平和垂直居中吗?

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


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

Sheet.DefaultColumnWidth = 100   \'列宽
Sheet.DefaultRowHeight = 80         \'行高

--  作者:狐狸爸爸
--  发布时间:2009/4/15 16:19:00
--  

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

可以这样:

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

next


--  作者:yangming
--  发布时间:2009/4/15 16:44:00
--  
好了,谢谢贺老师,我改了一下,第一行设为30,其它行设为25,这样就好看多了,呵呵
[此贴子已经被作者于2009-4-15 16:44:01编辑过]