Foxtable(狐表)用户栏目专家坐堂 → [求助]在一个Excel工作薄里导出多表


  共有1789人关注过本帖树形打印复制链接

主题:[求助]在一个Excel工作薄里导出多表

帅哥哟,离线,有人找我吗?
huaziqiao1976
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:137 积分:1175 威望:0 精华:0 注册:2020/9/28 9:41:00
[求助]在一个Excel工作薄里导出多表  发帖心情 Post By:2024/1/16 9:29:00 [只看该作者]

数据库里有台账一、台账二、台账三,要实现把三个表的内类导出在一个Excel工作薄的三个表格里

Dim tbl As Table = Tables("台账一")
Dim hdr As Integer = tbl.HeaderRows '获得表头的层数
Dim cnt As Integer
Dim Book As New XLS.Book
Dim Sheet As XLS.Sheet = Book.Sheets(0)
tbl.CreateSheetHeader(Sheet) '生成表头
For c As Integer = 0 To tbl.Cols.Count - 1
    If tbl.Cols(c).Visible Then
        For r As Integer = 0 To tbl.Rows.Count - 1
            sheet(r + hdr,cnt).value = tbl(r,c)
        Next
        cnt = cnt + 1
    End If
Next

........................(这段代码应该怎么写)

Dim dlg As New SaveFileDialog '定义一个新的SaveFileDialog
dlg.Filter= "Excel文件|*.xls" '设置筛选器
dlg.FileName = 台账汇总"
If dlg.ShowDialog = DialogResult.Ok Then
    Book.Save(dlg.FileName)
    Dim Proc As New Process
    Proc.File = dlg.FileName
    Proc.Start()
End If

 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/1/16 10:07:00 [只看该作者]

第一个表格导到Dim Sheet As XLS.Sheet = Book.Sheets(0)
第二个表格导到Dim Sheet As XLS.Sheet = Book.Sheets(1)
第三个表格导到Dim Sheet As XLS.Sheet = Book.Sheets(2)

 回到顶部
帅哥哟,离线,有人找我吗?
huaziqiao1976
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:137 积分:1175 威望:0 精华:0 注册:2020/9/28 9:41:00
  发帖心情 Post By:2024/1/16 10:58:00 [只看该作者]

只能识别 Book.Sheets(0) ,改成 Book.Sheets(1)提示:
  “索引超出范围。必须为非负值并小于集合大小。
参数名: index

 回到顶部
帅哥,在线噢!
有点蓝
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/1/16 11:09:00 [只看该作者]


 回到顶部
帅哥哟,离线,有人找我吗?
huaziqiao1976
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:137 积分:1175 威望:0 精华:0 注册:2020/9/28 9:41:00
  发帖心情 Post By:2024/1/16 11:36:00 [只看该作者]

问题解决,谢谢蓝板
Dim tbl As Table = Tables("台账一")
Dim hdr As Integer = tbl.HeaderRows '获得表头的层数
Dim cnt As Integer
Dim Book As New XLS.Book
Dim Sheet As XLS.Sheet = Book.Sheets(0)
tbl.CreateSheetHeader(Sheet) '生成表头
For c As Integer = 0 To tbl.Cols.Count - 1
    If tbl.Cols(c).Visible Then
        For r As Integer = 0 To tbl.Rows.Count - 1
            sheet(r + hdr,cnt).value = tbl(r,c)
        Next
        cnt = cnt + 1
    End If
Next

Dim tbl1 As Table = Tables("台账二")
Dim hdr1 As Integer = tbl1.HeaderRows '获得表头的层数
Dim cnt1 As Integer
Book.Sheets.Add()
Dim Sheet1 As XLS.Sheet = Book.Sheets(1)
tbl1.CreateSheetHeader(Sheet1) '生成表头
For c1 As Integer = 0 To tbl1.Cols.Count - 1
    If tbl1.Cols(c1).Visible Then
        For r1 As Integer = 0 To tbl1.Rows.Count - 1
            sheet1(r1 + hdr1,cnt1).value = tbl1(r1,c1)
        Next
        cnt1 = cnt1 + 1
    End If
Next

Dim tbl2 As Table = Tables("台账三")
Dim hdr2 As Integer = tbl2.HeaderRows '获得表头的层数
Dim cnt2 As Integer
Book.Sheets.Add()
Dim Sheet2 As XLS.Sheet = Book.Sheets(2)
tbl2.CreateSheetHeader(Sheet2) '生成表头
For c2 As Integer = 0 To tbl2.Cols.Count - 1
    If tbl2.Cols(c2).Visible Then
        For r2 As Integer = 0 To tbl2.Rows.Count - 1
            sheet2(r2 + hdr2,cnt2).value = tbl2(r2,c2)
        Next
        cnt2 = cnt2 + 1
    End If
Next

Dim dlg As New SaveFileDialog '定义一个新的SaveFileDialog
dlg.Filter= "Excel文件|*.xls" '设置筛选器
dlg.FileName = "汇总台账"
If dlg.ShowDialog = DialogResult.Ok Then
    Book.Save(dlg.FileName)
    Dim Proc As New Process
    Proc.File = dlg.FileName
    Proc.Start()
End If

 回到顶部
帅哥,在线噢!
有点蓝
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/1/16 11:52:00 [只看该作者]

可以简化一下

Dim tbs() As String = {"台账一", "台账二", "台账三"}
Dim Book As New XLS.Book
For j As Integer = 1 To tbs.length - 1
    Book.Sheets.Add()
Next
Dim tbl As Table
For i As Integer = 0 To tbs.length - 1
    tbl = Tables(tbs(i))
    Dim hdr2 As Integer = tbl.HeaderRows '获得表头的层数
    Dim cnt2 As Integer = 0
    Dim Sheet As XLS.Sheet = Book.Sheets(i)
    tbl.CreateSheetHeader(Sheet) '生成表头
    For c2 As Integer = 0 To tbl.Cols.Count - 1
        If tbl.Cols(c2).Visible Then
            For r2 As Integer = 0 To tbl.Rows.Count - 1
                sheet2(r2 + hdr2, cnt2).value = tbl(r2, c2)
            Next
            cnt2 = cnt2 + 1
        End If
    Next
Next

Dim dlg As New SaveFileDialog '定义一个新的SaveFileDialog
dlg.Filter = "Excel文件|*.xls" '设置筛选器
dlg.FileName = "汇总台账"
If dlg.ShowDialog = DialogResult.Ok Then
    Book.Save(dlg.FileName)
    Dim Proc As New Process
    Proc.File = dlg.FileName
    Proc.Start()
End If

 回到顶部