以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  只导出窗口表中显示的数据代码出错  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=132997)

--  作者:ZJZK2018
--  发布时间:2019/4/3 16:56:00
--  只导出窗口表中显示的数据代码出错
老师:
只导出窗口表中显示的数据代码,我隐藏一列后,导出表头是对的,数据都往后移一位,下面代码如何修改??

    Case "Excel"
        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磅
        \'生成数据
        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
        For cn As Integer = 0 To tb.Cols.Count - 1
            If tb.Cols(cn).Visible Then
                sheet.MergeCell(0,0,1,cn + 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


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


--  作者:有点甜
--  发布时间:2019/4/3 17:08:00
--  

sheet(r + hdr,cn).Value = IIF(tb(r,cn) = Nothing, "", tb(r,cn))

 

改成

 

sheet(r + hdr,cn).Value = IIF(tb.rows(r)(tb.cols(cn).name) = Nothing, "", tb.rows(r)(tb.cols(cn).name))

 

 


--  作者:ZJZK2018
--  发布时间:2019/4/3 17:25:00
--  
有点甜老师还是不对,跟前面一样??
--  作者:有点甜
--  发布时间:2019/4/3 18:42:00
--  

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


 


--  作者:ZJZK2018
--  发布时间:2019/4/3 20:18:00
--  
自定义函数没有反应?

Functions.Execute("Table_Excel",CurrentTable)


Dim tb As Table = Args(0)   
\'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) \'生成表头
.....

--  作者:有点甜
--  发布时间:2019/4/3 20:44:00
--  
请上传具体事例测试。