上一篇中介绍了如何将服务器上的报表获取下来,并将报表的名称显示到ListView控件中,而本片文章主要讲解,如何将所获取到得报表,写入到本地的磁盘中,并且所保存的格式是xml文件。这里使用到的技术除了CrystalReport提供的API,还使用到了VS提供的I/O中的File类库的内容,依然借助于代码来分析这些内容,因为这一部分的代码内容有上千行之多,这里只截取其中一部分代码供我们分析。 首先,我们来看一下是如何利用VS提供的I/O中的File类库的内容的。 'Create a directory Public Function CreateDirectory(ByVal directorypath As String) As DirectoryInfo Return Directory.CreateDirectory(directorypath) End Function 上面这段代码,使用CreateDirectory方法,创建了一个指定地址和名称的文件,这是File类库的一个典型的应用。而这段代码是写在VB的Module中作为一个全局方法来使用的。使用时只需调用CreateDirectory方法,并传给正确的参数即可。 接着,我们来看一下程序的主要部分 'For every InfoObject in InfoObject collection 'Save it to the directory just created. For iReportsCount As Integer = 1 To m_infoReports.Count Application.DoEvents() ProgressCount() 'Declare the reportName reportName = m_infoReports(iReportsCount).Title + "+" + m_infoReports(iReportsCount).ID.ToString() m_reportDs = New DataSet(reportName) With m_infoReports(iReportsCount) 'Create a table for properties Dim table_Properties As DataTable = New DataTable("Properties") 'For every property in properties,add it to the hash table 'and create the table's column by its property name. For Each _property As [Property] In .Properties table_Properties.Columns.Add(_property.Name) m_hash.Add(_property.Name, _property.Value) Next 'Fill the properties table Dim _properties_row As DataRow = table_Properties.NewRow() 'For every fields in the m_hash 'Create a new row for the table. _properties_row.BeginEdit() For Each strkey As String In m_hash.Keys _properties_row(strkey) = m_hash(strkey) Next _properties_row.EndEdit() table_Properties.Rows.Add(_properties_row) 'Add the table to the dataset. m_reportDs.Tables.Add(table_Properties) m_hash.Clear() _properties_row = Nothing table_Properties = Nothing '----------------------------------------------------------------------------------------- 'Create a table for properties.files If .Properties("SI_FILES").Properties.Count > 0 Then Dim table_properties_files As DataTable = New DataTable("Properties_Files") For Each _property As [Property] In .Properties("SI_FILES").Properties table_properties_files.Columns.Add(_property.Name) m_hash.Add(_property.Name, _property.Value) Next 'Fill the properties_files table Dim _properties_Files_row As DataRow = table_properties_files.NewRow() _properties_Files_row.BeginEdit() For Each strkey As String In m_hash.Keys _properties_Files_row(strkey) = m_hash(strkey) Next _properties_Files_row.EndEdit() table_properties_files.Rows.Add(_properties_Files_row) m_reportDs.Tables.Add(table_properties_files) m_hash.Clear() _properties_Files_row = Nothing table_properties_files = Nothing End If ................................ 这里,我仍然采用的For Each的方法,遍历报表数组中的报表,并且遍历每一个报表中的每一个属性,将属性添加到Hash表中,如果找不到任何属性,这边还采用了goto语句,直接去处理找不到任何属性的事件,(goto语句,现在已经淡出程序员的眼光,在这里是不得已才用的这个goto语句,而笔者,在c#当中,也喜欢用goto语句,这只是笔者的一个习惯,但不推崇。)在遍历好每一个表之后,需要建立一个DataTable将每一个表中的记录保存到DataTable中,最后需要条用HashTable的Clear方法,将Hash表中的数据全部清空,然后再去处理遍历到得下一个表中的内容。 关于goto的使用,我是全部放在Catch中的,如下所示: Try ...... Catch GoTo Create_Print_Info End Try 在Vb中定义GoTo到得内容,和其他语言一样,没有区别。实际上,从vb6.0过度过来的程序员,对于GoTo并不陌生,因为我们在处理程序当中的异常时,常常需要使用goto语句。 最终要的一步,也就是将我们最后所得到的一个DataSet的内容,保存到本地,这里调用DataSet的WriteXml将数据写入到本地磁盘中,当然,如果你比较擅长xml读写的话,也可以使用xml读写方法将DataSet中的内容写入到本地磁盘。 到这里,就介绍完了如何利用CrystalReport提供的一组API获取报表服务器上的报表,关于如何将本地的报表再导入到报表服务器中是另外一个话题了。 |