以文本方式查看主题 - 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=27316) |
-- 作者:samica -- 发布时间:2012/12/28 14:03:00 -- [分享]+[求助]直接向excel工作表打印的通用代码。 [分享]直接向excel工作表打印的通用代码: Dim app As New MSExcel.Application Dim book As MSExcel.Workbook = app.Workbooks.Open(ProjectPath & "Reports\\book1.xls") Dim sheet As MSExcel.Worksheet = book.Sheets("Sheet2") Dim tb As Table = Tables("表A") Dim ColNames As String() = New String(){"列1", "列2", "列3"} For c As Integer = 0 To ColNames.Length - 1 For r As Integer = 0 To tb.Rows.Count -1 \'开始填入该列内容 sheet.Cells(r + 5, c + 1).value = tb.Rows(r)(ColNames(c)) Next Next app.Visible = True sheet.PrintPreview() book.saved=True book.Close() app.Quit() [求助]如何在上面的代码中加入条件:譬如在窗体中加入两个datetimepicker控件 Dim rq_Stat As Date = e.Form.Controls("datetimepicker1").value Dim rq_End As Date = e.Form.Controls("datetimepicker2").value 进行日期时间条件限制,请高手指导!谢谢! |
-- 作者:samica -- 发布时间:2012/12/28 14:26:00 -- 林老师在吗?您给看看,给个意见。谢谢! |
-- 作者:lin_hailun -- 发布时间:2012/12/28 14:42:00 -- Dim rq_Stat As Date = e.Form.Controls("datetimepicker1").value Dim rq_End As Date = e.Form.Controls("datetimepicker2").value Dim app As New MSExcel.Application Dim book As MSExcel.Workbook = app.Workbooks.Open(ProjectPath & "Reports\\book1.xls") Dim sheet As MSExcel.Worksheet = book.Sheets("Sheet2") Dim tb As Table = Tables("表A") Dim ColNames As String() = New String(){"列1", "列2", "列3"} For c As Integer = 0 To ColNames.Length - 1 For r As Integer = 0 To tb.Rows.Count -1 \'开始填入该列内容 If tb.Cols(ColNames(c)).IsDate Then If tb.Rows(r)(ColNames(c)) >= rq_start AndAlso tb.Rows(r)(ColNames(c)) <= rq_end Then sheet.Cells(r + 5, c + 1).value = tb.Rows(r)(ColNames(c)) End If Else sheet.Cells(r + 5, c + 1).value = tb.Rows(r)(ColNames(c)) End If Next Next app.Visible = True sheet.PrintPreview() book.saved=True book.Close() app.Quit()
[此贴子已经被作者于2012-12-28 15:05:54编辑过]
|
-- 作者:lin_hailun -- 发布时间:2012/12/28 14:44:00 -- 导出数据也可以这样。 http://www.foxtable.com/help/topics/1148.htm |
-- 作者:samica -- 发布时间:2012/12/28 14:52:00 -- 林老师,点确定后出现错误框: |
-- 作者:lin_hailun -- 发布时间:2012/12/28 15:06:00 -- 已修改,再复制应该可以了。 |
-- 作者:samica -- 发布时间:2012/12/28 15:45:00 -- 林老师,能否指定列名,进行条件限制? 此主题相关图片如下:snap2.png |
-- 作者:lin_hailun -- 发布时间:2012/12/28 15:52:00 -- 你说的限制,次序应该错了。你应该在数据表里筛选以后再打印到excel里的。 |
-- 作者:samica -- 发布时间:2012/12/28 16:04:00 -- 林老师,我不知道如何引用筛选的值,请您指导,谢谢! Dim app As New MSExcel.Application Dim book As MSExcel.Workbook = app.Workbooks.Open(ProjectPath & "Reports\\book1.xls") Dim sheet As MSExcel.Worksheet = book.Sheets("Sheet2") Dim rq_Stat As Date = e.Form.Controls("datetimepicker1").value Dim rq_End As Date = e.Form.Controls("datetimepicker2").value Dim tb As Table = Tables("表A") Tables("
表A ").Filter = "[列3] >= #"& rq_stat &"# And [列3] <= #"& rq_end &"#" Dim ColNames As String() = New String(){"列1", "列2", "列3"} For c As Integer = 0 To ColNames.Length - 1 For r As Integer = 0 To tb.Rows.Count -1 \'开始填入该列内容 sheet.Cells(r + 5, c + 1).value = tb.Rows(r)(ColNames(c)) Next Next app.Visible = True sheet.PrintPreview() book.saved=True book.Close() app.Quit() [此贴子已经被作者于2012-12-28 16:11:12编辑过]
|
-- 作者:lin_hailun -- 发布时间:2012/12/28 16:13:00 -- 楼上,什么叫做引用筛选的值,不就是你上面那样写的么? |