以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]导出EXCEL如何限制导出行数  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=134859)

--  作者:liuzhong
--  发布时间:2019/5/10 15:51:00
--  [求助]导出EXCEL如何限制导出行数
以下内容为程序代码:

1 Dim dt As Table = Tables("订单")
2 Dim nms() As String = {"产品","客户","数量","单价","金额","日期"} \'要导出的列名
3 Dim caps() As String = {"产品名称","用户名称","数量","单价","金额","日期"} \'对应的列标题
4 Dim szs() As Integer = {100,100,80,80,80,120} \'对应的列宽
5 Dim Book As New XLS.Book \'定义一个Excel工作簿
6 Dim Sheet As XLS.Sheet = Book.Sheets(0) \'引用工作簿的第一个工作表
7 Dim st As XLS.Style = Book.NewStyle \'日期列的显示格式
8 st.Format = "yyyy-MM-dd"
9 For c As Integer = 0 To nms.length -1
10 Sheet(0, c).Value = caps(c) \'指定列标题
11 Sheet.Cols(c).Width = szs(c) \'指定列宽
12 If dt.Cols(nms(c)).IsDate Then \'如果是日期列
13 Sheet.Cols(c).Style = st \'设置显示格式
14 End If
15 Next
16 For r As Integer = 0 To dt.Rows.Count - 1 \'填入数据
17 For c As Integer = 0 To nms.length -1
18 Sheet(r +1, c).Value = dt.rows(r)(nms(c))
19 Next
20 Next
21 Dim dlg As New SaveFileDialog \'定义一个新的SaveFileDialog
22 dlg.Filter= "Excel文件|*.xls" \'设置筛选器
23 If dlg.ShowDialog = DialogResult.Ok Then
24 Book.Save(dlg.FileName)
25 Dim Proc As New Process
26 Proc.File = dlg.FileName
27 Proc.Start()
28 End If
29  
请教:导出EXCEL如何限制导出行数。例如:设定导出前60行。我用的是帮助中的代码。

[此贴子已经被作者于2019/5/10 15:52:46编辑过]

--  作者:有点甜
--  发布时间:2019/5/10 15:59:00
--  

 

For r As Integer = 0 To dt.Rows.Count - 1 \'填入数据

 

改成

 

For r As Integer = 0 To iif(dt.Rows.Count>60, 59,  dt.Rows.Count-1) \'填入数据