none
[VB 2008] 請問在程式執行時,如何將表單畫面整個貼入excel裡? RRS feed

  • 問題

  • 大大好
    小弟在這邊想請問在執行程式時,
    如何將表單畫面整個存入excel裡?
    該如何撰寫程式碼呢?
    感謝大大的回覆 
    2009年6月3日 上午 06:05

解答

  • Imports Microsoft.Office.Interop
    
    Public Class Form1
    
        ' Button1 Click 後,將表單畫面整個存入excel裡
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' ========= 將表單畫面儲存成圖檔 Form.bmp ==========start
            Dim myImage As New Bitmap(Me.Width, Me.Height)
            Dim g = Graphics.FromImage(myImage)
            Dim a As Point
    
            g.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, New Size(Me.Width, Me.Height))
            Dim dc1 As IntPtr = g.GetHdc()
            g.ReleaseHdc(dc1)
            myImage.Save(System.Windows.Forms.Application.StartupPath & "/Form.bmp")
            ' ========= 將表單畫面儲存成圖檔 Form.bmp ==========End
    
            ' ========= 將圖檔 Form.bmp 存入excel裡==========start
            Dim oXL As Excel.Application
            Dim oWB As Excel.Workbook
            Dim oSheet As Excel.Worksheet
    
            ' Start Excel and get Application object.
            oXL = CreateObject("Excel.Application")
            oWB = oXL.Workbooks.Add
            oSheet = oWB.ActiveSheet
    
            oSheet.Visible = True
            Dim pic As String = System.Windows.Forms.Application.StartupPath & "/Form.bmp"
    
            oSheet.Range("B1:B1").Select() 'This is where the top left corner of picture will be
    
            Dim opicture As Object
            opicture = oSheet.Pictures.Insert(pic)
    
            oXL.Visible = True
            oXL = Nothing
            oWB = Nothing
            oSheet = Nothing
            ' ========= 將圖檔 Form.bmp 存入excel裡==========end
        End Sub
    End Class
    
    • 已標示為解答 yan0937 2009年6月3日 上午 11:43
    2009年6月3日 上午 07:09
    版主

所有回覆

  • Imports Microsoft.Office.Interop
    
    Public Class Form1
    
        ' Button1 Click 後,將表單畫面整個存入excel裡
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' ========= 將表單畫面儲存成圖檔 Form.bmp ==========start
            Dim myImage As New Bitmap(Me.Width, Me.Height)
            Dim g = Graphics.FromImage(myImage)
            Dim a As Point
    
            g.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, New Size(Me.Width, Me.Height))
            Dim dc1 As IntPtr = g.GetHdc()
            g.ReleaseHdc(dc1)
            myImage.Save(System.Windows.Forms.Application.StartupPath & "/Form.bmp")
            ' ========= 將表單畫面儲存成圖檔 Form.bmp ==========End
    
            ' ========= 將圖檔 Form.bmp 存入excel裡==========start
            Dim oXL As Excel.Application
            Dim oWB As Excel.Workbook
            Dim oSheet As Excel.Worksheet
    
            ' Start Excel and get Application object.
            oXL = CreateObject("Excel.Application")
            oWB = oXL.Workbooks.Add
            oSheet = oWB.ActiveSheet
    
            oSheet.Visible = True
            Dim pic As String = System.Windows.Forms.Application.StartupPath & "/Form.bmp"
    
            oSheet.Range("B1:B1").Select() 'This is where the top left corner of picture will be
    
            Dim opicture As Object
            opicture = oSheet.Pictures.Insert(pic)
    
            oXL.Visible = True
            oXL = Nothing
            oWB = Nothing
            oSheet = Nothing
            ' ========= 將圖檔 Form.bmp 存入excel裡==========end
        End Sub
    End Class
    
    • 已標示為解答 yan0937 2009年6月3日 上午 11:43
    2009年6月3日 上午 07:09
    版主

  • 非常感謝小歐ou大大
    答案十分詳盡

    2009年6月3日 上午 11:44