谁有vb.net的datagridview控件内容打印导到excel表然后在打印。的代码。给小第一段。给高分呀。谢谢了

jiaochengman 2009-02-04 03:28:51
谁有vb.net的datagridview控件内容打印导到excel表然后在打印。的代码。给小第一个
...全文
104 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaochengman 2009-02-05
  • 打赏
  • 举报
回复
是的。你有这方面的例子能给我一个吗、谢谢
一只熊猫 2009-02-05
  • 打赏
  • 举报
回复

#Region "打印家访单"
Private Sub 打印ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打印ToolStripMenuItem.Click
Dim xlsapp As New Excel.Application
Dim xlswkbk As Excel.Workbook
Dim xlsst As Excel.Worksheet
xlswkbk = xlsapp.Workbooks.Add
xlsst = xlswkbk.Sheets(1)
xlsapp.Visible = True
For i As Integer = 0 To TaskDatag.SelectedRows.Count - 1
Try
xlsst.Cells(1, 1) = "地区"
xlsst.Cells(1, 2) = "经销店/派单人"
xlsst.Cells(1, 3) = "单号"
xlsst.Cells(1, 4) = "申请人"
xlsst.Cells(1, 5) = "联系电话"
xlsst.Cells(1, 6) = "家访地点"
xlsst.Cells(1, 7) = "家访需求"
xlsst.Cells(1, 8) = "其它资料"
xlsst.Cells(2, 1) = TaskDatag.SelectedRows(i).Cells("地区").Value.ToString.Trim
xlsst.Cells(2, 2) = TaskDatag.SelectedRows(i).Cells("4S店名称(派单人)").Value.ToString.Trim
xlsst.Cells(2, 3) = TaskDatag.SelectedRows(i).Cells("单号").Value.ToString.Trim
xlsst.Cells(2, 4) = TaskDatag.SelectedRows(i).Cells("申请人姓名").Value.ToString.Trim
xlsst.Cells(2, 5) = TaskDatag.SelectedRows(i).Cells("联系电话").Value.ToString.Trim
xlsst.Cells(2, 6) = TaskDatag.SelectedRows(i).Cells("家访地点").Value.ToString.Trim
xlsst.Cells(2, 7) = TaskDatag.SelectedRows(i).Cells("家访需求").Value.ToString.Trim
Dim other As DataTable = myfunc.getdatatable("select 联系类型,数据 from information where 任务编号='" & TaskDatag.SelectedRows(i).Cells("任务编号").Value.ToString.Trim & "'") ‘getdatatable是一个返回查询结果的函数
Dim str As String = ""
For Each otherrow As DataRow In other.Rows
str = str & otherrow("联系类型").ToString.Trim & ":" & otherrow("数据").ToString.Trim & vbCrLf
Next
xlsst.Cells(2, 8) = str
xlsapp.Range("B2").Select() '从这里开始都是Excel的版式设置
With xlsapp.Selection
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With
xlsapp.Range("G2").Select()
With xlsapp.Selection
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With
xlsapp.Range("F2").Select()
With xlsapp.Selection
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With
xlsst.Columns("F:F").ColumnWidth = 12
xlsst.Columns("G:G").ColumnWidth = 41.5
xlsst.Rows("2:2").RowHeight = 173.25
xlsst.Columns("B:B").ColumnWidth = 18
xlsst.Columns("E:E").ColumnWidth = 12
xlsst.Columns("H:H").ColumnWidth = 26
xlsapp.Range("A1:H2").Select()
With xlsapp.Selection.Borders(7)
.LineStyle = 1
.Weight = 2
.ColorIndex = -4105
End With
With xlsapp.Selection.Borders(8)
.LineStyle = 1
.Weight = 2
.ColorIndex = -4105
End With
With xlsapp.Selection.Borders(9)
.LineStyle = 1
.Weight = 2
.ColorIndex = -4105
End With
With xlsapp.Selection.Borders(10)
.LineStyle = 1
.Weight = 2
.ColorIndex = -4105
End With
With xlsapp.Selection.Borders(11)
.LineStyle = 1
.Weight = 2
.ColorIndex = -4105
End With
With xlsapp.Selection.Borders(12)
.LineStyle = 1
.Weight = 2
.ColorIndex = -4105
End With
With xlsapp.ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
xlsapp.ActiveSheet.PageSetup.PrintArea = ""
With xlsapp.ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = -4142
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = 2
.Draft = False
.PaperSize = 9
.FirstPageNumber = -4105
.Order = 1
.BlackAndWhite = False
.Zoom = 80
.PrintErrors = 0
End With '设置结束
xlsapp.Selection.PrintOut(Copies:=1, Collate:=True) '打印
Catch
MessageBox.Show(TaskDatag.SelectedRows(i).Cells("申请人姓名").Value.ToString.Trim & "的家访单打印时出错未知错误,系统已自动忽略。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
Next
If File.Exists(Application.StartupPath & "\temp.xls") Then File.Delete(Application.StartupPath & "\temp.xls")
xlswkbk.SaveAs(Application.StartupPath & "\temp.xls")
xlsapp.Quit()
xlsst = Nothing
xlswkbk = Nothing
xlsapp = Nothing
GC.Collect()
End Sub
#End Region


完整的代码。。我自己程序里的。
功能是:将Taskdatag(DataGridView)里的选择的行导入Excel然后打印,一行生成一个文件。。
你自己改改就好了。
晓轩 2009-02-05
  • 打赏
  • 举报
回复
用水晶报表吧。
程序员Linc 2009-02-05
  • 打赏
  • 举报
回复
帮顶下!!!
别忘了给我分哦!!!~~~~
_NET2004 2009-02-04
  • 打赏
  • 举报
回复
思路:
先导出到Exceld模板,然后执行打印

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧