从 MVC 控制器导出到文本或 Excel

发布于 2024-12-22 19:42:13 字数 1521 浏览 2 评论 0 原文

我已经拥有的:我有控制器代码搅动一些值/计算并将它们发送到视图。现在,我正在视图页面上显示所有结果。

我想做的:按下“导出”按钮将视图上显示的结果导出到文本或 Excel 文件

使用:MVC

示例代码我有

控制器:

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

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

            return View(CalcModel);

    }

视图:

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

我希望能够写入这些值从控制器到文本文件或 Excel 文件,并让用户保存文件。感谢您的帮助。

编辑:

我找到了一个解决方案(某种程度),但需要进一步的帮助:

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);                  

    }

这会生成一个文本文件,其中打印有值。但是如何将每个值打印在单独的行中..现在所有值都打印在一条平线上。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为人所爱 2024-12-29 19:42:13

Envrionment.NewLine 应该这样做:

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"); 

编辑:
你也可以尝试\n。

string csvText = string.Format("First Value,{0}\nSecond Value,{1}",var1, var2);
File(new System.Text.UTF8Encoding().GetBytes(csvText), "text/csv", 
        "Report.txt"); 

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"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文