VS2005 WinForm中的PrintPreviewDialog控件预览时如何导出其它格式的文件?工具箱自带的这个控件没有导出功能哦,只能直接打印,现在我要求这个控件要像CrystalReportViewer一样能够导出其它Excel,PDF,Word等其它格式的文件,如何实现?如果用派生类的话,这个类该如何写?

解决方案 »

  1.   

    MARK 对其他文件格式的操作 都要建立对应的对象 进行操作才能使程序内部明白文件的格式 进行保存等操作
      

  2.   

    可以把文件中的数据用Stream读出来,然后用PrintDocument把读到的数据一行行描绘出来预览,这样不管对什么文件都是一样的。
      

  3.   

    数据是通过Grapics对象来描绘,关键是预览后的数据的导出问题?PrintPreviewDialog上的数据只能直接打印,不能导出哦!
      

  4.   


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

  5.   

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

  6.   

    继承PrintPreviewDialog
    获取工具栏
    然后在工具栏添加自己的按钮
    在按钮事件中添加导出代码Imports System.Reflection
    Imports System.Windows.Forms
    Imports System.ResourcesPublic 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 PropertyEnd Class