Foxtable(狐表)用户栏目专家坐堂 → [求助] CheckedListBox 选择项局部导出excel表


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

主题:[求助] CheckedListBox 选择项局部导出excel表

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


加好友 发短信
等级:管理员 帖子:47497 积分:251403 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By:2016/10/24 8:12:00 [显示全部帖子]

编程的逻辑能力你已经有了,但是有一些常识错误,要是花点时间看过两遍帮助,你可以成为高手。

Dim ckl As WinForm.CheckedListBox = e.Form.Controls("CheckedListBox1")
If ckl.CheckedIndices.count = 0 Then
    MessageBox.Show("至少要选择一个供应商","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Return
End If
Dim nms As String
For Each i As Integer In ckl.CheckedIndices '获取已经勾选的
    nms = nms & ",'" &   ckl.Items(i) & "'"
Next
nms = nms.Trim(",")
'生成Excel表
Dim dt As DataTable = DataTables("表A")
Dim nm() As String = {"供应商","产品","规格","合计"}   '要导出的列名
Dim caps() As String = {"供应商","产品名称","规格/型号","合计金额"}  '对应的列标题
Dim szs() As Integer = {150,180,120,85} '对应的列宽
Dim Book As New XLS.Book '定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) '引用工作簿的第一个工作表
For c As Integer = 0 To nm.length -1
    Sheet(1, c).Value = caps(c) '指定列标题
    Sheet.Cols(c).Width = szs(c) '指定列宽
    Next
Sheet(0, 1).Value = e.Form.Controls("ComboBox1").value
Dim drs As List(of DataRow) = dt.Select("供应商='" & e.Form.Controls("ComboBox1").value & "' And 产品 in (" & nms & ")")
For r As Integer = 0 To drs.count -1 '填入数据
    For i As Integer = 0 To nm.length -1
        Sheet(r + 2, i).Value = drs(r)(nm(i))
    Next
Next
'保存并打开excel文件
Book.Save("d:\test.xls")
Dim Proc As New Process
Proc.File = "d:\test.xls"
Proc.Start()
e.Form().Close
[此贴子已经被作者于2016/10/24 8:13:26编辑过]

 回到顶部