好久没有写blog了。
OWC可以生成图片,Excel 也可以生成图片。其中的对象接口都非常接近。
这里记录一下如何使用Excel来生成图片。
用Excel生成图片没有什么难度。
主要是几个概念。
- 系列-Series
- 数据区域
- Chart ,
- ChartArea
- Chart Type
写了一些东西进行了封装,没有什么技术含量在里面(很久都没有技术含量了,汗!)
1: Dim curve As ICurvePrice = Curvegraphic.getInstance
2: Dim serieslist As New seriesCollection
3: serieslist.Add(New seriesItem("ser1"))
4: serieslist.Add(New seriesItem("ser2"))
5: Dim axisX As New AxesX("PriceDate")
6:
7: curve.DefaultImageConfiguration.AutoExpendWidth = True
8: curve.createCurveExcle(AppPath + "\exceltemplate\g5.xls", getTestTable, serieslist, axisX)
9: curve.ExportFile()
10: Dim imagepath As String = curve.ExportImageFile(Nothing, System.Guid.NewGuid.ToString())
11: Dim ima As System.Drawing.Image = System.Drawing.Image.FromFile(imagepath)
12: PictureBox1.Image = ima
13:
14:
15: curve.closeworkbook()
By the way, 这里使用了单件模式。其中有一个获取Excel Application的代码
1: Private Function getApp() As Excel.Application
2: If _app Is Nothing Then
3: _app = New Excel.Application
4: Else
5: Try
6: _app.DisplayAlerts = False
7: Catch ex As Exception
8: _app = New Excel.Application
9: End Try
10: End If
11: _app.DisplayAlerts = False
12: _app.ScreenUpdating = False
13: Return _app
14: End Function
当时我使用的这个代码
1: Private Function getApp() As Excel.Application
2: If _app Is Nothing Then
3: _app = New Excel.Application
4:
5: End If
6: _app.DisplayAlerts = False
7: _app.ScreenUpdating = False
8: Return _app
9: End Function
发现一个问题,如果用户直接在任务管理器中结束Excel进程,而第二次调用的时候,实用getApp的时候,_app并不为空,结果会在_app.DisplayAlerts = false,这行报错。因为这个时候_app指向的对象已经不存在了。
那么,有没有其他的办法来判定_app指向的对象已经不能使用了呢?
项目文件