2012年1月29日 星期日

VB.Net Image 轉 Excel


Dim image As New Bitmap("C:\test.jpg")
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range

xlApp = New Excel.Application
xlApp.DisplayAlerts = False      
xlApp.Visible = True
xlBook = xlApp.Workbooks.Add()

'縮小 Excel Window
xlBook.Windows(1).Zoom = 10

xlSheet = xlBook.Sheets(1)

For i As Integer = 1 To image.Height - 1
    For j As Integer = 1 To image.Width - 1

        xlRange = xlSheet.Cells(i, j)
        xlRange.Interior.Color = ColorTranslator.ToWin32(image.GetPixel(j, i))

        '儲存格長寬設定
        xlRange.ColumnWidth = 2
        xlRange.RowHeight = 15

    Next j
Next i
xlBook.SaveAs("c:\1.xls")
xlBook.Close()
xlApp.Quit()

沒有留言:

張貼留言