?
Solved

(Excel Automation) Load Excel charts into VB.Net

Posted on 2003-02-27
5
Medium Priority
?
1,224 Views
Last Modified: 2007-12-19
I would like to plot (EXCEL) charts and display it in my VBA.

I have tried server methods


Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oWS As Excel.Worksheet
Dim oChart As Excel.Chart

oXL = CreateObject("Excel.Application")
.....
.....


Method 1)
oWS.ChartObjects(1).chart.chartarea.select()
oWS.ChartObjects(1).chart.chartarea.copypicture()

Dim Idata As IDataObject = Clipboard.GetDataObject()
picturebox.image = CType(Idata.GetData(DataFormats.Bitmap), Image)

I try to copy the chart direclt but it fail. I found that the "picture" in clipboard is not in Bitmap format, but MetaFilePic and I don't know how to load the "picutre" into the picture box directly.


Method 2)
oWS.Range("B5", "H14").Select()
oWS.Range("B5", "H14").Copy()

Dim Idata As IDataObject = Clipboard.GetDataObject()
picturebox.image = CType(Idata.GetData(DataFormats.Bitmap), Image)

This time it works, I set the chart width and Height and paste it on B5:H14. However, sometimes it is diffcuclt to control the size of the chart and it is urgy.


Method 3)
Dim Fname As String
Dim myDataObject As New DataObject()

Fname = "c:\" & oWS.ChartObjects(1).Name & ".gif"
oWS.ChartObjects(1).chart.Export(FileName:=Fname, FilterName:="GIF")

picturebox.Image = Image.FromFile(Fname)

This method also work, but I think this method is not good because I will generate many files. On the otherhand, once I load the file, I can't kill it because the system is using the gif(file violation).

Can someone teach me how to load the (EXCEL) Charts into VB.net?
If yes, can you post the syntax? Thx
0
Comment
Question by:leokwan2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 9

Expert Comment

by:bhagyesht
ID: 8040176
I would say method 3 as the best in case u want to use the 'Excel' for chart in vb. yes u shall get a lot of files but then u can clean the file once u r through. like on closing the application or on startup just make sure that the files are in a folder and empty the folder on either the unload or load.
0
 

Accepted Solution

by:
leokwan2000 earned 0 total points
ID: 8072234
This method works, hope it can help others.

        Fname = "c:\" & oWS.ChartObjects(1).Name & ".gif"
        oWS.ChartObjects(1).chart.Export(FileName:=Fname, FilterName:="GIF")

        Dim myReader As FileStream = File.OpenRead(Fname)
        picturebox1.Image = Image.FromStream(myReader)
        myReader.Close()
       
        Try
            File.Delete(Fname)

        Catch exc As Exception
            ' Show error to user.
            MsgBox("no hope")
        End Try
0
 

Expert Comment

by:CleanupPing
ID: 9447460
leokwan2000:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9449244
refund points
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question