reportviewer有自带的导出功能,对单个报表用button实现导出l功能也很简单,调用报表的Render方法得到Byte() ,再
通过Filestream向文件写入Byte()即可以得到导出的文件。但是button对与多个reportviewer的报表怎样一起导出呢?
1.把每个报表生成byte(),把所有的byte()连接,再写入Excel。这个方法没有得到一个sheet多张报表的结果,导出的只有一张,实际上Byte()是加总的。这可能跟报表导出的原理有关。一张报表导出有一个类似结束符的标志,深入研究还没来得及做。希望大家能在这种方式上面提供一些帮助及解释。
2.换种方式,把多张报表先逐个导出Excel,再用MS的Excel组件新建一个Excel文件,利用sheet复制的方法把先前导出的Excel汇总。删除导出报表的Excel文件,这样就实现了一键导出得到多报表的Excel的功能。 这种法师看上去别扭,但终究可以实现,另外我7张报表,整个过程10s左右 ,结果还是比较满意。
上 CODE
Private Sub btExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExcel.Click, ButtonSepcial.Click
Dim bytes As New List(Of Byte())
Dim strFileNames(10) As String
Dim bt As DevComponents.DotNetBar.ButtonX = sender
Dim tb As DevComponents.DotNetBar.TabControlPanel = bt.Parent.Parent
Dim saveFileDialog1 As New SaveFileDialog()
Dim count As Integer = 0
'把所有報表轉化成bytes 存入List
For Each control As Control In tb.TabItem.AttachedControl.Controls
If TypeOf control Is ReportViewer Then
Dim reportstart As Integer = CType(control, Microsoft.Reporting.WinForms.ReportViewer).ServerReport.ReportPath.LastIndexOf("/")
bytes.Add(CType(control, Microsoft.Reporting.WinForms.ReportViewer).ServerReport.Render("Excel", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing))
strFileNames(count) = CType(control, Microsoft.Reporting.WinForms.ReportViewer).ServerReport.ReportPath.Substring(reportstart + 1)
count += 1
End If
Next
saveFileDialog1.Filter = "xls|*.xls|All|*.*"
saveFileDialog1.Title = "導出報表"
saveFileDialog1.FileName = "Reportviewer"
If saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim strFilePath As String = saveFileDialog1.FileName
Dim pathend As Int16 = strFilePath.LastIndexOf("\")
Dim pathstr As String = ""
Dim fs As FileStream = Nothing
Dim fixflag As String = "BeaconFile"
Dim appexcel As New Excel.Application
Dim booknew As Excel.Workbook = Nothing
Dim workbook As Excel.Workbook = Nothing
Dim worksheet As Excel.Worksheet = Nothing
Dim sheet As Excel.Worksheet = Nothing
Try
booknew = appexcel.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet)
For i As Integer = 0 To bytes.Count - 1
'報表bytes寫入單個的Excel文件中
pathstr = strFilePath.Substring(0, pathend + 1) & strFileNames(i) & fixflag & ".xls"
fs = New FileStream(pathstr, FileMode.Create)
fs.Write(bytes.Item(i), 0, bytes.Item(i).Length)
fs.Close()
'啟動Excel組件,打開寫入報表的Excel
workbook = appexcel.Workbooks.Open(pathstr, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing)
worksheet = CType(booknew.Sheets.Add(Type.Missing, Type.Missing, 1, Excel.XlSheetType.xlWorksheet), Excel.Worksheet)
worksheet.Name = strFileNames(i)
sheet = CType(workbook.Worksheets.Item(1), Excel.Worksheet)
sheet.Cells.Select()
sheet.Cells.Copy(Type.Missing)
worksheet.Paste(Type.Missing, Type.Missing)
appexcel.CutCopyMode = Excel.XlCutCopyMode.xlCopy
workbook.Close(Excel.XlSaveAction.xlDoNotSaveChanges, workbook.FullName, Type.Missing)
File.Delete(pathstr)
Next
booknew.SaveAs(strFilePath, Excel.XlFileFormat.xlTemplate, Type.Missing, Type.Missing, False, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
appexcel.Quit()
appexcel = Nothing
GC.Collect()
End Try
End If
End Sub
希望大家能给些实现这个功能其他的思路。O(∩_∩)O