生成Excel文件,然后在ASP.NET MVC应用程序中从浏览器下载它 [英] Generating an Excel File, then Downloading it From Browser in ASP.NET MVC Application

查看:149
本文介绍了生成Excel文件,然后在ASP.NET MVC应用程序中从浏览器下载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的应用程序中有这个页面,用户可以下载一个渲染视图的硬拷贝,这表示技能及其对项目中角色的要求,显示任何人对角色的完成所述技能



我有一个使用csv文件的功能,因为我可以使用 StringBuilder 创建逗号不必要的文件。



然而,在接近Excel方法之前,我想要一些轻型格式化,我意识到我无法以同样的方式发现。



我以前使用Interop生成Excel文件,但是如何创建可以在生成之后下载的文件?



以下是生成和返回CSV文件的工作代码:

  public ActionResult DownloadSQEPToCSV(int projectID)
{
//源我的数据

StringBuilder sBuilder = new StringBuilder();

sBuilder.Append(SQEPMatrix,For Project,+ data.First()。Project.ContractNumber);

foreach(数据中的var角色)
{
sBuilder.Append(\r\\\
Role :,+ role.First()。Title.Name)
sBuilder.Append(\\\\
Skill,Requirement);
foreach(var person in role.Distinct(uCom))
{
sBuilder.Append(,+ person.User.UserDetail.Name);
}
foreach(var skill in role.Distinct(uCom))
{
//更多的东西来生成我想要的
}
sBuilder。附加( \r\\\
);
}

//将文件附加到标题
Response.Clear();
Response.AddHeader(Content-Disposition,attachment; filename = SQEPMatrix for+ data.First()。Project.ContractNumber +.csv);
Response.ContentType =text / csv;
Response.Write(sBuilder);
Response.End();

返回SetTitleAndID(SQEP,dl_sqep_csv);
}

此代码由以下脚本调用:

 函数下载(id){
window.location.href ='../../Project/DownloadSQEPExcel?projectID='+ id;
}

所以我的问题是如何生成Excel电子表格,并以与我返回.csv文件相似的方式返回生成的文件?

解决方案

如果您使用像 DocumentFormat.OpenXml 一样,您可以在内存流中创建Excel文件,然后返回流使用FileStreamResult:

  var theStreamContainingSpreadsheet = CreateSpreadsheet(); 
theStreamContainingSpreadsheet.Position = 0;
返回新的FileStreamResult(theStreamContainingSpreadsheet,application / vnd.openxmlformats-officedocument.spreadsheetml.sheet)
{FileDownloadName =Export.xlsx};


So I have this page on my application where the user can download a "hard copy" of a rendered view, which represents Skills and their Requirements on Roles within a Project, showing the fulfilment of said Skills by any people on the Role.

I have the functionality working with a csv file, as I can just use a StringBuilder for creating a comma-delimited file.

However, before approaching the Excel approach, where I want some light formatting, I realise I cannot acheive this the same way.

I have used Interop for generating Excel files before, but how would I be able to create one that can be downloaded after it's generation?

Here is the working code to generate and return a CSV file:

public ActionResult DownloadSQEPToCSV(int projectID)
{
    //source my data

    StringBuilder sBuilder = new StringBuilder();

    sBuilder.Append("SQEPMatrix, For Project," + data.First().Project.ContractNumber);

    foreach (var role in data)
    {
        sBuilder.Append("\r\nRole:," + role.First().Title.Name);
        sBuilder.Append("\r\nSkill,Requirement");
        foreach (var person in role.Distinct(uCom))
        {
            sBuilder.Append("," + person.User.UserDetail.Name);
        }
        foreach (var skill in role.Distinct(uCom))
        {
            //More stuff to generate what I want
        }
        sBuilder.Append("\r\n");
    }

    //Attach file to the header 
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment;filename=SQEPMatrix for " + data.First().Project.ContractNumber + ".csv");
    Response.ContentType = "text/csv";
    Response.Write(sBuilder);
    Response.End();

    return SetTitleAndID("SQEP","dl_sqep_csv");
}

This code is invoked by the following script:

function download(id) {
    window.location.href = '../../Project/DownloadSQEPExcel?projectID=' + id;
}

So my question, is how can I generate an Excel spreadsheet, and return the generated file in a manner similar to how I return my .csv file?

解决方案

If you use something like DocumentFormat.OpenXml you can create the Excel file in a memory stream and then return the stream using a FileStreamResult:

var theStreamContainingSpreadsheet = CreateSpreadsheet();
theStreamContainingSpreadsheet.Position = 0;
return new FileStreamResult(theStreamContainingSpreadsheet, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                   {FileDownloadName = "Export.xlsx"};

这篇关于生成Excel文件,然后在ASP.NET MVC应用程序中从浏览器下载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆