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


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

主题:[求助]在一个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

 回到顶部
帅哥哟,离线,有人找我吗?
huaziqiao1976
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | 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

 回到顶部
帅哥哟,离线,有人找我吗?
huaziqiao1976
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | 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

 回到顶部