以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  导出Excel表中日期列的设置  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=124804)

--  作者:ZJZK2018
--  发布时间:2018/9/14 0:39:00
--  导出Excel表中日期列的设置
老师下面代码有二个问题:
1、窗口表中日期列设置无效,如何调整?
2、在导出excel中第一行设置为:Sheet(0,1).Value = tb.DataTable.Name,最好为有多少列合并为一个单元格并单独设置"字体及大小"


Dim tb As Table = Tables("往来款项台帐_Table1")
Dim hdr As Integer = tb.HeaderRows \'获得表头的层数
\'Dim cnt As Integer

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"

For cn As Integer = 0 To tb.Cols.Count - 1
    If tb.Cols(cn).Visible Then
        If tb.Cols(cn).IsDate Then  \'如果是日期列
            Sheet.Cols(cn).Style = st \'设置显示格式
        End If
        For r As Integer = 0 To tb.Rows.Count - 1
            sheet(r + hdr,cn).Value = tb(r,cn)
            sheet(r,cn).Style = Style     \'定义单元格线颜色
        Next
        \'cnt = cnt + 1
    End If
Next

--  作者:有点甜
--  发布时间:2018/9/14 9:20:00
--  

1、加入msgbox,能否进入执行红色代码?

 

2、如果sheet.cols()设置样式不行,那你试试sheet(r,cn).style这样设置样式。

 

3、合并单元格,参考 http://www.foxtable.com/webhelp/scr/1153.htm

 

设置字体大小,给单元格设置一个新的style即可。


--  作者:ZJZK2018
--  发布时间:2018/9/14 10:22:00
--  
老师还是不行

Dim tb As Table = CurrentTable   \' Tables("往来款项台帐_Table1")
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"

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
            If tb.Cols(cn).IsDate Then  \'如果是日期列
                \'Sheet.Cols(cn).Style = st \'设置显示格式
                Sheet(r + hdr,cn).Style = st
            End If
            \'For r As Integer = 0 To tb.Rows.Count - 1
            sheet(r + hdr,cn).Value = tb(r,cn)
        Next
        For r As Integer = 0 To tb.Rows.Count - 1 + hdr
            sheet(r,cn).Style = Style     \'定义单元格线颜色
        Next
    End If
Next

--  作者:有点甜
--  发布时间:2018/9/14 10:38:00
--  

Dim tb As Table = CurrentTable   \' Tables("往来款项台帐_Table1")
Dim hdr As Integer = tb.HeaderRows \'获得表头的层数
Dim Book As New XLS.Book \'定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) \'引用工作簿的第一个工作表
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"


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,cn).Style = Style     \'定义单元格线颜色
        Next
        For r As Integer = 0 To tb.Rows.Count - 1
            If tb.Cols(cn).IsDate Then  \'如果是日期列
                Sheet(r + hdr,cn).Style = st
            End If
            sheet(r + hdr,cn).Value = iif(tb(r,cn)=Nothing, "", tb(r,cn))
        Next
    End If
Next

Sheet.Rows.Insert(0)
sheet(0,0).Value = tb.DataTable.Name
sheet.MergeCell(0,0,1,CurrentTable.Cols.count)
st  = Book.NewStyle()   \'日期列的显示格式
st.Font = new font("宋体", 20)
st.AlignHorz = XLS.AlignHorzEnum.Center
sheet(0,0).style = st

book.save("d:\\test.xls")
Dim proc As new Process
proc.file = "d:\\test.xls"
proc.start


--  作者:ZJZK2018
--  发布时间:2018/9/14 10:53:00
--  
按上面代码日期列是对了,但日期列的线框没有了,而且字体格式也不对?


图片点击可在新窗口打开查看此主题相关图片如下:qq截图20180914105731.png
图片点击可在新窗口打开查看

还有老师:tb(r,cn)这个写法代表什么??

[此贴子已经被作者于2018/9/14 10:59:34编辑过]

--  作者:有点甜
--  发布时间:2018/9/14 11:32:00
--  

修改这个代码,把你需要的样式,都给st设置进去,即可。

 

Dim st As XLS.Style = Book.NewStyle()   \'日期列的显示格式
st.Format = "yyyy-MM-dd"


--  作者:ZJZK2018
--  发布时间:2018/9/14 13:26:00
--  
老师你好:

如果窗口副表采用了汇总模式并设置了显示目录树,但导出到excel中时,目录树的形式没有了,如何处理?

--  作者:有点甜
--  发布时间:2018/9/14 14:47:00
--  
以下是引用ZJZK2018在2018/9/14 13:26:00的发言:
老师你好:

如果窗口副表采用了汇总模式并设置了显示目录树,但导出到excel中时,目录树的形式没有了,如何处理?

 

没办法的,无法保留目录树的。