Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:34:09+00:00 2026-06-15T19:34:09+00:00

I have a cshtml view in mvc filled with xml such as : @model

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:34:10+00:00Added an answer on June 15, 2026 at 7:34 pm

    I couldnt implement razor view successfull but after working 30 hours on it. i found another method..

    First I created an XmlString as below:

    public String XmlToImplement(int id)
    {
        // Add values from db to model
        MyModel model = new MyModel();
        model.contents = db.contents.Where(c => c.MyData.id == id).ToList();
        // XmlString
        String XmlString = @"<?xml version='1.0' encoding='utf-8'?>
        <ss:Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
                    xmlns:o='urn:schemas-microsoft-com:office:office'
                    xmlns:x='urn:schemas-microsoft-com:office:excel'
                    xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'
                    xmlns:html='http://www.w3.org/TR/REC-html40'>
            <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.contents.Count > 0)
                {
                    XmlString = XmlString + @"<!-- MyModelData -->
                    <ss:Row>
                        <ss:Cell><ss:Data ss:Type='String'> MyModelData </ss:Data></ss:Cell>
                    </ss:Row>
                    <ss:Row>
                        <ss:Cell><ss:Data ss:Type='String'>Date</ss:Data></ss:Cell>
                        <ss:Cell><ss:Data ss:Type='String'>Number</ss:Data></ss:Cell>
                        <ss:Cell><ss:Data ss:Type='String'>Name/Surname</ss:Data></ss:Cell>
                    </ss:Row>"; 
                    foreach (var content in model.contents)
                    {
                        XmlString = XmlString + @"<ss:Row>";
                        XmlString = XmlString + @"<ss:Cell><ss:Data ss:Type='String'>" + content.thedate.Date.ToShortDateString() + "</ss:Data></ss:Cell>";
                        XmlString = XmlString + @"<ss:Cell><ss:Data ss:Type='String'>" + content.number + "</ss:Data></ss:Cell>";
                        XmlString = XmlString + @"<ss:Cell><ss:Data ss:Type='String'>" + content.namesurname+ "</ss:Data></ss:Cell>";
                        XmlString = XmlString + @"</ss:Row>";
                    }
                    // An empty row for next value set
                    XmlString = XmlString + "<ss:Row> </ss:Row>"; 
                }
                XmlString = XmlString + @"</ss:Table>
            </ss:Worksheet>
        </ss:Workbook>";
        return XmlString;
    }
    

    Then i saved it to MemoryStream with UTF8 Encoding..

    // Create attachment
    // Add XML to MemoryStream                                          /* MyXmlString */
    MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(XmlToImplement(id)));
    

    Then defined ContentType as Excel

    // Content Type
    ContentType contentType = new ContentType("application/vnd.ms-excel");
    contentType.Name = UserName + "(" + FileDate + ").xls";
    

    At last i attached it to .net mail

    // Attach
    mail.Attachment = new Attachment(memoryStream, contentType);
    mail.Attachment.NameEncoding = UTF8Encoding.UTF8;
    mail.Attachment.TransferEncoding = TransferEncoding.Base64;
    mail.Attachment.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following view: @model MyModel1 @{ ViewBag.Title = Index; Layout = ~/Views/Shared/_Layout.cshtml;
I have an Index.cshtml view: @model AttendenceModel @{ Layout = ~/Views/Shared/_Layout.cshtml; } @using (Html.BeginForm(VisOppsummering,
I'm having an odd problem with asp.net MVC razor view. I want my model
I am using ASP.NET MVC 3 with the Razor view engine . I have
We had a view (.cshtml) which rendered XML for an RSS feed using ASP.NET
Suppose I have a parent.cshtml view with a parentModel, and a child.cshtml view with
I have an code segment of view .cshtml page like : @Html.ActionLink(Add Charts of
Say I have this line of code in the view. <?php echo CHtml::activeTextField($model,'start_time'); ?>
I have a System.Web.Mvc.RazorView object which is strongly typed when in cshtml. Can I
In an ASP.MVC application, I have a function defined in my _ViewStart.cshtml like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.