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

  • SEARCH
  • Home
  • 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 6977775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:40:08+00:00 2026-05-27T17:40:08+00:00

What I have already : I have controller code churning up some values /

  • 0

What I have already : I have controller code churning up some values / calculations and sending them to the View. Right now, I am displaying all results on the view page.

What I am trying to do : Export those results that are displayed on the view to a text or excel file on press of a button “Export”

Using : MVC

Sample code I have

Controller:

    public ActionResult Calculations()
    {       
            dynamic CalcModel = new ExpandoObject();
            int var1 = //Value calculated here
            int var2 = //Calculation

            CalcModel.Var1= var1;
            CalcModel.Var2= var2;

            return View(CalcModel);

    }

View:

<table>
   <tr><td>First Value:</td><td><%=Model.Var1%></td></tr>
   <tr><td>Second Value:</td><td><%=Model.Var2%></td></tr>
</table>

I want to be able to write these values from the controller to a text file or excel file and let the user save the file. Thanks for the help.

EDIT:

I found a solution (kind of) but need further help:

Latest Controller code:

    public ActionResult Calculations()
    {       
            dynamic CalcModel = new ExpandoObject();
            int var1 = //Value calculated here
            int var2 = //Calculation

            CalcModel.Var1= var1;
            CalcModel.Var2= var2;   

            //Export code.

            string csv = "Value1 = " + var1 + "|| Value2 = " + var2; 
            return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", 
            "Report.txt"); 

            //Export code end.

            return View(CalcModel);                  

    }

This generates a text file with the values printed in it. But How do I get each value printed in a separate line.. right now all values are printed in one flat line.

  • 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-05-27T17:40:08+00:00Added an answer on May 27, 2026 at 5:40 pm

    Envrionment.NewLine should do it:

    var builder = new StringBuilder();
    //this is probably a loop...
    builder.AppendFormat("1,2{0}", Envrionment.NewLine);
    builder.AppendFormat("3,4{0}", Envrionment.NewLine);
    
    File(new System.Text.UTF8Encoding().GetBytes(builder.ToString()), "text/csv", 
            "Report.txt"); 
    

    Edit:
    You can try \n as well.

    string csvText = string.Format("First Value,{0}\nSecond Value,{1}",var1, var2);
    File(new System.Text.UTF8Encoding().GetBytes(csvText), "text/csv", 
            "Report.txt"); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already search for some time over the net on how to create
I have already implemented some AJAX pagination in my Rails app by using the
I have already have this code to force these URLs to HTTPS: RewriteCond %{HTTPS}
I have the following code in my controller: public ActionResult Details(int id) { var
Greetings, Problem: I have a main view, that already is associated with a xib
I have generated a group of scaffolds with edited code inside, but now I
I have the following code in my viewWillAppear: - (void) viewWillAppear:(BOOL)animated{ self.view.backgroundColor = [UIColor
I have already posted something similar here but I would like to ask the
I have already tried PreRenderComplete and unload is too late
I have already googled for this I have a Table with following structure in

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.