以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]导出  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=128235)

--  作者:江南小镇
--  发布时间:2018/12/1 17:57:00
--  [求助]导出
老师好,下面代码报错。


图片点击可在新窗口打开查看此主题相关图片如下:图像 1.png
图片点击可在新窗口打开查看


Dim dt As Table = Tables("订单管理")
dt.filter = "发货状态=\'已发货\' and 订单状态=\'确定\'"
Dim nms() As String = {"订单编号","订单日期","客户名称"} \'要导出的列名
Dim caps() As String = {"订单编号","订单日期","客户名称"} \'对应的列标题
Dim szs() As Integer = {100,100,100} \'对应的列宽
Dim Book As New XLS.Book \'定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) \'引用工作簿的第一个工作表
Dim st As XLS.Style = Book.NewStyle \'日期列的显示格式
st.Format = "yyyy-MM-dd"
For c As Integer = 0 To nms.length -1
    Sheet(0, c).Value = caps(c) \'指定列标题
    Sheet.Cols(c).Width = szs(c) \'指定列宽
    If dt.Cols(nms(c)).IsDate Then \'如果是日期列
        Sheet.Cols(c).Style = st \'设置显示格式
    End If
Next
For r As Integer = 0 To dt.Rows.Count - 1 \'填入数据
    For c As Integer = 0 To nms.length -1
        Sheet(r +1, c).Value = dt.rows(r)(nms(c))
    Next
Next
Dim dlg As New SaveFileDialog \'定义一个新的SaveFileDialog
dlg.Filter= "Excel文件|*.xls" \'设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then
    Book.Save(dlg.FileName)
    Dim Proc As New Process
    Proc.File = dlg.FileName
    Proc.Start()
End If

--  作者:y2287958
--  发布时间:2018/12/2 9:33:00
--  
代码没问题,具体请上实例
--  作者:江南小镇
--  发布时间:2018/12/2 16:42:00
--  
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:管理项目1.table


--  作者:有点甜
--  发布时间:2018/12/2 21:19:00
--  

修改红色代码

 

Dim dt As Table = Tables("订单管理")
dt.filter = "发货状态=true and 订单状态=true"

 


--  作者:江南小镇
--  发布时间:2018/12/2 22:19:00
--  
 谢谢老师
--  作者:江南小镇
--  发布时间:2018/12/3 5:59:00
--  
 老师,我想在原来设置代码的基础上增加功能,把数据导出到工作簿上然后根据客户的名称分散到工作表中。谢谢老师!
Dim dt As Table = Tables("订单管理")
dt.filter = "发货状态=true and 订单状态=true"
Dim nms() As String = {"订单编号","订单日期","客户名称"} \'要导出的列名
Dim caps() As String = {"订单编号","订单日期","客户名称"} \'对应的列标题
Dim szs() As Integer = {100,100,100,100} \'对应的列宽
Dim Book As New XLS.Book \'定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) \'引用工作簿的第一个工作表
Dim st As XLS.Style = Book.NewStyle \'日期列的显示格式
st.Format = "yyyy-MM-dd"
For c As Integer = 0 To nms.length -1
    Sheet(0, c).Value = caps(c) \'指定列标题
    Sheet.Cols(c).Width = szs(c) \'指定列宽
    If dt.Cols(nms(c)).IsDate Then \'如果是日期列
        Sheet.Cols(c).Style = st \'设置显示格式
    End If
Next
For r As Integer = 0 To dt.Rows.Count - 1 \'填入数据
    For c As Integer = 0 To nms.length -1
        Sheet(r +1, c).Value = dt.rows(r)(nms(c))
    Next
Next
Dim dlg As New SaveFileDialog \'定义一个新的SaveFileDialog
dlg.Filter= "Excel文件|*.xls" \'设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then
    Book.Save(dlg.FileName)
    Dim Proc As New Process
    Proc.File = dlg.FileName
    Proc.Start()
End If

--  作者:有点甜
--  发布时间:2018/12/3 8:52:00
--  
Dim dlg As New SaveFileDialog \'定义一个新的SaveFileDialog
dlg.Filter= "Excel文件|*.xls" \'设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then
    Dim dt As DataTable = DataTables("订单管理")
    Dim nms() As String = {"订单编号","订单日期","客户名称"} \'要导出的列名
    Dim caps() As String = {"订单编号","订单日期","客户名称"} \'对应的列标题
    Dim szs() As Integer = {100,100,100,100} \'对应的列宽
    Dim Book As New XLS.Book \'定义一个Excel工作簿
    For Each mc As String In dt.GetValues("客户名称", "发货状态=true and 订单状态=true and 客户名称 is not null")
        Dim Sheet As XLS.Sheet = Book.Sheets.Add \'引用工作簿的第一个工作表
        sheet.name = mc
        Dim drs = dt.Select("客户名称=\'" & mc & "\' and 发货状态=true and 订单状态=true")
        Dim st As XLS.Style = Book.NewStyle \'日期列的显示格式
        st.Format = "yyyy-MM-dd"
        For c As Integer = 0 To nms.length -1
            Sheet(0, c).Value = caps(c) \'指定列标题
            Sheet.Cols(c).Width = szs(c) \'指定列宽
            If dt.dataCols(nms(c)).IsDate Then \'如果是日期列
                Sheet.Cols(c).Style = st \'设置显示格式
            End If
        Next
        For r As Integer = 0 To drs.Count - 1 \'填入数据
            For c As Integer = 0 To nms.length -1
                Sheet(r +1, c).Value = drs(r)(nms(c))
            Next
        Next
    Next
    book.Sheets.RemoveAt(0)
    Book.Save(dlg.FileName)
    Dim Proc As New Process
    Proc.File = dlg.FileName
    Proc.Start()
End If

--  作者:江南小镇
--  发布时间:2018/12/3 10:12:00
--  
 老师,楼上代码能改成多层表头导出。
--  作者:有点甜
--  发布时间:2018/12/3 10:14:00
--  

参考

 

http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=103833&skin=0

 


--  作者:江南小镇
--  发布时间:2018/12/3 11:33:00
--  
老师休息时间也打捞你,下面代码有点搞不懂,想修改成指定的保存地址。

 Dim dlg As New SaveFileDialog \'定义一个新的SaveFileDialog
dlg.Filter= "Excel文件|*.xls" \'设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then
    Dim dt As DataTable = DataTables("订单管理")
    Dim nms() As String = {"订单_编号","订单_日期","客户名称"} \'要导出的列名
    Dim caps() As String = {"订单_编号","订单_日期","客户名称"} \'对应的列标题
Dim hlvl As Integer = 3
    Dim szs() As Integer = {100,100,100,100} \'对应的列宽
    Dim Book As New XLS.Book \'定义一个Excel工作簿
    For Each mc As String In dt.GetValues("客户名称", "发货状态=true and 订单状态=true and 客户名称 is not null")
        Dim Sheet As XLS.Sheet = Book.Sheets.Add \'引用工作簿的第一个工作表
        sheet.name = mc
        Dim drs = dt.Select("客户名称=\'" & mc & "\' and 发货状态=true and 订单状态=true")
        Dim st As XLS.Style = Book.NewStyle \'日期列的显示格式
        st.Format = "yyyy-MM-dd"
        For c As Integer = 0 To nms.length -1
            Sheet(0, c).Value = caps(c) \'指定列标题
            Sheet.Cols(c).Width = szs(c) \'指定列宽
            If dt.dataCols(nms(c)).IsDate Then \'如果是日期列
                Sheet.Cols(c).Style = st \'设置显示格式
            End If
        Next
       




\'================
Dim jz As xls.style = book.NewStyle
jz.AlignHorz = XLS.AlignHorzEnum.Center
jz.AlignVert = XLS.AlignVertEnum.Center
For c As Integer = 0 To nms.length -1
    Dim ary() As String = caps(c).split("_")
    For i As Integer = 0 To ary.length-1
        sheet(i, c).value = ary(i)
        sheet(i, c).Style = jz
    Next
Next
For i As Integer = 0 To hlvl-1
    Dim pi As Integer = 0
    For c As Integer = 0 To nms.length -2
        If sheet(i,c).text = sheet(i,c+1).text Then
            Dim flag As Boolean = True
            For k As Integer = i-1 To 0 Step -1
                If sheet(k,c).value <> sheet(k,c+1).value Then
                    flag = False
                End If
            Next
            If flag Then
                pi += 1
            Else
                pi = 0
            End If
        Else
            sheet.MergeCell(i, c-pi, 1, pi+1)
            pi = 0
        End If
    Next
    sheet.MergeCell(i, nms.length-pi-1, 1, pi+1)
Next

For c As Integer = 0 To nms.length -1
    Dim pi As Integer = 0
    For i As Integer = hlvl-1 To 0 Step -1
        If sheet(i, c).text = "" Then
            pi += 1
        Else
            sheet.MergeCell(i, c, pi+1, 1)
            Exit For
        End If
    Next
Next
\'-----------------------
 For r As Integer = 0 To drs.Count - 1 \'填入数据
            For c As Integer = 0 To nms.length -1
                Sheet(r +1, c).Value = drs(r)(nms(c))
            Next
        Next
    Next
    book.Sheets.RemoveAt(0)
    Book.Save(dlg.FileName)
   Dim Proc As New Process
   Proc.File = dlg.FileName
   Proc.Start()
book.save("H:\\test.xls")
End If