Salem to all;
i was searching about printing and repoting in light switch and i did found this solution :
http://blogs.msdn.com/b/bethmassi/archive/2010/09/10/using-microsoft-word-to-create-reports-for-lightswitch-or-silverlight.aspx[
^]
but the problem is the solution is in vb and i did not convert it to c# , this is the code that i want to convert it:
Imports System.Runtime.InteropServices.Automation
Imports <xmlns:ns0="urn:microsoft:ordermanager:customer">
Namespace LightSwitchApplication
Module MyReports
Public Sub RunCustomerReportFixedTemplate(ByVal cust As Customer)
If AutomationFactory.IsAvailable Then
Try
'Create the XML data from our entity properties dynamically
Dim myXML = <customer>
<%= From prop In cust.Details.Properties.All
Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
%>
</customer>
Using word = AutomationFactory.CreateObject("Word.Application")
Dim resourceInfo = System.Windows.Application.GetResourceStream(New Uri("CustomerDetails.docx", UriKind.Relative))
Dim fileName = CopyStreamToTempFile(resourceInfo.Stream, ".docx")
Dim doc = word.Documents.Open(fileName)
'Grab the existing bound custom XML in the doc
Dim customXMLPart = doc.CustomXMLParts("urn:microsoft:ordermanager:customer")
Dim all = customXMLPart.SelectSingleNode("//*")
Dim replaceNode = customXMLPart.SelectSingleNode("/ns0:root[1]/customer[1]")
'replace the <customer> node in the existing custom XML with this new data
all.ReplaceChildSubtree(myXML.ToString, replaceNode)
word.Visible = True
End Using
Catch ex As Exception
Throw New InvalidOperationException("Failed to create customer report.", ex)
End Try
End If
End Sub
Public Sub RunCustomerReportDynamicTemplate(ByVal cust As Customer)
If AutomationFactory.IsAvailable Then
Try
Dim templateFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
"\Reports\CustomerDetails.docx"
'Create the XML data from our entity properties dynamically
Dim myXML = <ns0:root>
<customer>
<%= From prop In cust.Details.Properties.All
Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
%>
</customer>
</ns0:root>
Using word = AutomationFactory.CreateObject("Word.Application")
Dim tempFile = GetTempFileName(".docx")
File.Copy(templateFile, tempFile)
Dim doc = word.Documents.Open(tempFile)
'Add the new custom XML part to the document
Dim customXMLPart = doc.CustomXMLParts.Add(myXML.ToString())
'bind any content controls that we find based on the title of the control
For i = 1 To doc.ContentControls.Count
Dim ctrl = doc.ContentControls(i)
If Not ctrl.XMLMapping.IsMapped Then
ctrl.XMLMapping.SetMapping("/ns0:root[1]/customer[1]/" + ctrl.Title.ToString.ToLower(),
"xmlns:ns0=""urn:microsoft:ordermanager:customer""", customXMLPart)
End If
Next
word.Visible = True
End Using
Catch ex As Exception
Throw New InvalidOperationException("Failed to create customer report.", ex)
End Try
End If
End Sub
Private Function CopyStreamToTempFile(ByVal stream As System.IO.Stream, ByVal ext As String) As String
Dim path = GetTempFileName(ext)
'Create the temp file
Dim file = System.IO.File.Create(path)
file.Close()
'Write the stream to disk
Using fileStream = System.IO.File.Open(path,
System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.Write,
System.IO.FileShare.None)
Dim buffer(0 To stream.Length - 1) As Byte
stream.Read(buffer, 0, stream.Length)
fileStream.Write(buffer, 0, buffer.Length)
fileStream.Close()
End Using
Return path
End Function
Private Function GetTempFileName(ByVal ext As String) As String
'Return a uinuqe file name in My Documents\Reports based on a guid
Dim path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Reports"
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
Dim filename = Guid.NewGuid().ToString() & ext
path = System.IO.Path.Combine(path, filename)
Return path
End Function
End Module
End Namespace
all this class i did not convert it properly and no online converter did convert properly too,
specially this part :
Dim myXML = <customer>
<%= From prop In cust.Details.Properties.All
Select <<%= prop.Name.ToLower %>><%= If(prop.Value, "-") %></>
%>
</customer>
any help for this???