Можно ли встроить файл.RDL в.EXE для развертывания?
У меня есть несколько отчетов.RDL, которые я запускаю в (VB) приложении Windows Forms.
Я хотел бы распространять его в виде одного файла.EXE.
Можно ли встраивать файлы.RDL в.EXE?
Есть это вкусное свойство с именем LocalReport.ReportEmbeddedResource, но оно не встраивает.RDL в конечный файл.
2 ответа
Да. Метод LocalReport.LoadReportDefinition(TextReader) может принимать поток. Вы можете использовать StringReader для загрузки отчета из ваших ресурсов или из константы (строки), встроенной в ваш код.
http://msdn.microsoft.com/en-us/library/system.io.stringreader%28v=vs.110%29.aspx
Пример:
Const rdl As String = "<Report>" & _
" <DataSets>" & _
" <DataSet Name=""IrrelevantToThisExample"">" & _
" <Query>" & _
" <DataSourceName>DataTableName</DataSourceName>" & _
" <CommandText>SELECT * FROM sys.Tables</CommandText>" & _
" </Query>" & _
" </DataSet>" & _
" </DataSets>" & _
"</Report>"
'the LocalReport.LoadReportDefinition needs to read the string from a stream
'Create a string reader to convert the string into a stream
Using sr As New System.IO.StringReader(rdl)
MyReportViewer.LocalReport.LoadReportDefinition(sr)
sr.Close()
End Using
Это было окончательное решение, основанное на ответе Уилла Бертона по адресу http://social.msdn.microsoft.com/Forums/en-US/f7f92d61-2c23-47e7-b2a3-12ee4ed9fa9a/loading-an-embedded-resource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' This is a simplest-possible self-contained example that works!!
Dim data() As Byte = My.Resources.aSimpleReport
Dim reportStream As New IO.MemoryStream
reportStream.Write(data, 0, data.Length)
reportStream.Position = 0
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.LoadReportDefinition(reportStream)
ReportViewer1.RefreshReport()
End Sub
Просто для пояснения настройки: очень простой (просто текстовое поле) файл.RDL с именем aSimpleReport.RDL был добавлен как "Существующий файл" в Ресурсы проекта.