应用实例一

假定要设计下图所示的导出窗口,让用户选择要导出的列,单击确定按钮后导出为Excel文件:

设计方法很简单:

1、将窗口的AfterLoad事件代码设置为:

Dim ckl As WinForm.CheckedListBox = e.Form.Controls("CheckedListBox1")
For
Each dc As DataCol In DataTables("订单").DataCols
    ckl.Items.Add(dc.name)

Next

2、将导出按钮的Clik事件代码设置为:

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)
For
i As Integer = 0 To nms.count -1
    Sheet(0, i).Value = nms(i)

Next
For
r As Integer = 0 To dt.Rows.Count - 1 '填入数据
    For i As Integer = 0 To nms.count -1
        Sheet(r + 1, 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()


本页地址:http://www.foxtable.com/webhelp/topics/2745.htm