以文本方式查看主题

-  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=190089)

--  作者:huaziqiao1976
--  发布时间:2024/1/16 9:29:00
--  [求助]在一个Excel工作薄里导出多表
数据库里有台账一、台账二、台账三,要实现把三个表的内类导出在一个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

--  作者:有点蓝
--  发布时间: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
--  发布时间:2024/1/16 10:58:00
--  
只能识别 Book.Sheets(0) ,改成 Book.Sheets(1)提示:
  “索引超出范围。必须为非负值并小于集合大小。
参数名: index

--  作者:有点蓝
--  发布时间:2024/1/16 11:09:00
--  
新增一个,再用:http://www.foxtable.com/webhelp/topics/1465.htm
--  作者:huaziqiao1976
--  发布时间: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

--  作者:有点蓝
--  发布时间: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