以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  导出列问题  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=71744)

--  作者:HJG_HB950207
--  发布时间:2015/7/17 11:25:00
--  导出列问题
通过选取部分列,导出部分列多层表头电子表,研究半天http://www.foxtable.com/help/topics/1971.htm结果导出的还是全部列(依葫芦画瓢画成西瓜了)。前面的选择与后面的导出好像没关联起来。谢谢老师将下代码改正:



Dim ckl As WinForm.CheckedListBox = e.Form.Controls("CheckedListBox1")
Dim nms As New List(of String)
For i As Integer = 0 To ckl.Items.count - 1 
    If ckl.GetItemChecked(i) Then
        nms.Add(ckl.Items(i))
    End If
Next
If nms.Count = 0 Then
    MessageBox.Show("至少要选择一列","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return
End If


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
Book.Save("c:\\reports\\test.xls")
Dim Proc As New Process
Proc.File = "c:\\reports\\test.xls"
Proc.Start()

--  作者:大红袍
--  发布时间:2015/7/17 11:31:00
--  

 Dim ckl As WinForm.CheckedListBox = e.Form.Controls("CheckedListBox1")
Dim nms As New List(of String)
For i As Integer = 0 To ckl.Items.count - 1 \'获取已经勾选的列
    If ckl.GetItemChecked(i) Then
        nms.Add(ckl.Items(i))
    End If
Next
If nms.Count = 0 Then
    MessageBox.Show("至少要选择一列","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return
End If
\'生成Excel表
Dim dt As Table = Tables("订单")
Dim Book As New XLS.Book
Dim Sheet As XLS.Sheet = Book.Sheets(0)
dt.CreateSheetHeader(Sheet)  \'生成表头
Dim rcount As Integer = dt.HeaderRows
For r As Integer = 0 To dt.Rows.Count - 1 \'填入数据
    For i As Integer = 0 To nms.count -1
        Sheet(r + rcount, i).Value = dt.rows(r)(nms(i))
    Next
Next
\'保存并打开excel文件
Book.Save("c:\\reports\\test.xls")
Dim Proc As New Process
Proc.File = "c:\\reports\\test.xls"
Proc.Start()


--  作者:HJG_HB950207
--  发布时间:2015/7/17 11:32:00
--  
谢谢,一定举一反三,搞懂!
--  作者:HJG_HB950207
--  发布时间:2015/7/17 11:41:00
--  
上代码还是有点问题,执行结果显示的电子表,好像一是列名与记录数据错位了,二是没选的列也在表里(数据为空)
--  作者:大红袍
--  发布时间:2015/7/17 11:51:00
--  

Dim ckl As WinForm.CheckedListBox = e.Form.Controls("CheckedListBox1")
Dim dt As Table = Tables("表A")
Dim cvw As String = dt.getColVisibleWidth
Dim nms As New List(of String)
For i As Integer = 0 To ckl.Items.count - 1 \'获取已经勾选的列
    If ckl.GetItemChecked(i) Then
        nms.Add(ckl.Items(i))
    Else
        dt.Cols(ckl.Items(i)).Visible = False
    End If
Next
If nms.Count = 0 Then
    MessageBox.Show("至少要选择一列","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return
End If
\'生成Excel表

Dim Book As New XLS.Book
Dim Sheet As XLS.Sheet = Book.Sheets(0)
dt.CreateSheetHeader(Sheet)  \'生成表头
Dim rcount As Integer = dt.HeaderRows
For r As Integer = 0 To dt.Rows.Count - 1 \'填入数据
    For i As Integer = 0 To nms.count -1
        Sheet(r + rcount, i).Value = dt.rows(r)(nms(i))
    Next
Next

dt.SetColVisibleWidth(cvw)

\'保存并打开excel文件
Book.Save("c:\\reports\\test.xls")
Dim Proc As New Process
Proc.File = "c:\\reports\\test.xls"
Proc.Start()


--  作者:HJG_HB950207
--  发布时间:2015/7/17 13:23:00
--  
还是错位,不好意思。
--  作者:大红袍
--  发布时间:2015/7/17 14:36:00
--  
 测 试 没 问 题