OWC(Office Web Component)的使用
2006-06-26 22:36
399 查看
OWC(Office Web Component)是Microsoft Office中自带的Web报表组件工具,这个工具虽远远不及Crystal Report,但是没有Lisence的问题,只要公司购买了Office就可以免费使用这个报表工具,所以很多公司在实现报表功能时选用OWC来实现,遗憾的是该工具没有任何图形化的操作界面,只是提供了一些自动化调用接口。下面是我在工作中写得一个调用OWC生成报表的通用类,贴于此处与大家分享。
'General OWC class
Imports OWC 'OWC9.0(Office2000)
Imports System.IO
Public Class OWC
Public Sub New()
End Sub
Public Function GetOwcStr(ByVal dt As DataTable, ByVal ColumnName As String) As String
'OWC11中以/t为间隔符间隔字符串,OWC9中以ControlChars.Tab为间隔符间隔字符串
Dim strReturn As String = ""
For i As Integer = 0 To dt.Rows.Count - 1
strReturn = strReturn + dt.Rows(i)(ColumnName).ToString() + ControlChars.Tab
Next
If strReturn.IndexOf(ControlChars.Tab) >= 0 Then
strReturn = strReturn.Substring(0, strReturn.LastIndexOf(ControlChars.Tab))
End If
Return strReturn
End Function
Public Function BuildColumnClusteredChart(ByVal strPath As String, ByVal chartCategoriesStr As String, ByVal chartValues1Str As String, ByVal chartvalues2Str As String, ByVal TitleCaption As String, ByVal AxesYTitle As String, ByVal AxesXTitle As String, ByVal Series1Caption As String, ByVal Series2Caption As String) As String
RemoveFile(strPath)
Dim oChartSpace As ChartSpaceClass = New ChartSpaceClass
oChartSpace.Charts.Add(0)
oChartSpace.Border.Color = "white"
oChartSpace.Charts(0).Type = ChartChartTypeEnum.chChartTypeColumnClustered
'标题
oChartSpace.Charts(0).HasTitle = True
oChartSpace.Charts(0).Title.Font.Name = "黑体"
oChartSpace.Charts(0).Title.Font.Size = 14
oChartSpace.Charts(0).Title.Caption = TitleCaption '
'添加数据系列1
oChartSpace.Charts(0).SeriesCollection.Add(0)
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection.Add()
'显示数值
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).HasPercentage = False
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).HasValue = True
'字体
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Name = "verdana"
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Size = 10
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Bold = True
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Color = "white"
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Position = ChartDataLabelPositionEnum.chLabelPositionCenter
'数据源
oChartSpace.Charts(0).SeriesCollection(0).SetData(ChartDimensionsEnum.chDimCategories, Convert.ToInt32(ChartSpecialDataSourcesEnum.chDataLiteral), chartCategoriesStr)
oChartSpace.Charts(0).SeriesCollection(0).SetData(ChartDimensionsEnum.chDimValues, Convert.ToInt32(ChartSpecialDataSourcesEnum.chDataLiteral), chartValues1Str)
'添加数据系列2
oChartSpace.Charts(0).SeriesCollection.Add(1)
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection.Add()
'显示数值
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).HasPercentage = False
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).HasValue = True
'字体
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Font.Name = "verdana"
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Font.Size = 10
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Font.Bold = True
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Font.Color = "white"
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Position = ChartDataLabelPositionEnum.chLabelPositionCenter
'数据源
oChartSpace.Charts(0).SeriesCollection(1).SetData(ChartDimensionsEnum.chDimCategories, Convert.ToInt32(ChartSpecialDataSourcesEnum.chDataLiteral), chartCategoriesStr)
oChartSpace.Charts(0).SeriesCollection(1).SetData(ChartDimensionsEnum.chDimValues, Convert.ToInt32(ChartSpecialDataSourcesEnum.chDataLiteral), chartvalues2Str)
'坐标轴:横轴
oChartSpace.Charts(0).Axes(0).HasTitle = True
oChartSpace.Charts(0).Axes(0).Title.Font.Name = "宋体"
oChartSpace.Charts(0).Axes(0).Title.Font.Size = 10
oChartSpace.Charts(0).Axes(0).Title.Caption = AxesXTitle '
'坐标轴:纵轴
oChartSpace.Charts(0).Axes(1).HasTitle = True
oChartSpace.Charts(0).Axes(1).Title.Font.Name = "宋体"
oChartSpace.Charts(0).Axes(1).Title.Font.Size = 10
oChartSpace.Charts(0).Axes(1).Title.Caption = AxesYTitle '
'图例
oChartSpace.HasChartSpaceLegend = True
oChartSpace.ChartSpaceLegend.Font.Name = "宋体"
oChartSpace.ChartSpaceLegend.Font.Size = 9
oChartSpace.ChartSpaceLegend.Position = ChartLegendPositionEnum.chLegendPositionBottom
'图例提示
oChartSpace.Charts(0).SeriesCollection(0).Caption = "=" + Series1Caption
oChartSpace.Charts(0).SeriesCollection(1).Caption = "=" + Series2Caption
Dim pictureName As String = Guid.NewGuid().ToString() + ".gif"
oChartSpace.ExportPicture(strPath + pictureName, "gif", 730, 320)
Return "../Images/temp/" + pictureName
End Function
Private Sub RemoveFile(ByVal strPath As String)
Dim di As DirectoryInfo = New DirectoryInfo(strPath)
Dim fis As FileInfo() = di.GetFiles()
Dim fi As FileInfo
For Each fi In fis
If fi.Extension.ToString() = ".gif" Then
If DateDiff(DateInterval.Day, fi.CreationTime, DateTime.Now.Date) > 0 Then
fi.Delete()
End If
End If
Next
End Sub
End Class
'General OWC class
Imports OWC 'OWC9.0(Office2000)
Imports System.IO
Public Class OWC
Public Sub New()
End Sub
Public Function GetOwcStr(ByVal dt As DataTable, ByVal ColumnName As String) As String
'OWC11中以/t为间隔符间隔字符串,OWC9中以ControlChars.Tab为间隔符间隔字符串
Dim strReturn As String = ""
For i As Integer = 0 To dt.Rows.Count - 1
strReturn = strReturn + dt.Rows(i)(ColumnName).ToString() + ControlChars.Tab
Next
If strReturn.IndexOf(ControlChars.Tab) >= 0 Then
strReturn = strReturn.Substring(0, strReturn.LastIndexOf(ControlChars.Tab))
End If
Return strReturn
End Function
Public Function BuildColumnClusteredChart(ByVal strPath As String, ByVal chartCategoriesStr As String, ByVal chartValues1Str As String, ByVal chartvalues2Str As String, ByVal TitleCaption As String, ByVal AxesYTitle As String, ByVal AxesXTitle As String, ByVal Series1Caption As String, ByVal Series2Caption As String) As String
RemoveFile(strPath)
Dim oChartSpace As ChartSpaceClass = New ChartSpaceClass
oChartSpace.Charts.Add(0)
oChartSpace.Border.Color = "white"
oChartSpace.Charts(0).Type = ChartChartTypeEnum.chChartTypeColumnClustered
'标题
oChartSpace.Charts(0).HasTitle = True
oChartSpace.Charts(0).Title.Font.Name = "黑体"
oChartSpace.Charts(0).Title.Font.Size = 14
oChartSpace.Charts(0).Title.Caption = TitleCaption '
'添加数据系列1
oChartSpace.Charts(0).SeriesCollection.Add(0)
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection.Add()
'显示数值
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).HasPercentage = False
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).HasValue = True
'字体
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Name = "verdana"
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Size = 10
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Bold = True
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Font.Color = "white"
oChartSpace.Charts(0).SeriesCollection(0).DataLabelsCollection(0).Position = ChartDataLabelPositionEnum.chLabelPositionCenter
'数据源
oChartSpace.Charts(0).SeriesCollection(0).SetData(ChartDimensionsEnum.chDimCategories, Convert.ToInt32(ChartSpecialDataSourcesEnum.chDataLiteral), chartCategoriesStr)
oChartSpace.Charts(0).SeriesCollection(0).SetData(ChartDimensionsEnum.chDimValues, Convert.ToInt32(ChartSpecialDataSourcesEnum.chDataLiteral), chartValues1Str)
'添加数据系列2
oChartSpace.Charts(0).SeriesCollection.Add(1)
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection.Add()
'显示数值
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).HasPercentage = False
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).HasValue = True
'字体
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Font.Name = "verdana"
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Font.Size = 10
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Font.Bold = True
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Font.Color = "white"
oChartSpace.Charts(0).SeriesCollection(1).DataLabelsCollection(0).Position = ChartDataLabelPositionEnum.chLabelPositionCenter
'数据源
oChartSpace.Charts(0).SeriesCollection(1).SetData(ChartDimensionsEnum.chDimCategories, Convert.ToInt32(ChartSpecialDataSourcesEnum.chDataLiteral), chartCategoriesStr)
oChartSpace.Charts(0).SeriesCollection(1).SetData(ChartDimensionsEnum.chDimValues, Convert.ToInt32(ChartSpecialDataSourcesEnum.chDataLiteral), chartvalues2Str)
'坐标轴:横轴
oChartSpace.Charts(0).Axes(0).HasTitle = True
oChartSpace.Charts(0).Axes(0).Title.Font.Name = "宋体"
oChartSpace.Charts(0).Axes(0).Title.Font.Size = 10
oChartSpace.Charts(0).Axes(0).Title.Caption = AxesXTitle '
'坐标轴:纵轴
oChartSpace.Charts(0).Axes(1).HasTitle = True
oChartSpace.Charts(0).Axes(1).Title.Font.Name = "宋体"
oChartSpace.Charts(0).Axes(1).Title.Font.Size = 10
oChartSpace.Charts(0).Axes(1).Title.Caption = AxesYTitle '
'图例
oChartSpace.HasChartSpaceLegend = True
oChartSpace.ChartSpaceLegend.Font.Name = "宋体"
oChartSpace.ChartSpaceLegend.Font.Size = 9
oChartSpace.ChartSpaceLegend.Position = ChartLegendPositionEnum.chLegendPositionBottom
'图例提示
oChartSpace.Charts(0).SeriesCollection(0).Caption = "=" + Series1Caption
oChartSpace.Charts(0).SeriesCollection(1).Caption = "=" + Series2Caption
Dim pictureName As String = Guid.NewGuid().ToString() + ".gif"
oChartSpace.ExportPicture(strPath + pictureName, "gif", 730, 320)
Return "../Images/temp/" + pictureName
End Function
Private Sub RemoveFile(ByVal strPath As String)
Dim di As DirectoryInfo = New DirectoryInfo(strPath)
Dim fis As FileInfo() = di.GetFiles()
Dim fi As FileInfo
For Each fi In fis
If fi.Extension.ToString() = ".gif" Then
If DateDiff(DateInterval.Day, fi.CreationTime, DateTime.Now.Date) > 0 Then
fi.Delete()
End If
End If
Next
End Sub
End Class
相关文章推荐
- 1分钟创建图表[利用Office Web Component OWC组件]
- 使用Office Web Components (OWC)来创建统计图的一个数据类。
- 在ASP.NET中使用Office Web Components (OWC)创建统计图
- 在ASP.NET中使用Office Web Components (OWC)创建统计图
- Office Web Components(OWC)绘图控件使用11个问答
- 在ASP.NET中使用Office Web Components (OWC)创建统计图
- Office Web Components(OWC)绘图控件使用11个问答[转]
- 在ASP.NET中使用Office Web Components (OWC)创建统计图
- 在ASP.NET中使用Office Web Components (OWC)创建统计图
- 如何用owc(office web component)画图表
- 在ASP.NET中使用Office Web Components (OWC)创建统计图
- 在ASP.NET中使用Office Web Components (OWC)创建统计图
- Office Web Components(OWC)绘图控件使用11个问答
- Office Web Components(OWC)绘图控件使用11个问答[转]
- 在ASP.NET中使用Office Web Components (OWC)创建统计图
- Office Web Components(OWC)绘图控件使用11个问答
- MOSS 2010:安装和使用Office Web Apps
- WEB下使用的OFFICE控件介绍
- WEB下使用的OFFICE控件介绍
- ComponentOne WebChart的使用