我正在尝试自学编程,我正在制作一个自动绘图程序,将数据提交到excel并返回图形。代码如下: 保存文件并将其导出:
'在程序激活时,为数据库建立I / O流并加载图形
Private Sub Form1_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
' Open the database
xlWorkBook = xlApp.Workbooks.Open(DatabasePath + "Database.xlsx")
' Set the relevant worksheet
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
' Set the display status of the database
xlApp.Visible = False
' Clear the picture box before exporting to prevent the compiler from accessing a file already in use
If Not (Graph.Image Is Nothing) Then
Graph.Image.Dispose()
End If
' Export the graph
xlWorkSheet.ChartObjects(1).chart.Export(FileName:=DatabasePath + ("Graph.gif"), FilterName:="gif")
' Load the saved graph image into the userform
Graph.Image = Image.FromFile(DatabasePath + ("Graph.gif"))
End Sub
虽然它不会在表单激活时加载文件。 当我使用另一段代码时,会出现系统错误,该代码更新excel文件中的相关单元格并更改绘制的数据。使用userform中的文本框更改事件更新单元格:
Private Sub Proposed_Dollars_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Proposed_Dollars.TextChanged
' Export the data values
xlWorkSheet.Range("Q10").Value = Proposed_Dollars.Text()
' Clear the picture box before exporting to prevent the compiler from accessing a file already in use
If Not (Graph.Image Is Nothing) Then
Graph.Image.Dispose()
End If
' Export the graph
xlWorkSheet.ChartObjects(1).chart.Export(FileName:=DatabasePath + ("Graph.gif"), FilterName:="GIF")
' Load the saved graph image into the userform
Graph.Image = Image.FromFile(DatabasePath + ("Graph.gif"))
End Sub
是否有任何明显的内存泄漏导致程序无法将图形加载到用户表单图片框中? 我感谢任何帮助。
答案 0 :(得分:0)
可以将数据保存回excel文件,但强制它将每个字符都过多绘制图形。您应该做的是从excel文件加载一些变量,允许用户更改数据,这将更改变量,然后使用Paint事件绘制整个图形并使用变量显示线条,矩形,省略号等......看看GDI+
它可以做你需要的一切。