printPreviewDialog如何导出Excel等其它格式的文件?

etherealkite 2008-06-17 10:33:11
VS2005 WinForm中的PrintPreviewDialog控件预览时如何导出其它格式的文件?工具箱自带的这个控件没有导出功能哦,只能直接打印,现在我要求这个控件要像CrystalReportViewer一样能够导出其它Excel,PDF,Word等其它格式的文件,如何实现?如果用派生类的话,这个类该如何写?
...全文
316 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
etherealkite 2008-06-20
  • 打赏
  • 举报
回复
不明白8楼说的意思?怎么读取预览页面的数据?
etherealkite 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 live_7sky 的回复:]

本来 就不提供导出功能 ,如果你要预览的时候,导出EXCEL ,
只有模拟一个页面,然后读取页面的内容,然后导出到EXCEL
[/Quote]
怎么读取页面内容,然后导出Excel?可否提供代码参考。
live_7sky 2008-06-19
  • 打赏
  • 举报
回复

本来 就不提供导出功能 ,如果你要预览的时候,导出EXCEL ,
只有模拟一个页面,然后读取页面的内容,然后导出到EXCEL
live_7sky 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 etherealkite 的回复:]
引用 5 楼 live_7sky 的回复:

本来 就不提供导出功能 ,如果你要预览的时候,导出EXCEL ,
只有模拟一个页面,然后读取页面的内容,然后导出到EXCEL

怎么读取页面内容,然后导出Excel?可否提供代码参考。
[/Quote]

读取文本的方式,然后通过正则取来 。
allanli 2008-06-19
  • 打赏
  • 举报
回复
继承PrintPreviewDialog
获取工具栏
然后在工具栏添加自己的按钮
在按钮事件中添加导出代码

Imports System.Reflection
Imports System.Windows.Forms
Imports System.Resources

Public Class PrintPreviewDialogEx
Inherits System.Windows.Forms.PrintPreviewDialog

Protected WithEvents pToolBar As System.Windows.Forms.ToolBar
Private nInitialImages As Integer
Private AddedButtons As ArrayList

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

pToolBar = Me.ParentToolbar

Dim b As Button = GetCloseButton()
b.Text = "关闭"
Me.Text = "打印预览"
AddHandler pToolBar.ButtonClick, AddressOf AddedButtons_Click
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Public WithEvents AddedButtonsImageList As System.Windows.Forms.ImageList
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(PrintPreviewDialogEx))
Me.AddedButtonsImageList = New System.Windows.Forms.ImageList(Me.components)
'
'AddedButtonsImageList
'
Me.AddedButtonsImageList.ImageSize = New System.Drawing.Size(16, 16)
Me.AddedButtonsImageList.TransparentColor = System.Drawing.Color.Transparent
'
'PrintPreviewDialogEx
'
Me.ClientSize = New System.Drawing.Size(864, 543)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "PrintPreviewDialogEx"

End Sub

#End Region

Public Event AddedButtonsClick As System.Windows.Forms.ToolBarButtonClickEventHandler

Private Sub AddedButtons_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs)
Dim i As Integer
For i = 0 To AddedButtons.Count - 1
If e.Button Is AddedButtons(i) Then
RaiseEvent AddedButtonsClick(Me, e)
Return
End If
Next
End Sub

Public Sub AddToolBarButtons(ByVal Buttons() As System.Windows.Forms.ToolBarButton)
Dim imgList As ImageList = pToolBar.ImageList
Dim i As Integer
nInitialImages = imgList.Images.Count
AddedButtons = New ArrayList
For i = 0 To AddedButtonsImageList.Images.Count - 1
imgList.Images.Add(AddedButtonsImageList.Images.Item(i))
Next

Dim initw As Integer = 0
For i = 0 To Buttons.GetLength(0) - 1
AddedButtons.Add(Buttons(i))
If Buttons(i).ImageIndex >= 0 Then
Buttons(i).ImageIndex += nInitialImages
End If
pToolBar.Buttons.Add(Buttons(i))
initw += pToolBar.Buttons(pToolBar.Buttons.Count - 1).Rectangle.Width
Next
Dim s As System.Drawing.Size = Me.MinimumSize
s.Width += initw
Me.MinimumSize = s
Dim b As Button = GetCloseButton()
b.Left += initw
End Sub

Private Function GetCloseButton() As Button
Dim fi As FieldInfo = GetType(System.Windows.Forms.PrintPreviewDialog).GetField("closeButton", BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance)
Return CType(fi.GetValue(Me), System.Windows.Forms.Button)
End Function

Public ReadOnly Property ParentToolbar() As System.Windows.Forms.ToolBar
Get
Dim fi As FieldInfo = GetType(System.Windows.Forms.PrintPreviewDialog).GetField("toolBar1", BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance)
Return CType(fi.GetValue(Me), System.Windows.Forms.ToolBar)
End Get
End Property

End Class
etherealkite 2008-06-17
  • 打赏
  • 举报
回复
1楼说的有道理,Up一下,还有谁解决了这个问题没有?帮顶有分哦!
yilanwuyu123 2008-06-17
  • 打赏
  • 举报
回复
MARK 对其他文件格式的操作 都要建立对应的对象 进行操作才能使程序内部明白文件的格式 进行保存等操作
etherealkite 2008-06-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cxiaoxifeng 的回复:]
可以把文件中的数据用Stream读出来,然后用PrintDocument把读到的数据一行行描绘出来预览,这样不管对什么文件都是一样的。
[/Quote]
数据是通过Grapics对象来描绘,关键是预览后的数据的导出问题?PrintPreviewDialog上的数据只能直接打印,不能导出哦!
cxiaoxifeng 2008-06-17
  • 打赏
  • 举报
回复
可以把文件中的数据用Stream读出来,然后用PrintDocument把读到的数据一行行描绘出来预览,这样不管对什么文件都是一样的。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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