I have a cshtml view in mvc filled with xml such as :
@model myproject.net.Models.mymodel
@{
Layout = null;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; " + "filename=" +
Model.myusername.ToString() + "(" +
Model.mydate.Date.ToShortDateString() + ").xls");
}
<?xml version="1.0" encoding="utf-8"?>
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Styles>
<Style ss:ID="s25">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
<Interior ss:Color="#FFA500" ss:Pattern="Solid"/>
</Style>
</Styles>
<ss:Worksheet ss:Name="Sheet1">
<ss:Table>
@if (Model.someNumerableContent.Count > 0)
{
<!-- MyContent -->
<ss:Row>
<ss:Cell><ss:Data ss:Type="String">Content Header</ss:Data></ss:Cell>
</ss:Row>
<ss:Row>
<ss:Cell><ss:Data ss:Type="String">SubHeader 1</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type="String">SubHeader 2</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type="String">SubHeader 3</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type="String">SubHeader 4</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type="String">SubHeader 5</ss:Data></ss:Cell>
</ss:Row>
foreach (var subContent in Model.someNumerableContent)
{
<ss:Row>
<ss:Cell><ss:Data ss:Type="String">@subContent.mydate.Date.ToShortDateString()</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type="String">@subContent.number</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type="String">@subContent.name</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type="String">@subContent.surname</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type="String">@subContent.issue</ss:Data></ss:Cell>
</ss:Row>
}
<ss:Row> </ss:Row>
}
</ss:Table>
</ss:Worksheet>
</ss:Workbook>
and i wanna convert it to .xls file and attach to a mail without saving it anywhere. but couldnt figure out how can i achive this. I cant use office.interop for some restrictions i got in the server so its not an option to me. I only want to create an .xls file by Xml and send it using .net mail. so far i've done this:
// Memory stream for the xml file
using (MemoryStream memoryStream = new MemoryStream())
{
// Can i use something like this?
byte[] contentAsBytes = Encoding.Default.GetBytes( View("ExportToExcel").ToString() );
memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);
// Set the position to the beginning of the stream.
memoryStream.Seek(0, SeekOrigin.Begin);
// Create attachment
ContentType contentType = new ContentType();
contentType.MediaType = MediaTypeNames.Text.Xml;
contentType.Name = UserName + "(" + FileDate + ").xls";
// Attach
mail.Attachment = new Attachment(memoryStream, contentType);
}
so how can i achive this?
I couldnt implement razor view successfull but after working 30 hours on it. i found another method..
First I created an XmlString as below:
Then i saved it to
MemoryStream
withUTF8
Encoding..Then defined
ContentType
asExcel
At last i attached it to .net mail
and it worked just fine. I may not be able to turn an xml view to mail attachment but easily i could turn an xml string to mail attachment and i hope this helps to the others that goes trough this problem.