C#Asp.NET MVC使用FileStreamResult下载Excel文件 [英] c# Asp.NET MVC downloading excel file using FileStreamResult

查看:234
本文介绍了C#Asp.NET MVC使用FileStreamResult下载Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建一个方法,该方法将接收模型,从模型中构建excel(构建和接收零件时不会出现问题),然后使用内存流(不将其保存在服务器上)导出(让用户下载它).我是ASP.NET和MVC的新手,所以我找到了指南并将其构建为教程项目:

I need to build a method, that will receive model, build excel from it (building and receiving part is done without problems) and then export (let user download it) using memory stream (without saving it on the server). I am new to ASP.NET and MVC so i found guide and built this as a tutorial project:

    public FileResult Download()
    {
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

        object misValue = System.Reflection.Missing.Value;

        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        xlWorkSheet.Cells[1, 1] = "ID";
        xlWorkSheet.Cells[1, 2] = "Name";
        xlWorkSheet.Cells[2, 1] = "1";
        xlWorkSheet.Cells[2, 2] = "One";
        xlWorkSheet.Cells[3, 1] = "2";
        xlWorkSheet.Cells[3, 2] = "Two";


        var path = "C:\\Work-Work\\TestFolder\\XCLbuildTry1\\csharp-Excel.xls";
        xlWorkBook.SaveAs(path);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        Marshal.ReleaseComObject(xlWorkSheet);
        Marshal.ReleaseComObject(xlWorkBook);
        Marshal.ReleaseComObject(xlApp);
        return File(path, "application/vnd.ms-excel", "WidgetData.xlsx");
    }

现在,我需要更改此代码,以使此方法发送excel文件而不将excel文件保存在服务器上.我曾尝试在此处以堆栈方式搜索一些指南和答案,但现在我找不到可以实现的解决方案.

Now i need to change this code to make this method sent my excel file without saving my excel file on server. I have tried to google some guides and answers here, on stack, but for now i cant find a solution, which i can implement.

我认为我应该使用 FileStreamResult ,但是所有指南都没有提供有关创建和将我的文件插入流中的特定信息.

I think i should use FileStreamResult, but all guides give no particular information about creating and inserting my file into stream.

推荐答案

不幸的是,它似乎没有显示 Microsoft.Office.Interop.Excel.Workbook 界面(

Unfortunately it does not appear that the Microsoft.Office.Interop.Excel.Workbook interface (msdn) will allow you to convert your workbook to a memory stream.

但是,过去我使用了外部库 EPPlus ,该库运行非常出色.

However, in the past I have used an external library, EPPlus, which has worked fantastically.

有关使用EPPlus的代码,请参见下文,该代码不需要您将文件保存在服务器上.

See below for code using EPPlus which will not require you to save the file on the server.

BusinessLogic

public MemoryStream Download() {
    MemoryStream memStream;

    using (var package = new ExcelPackage()) {
        var worksheet = package.Workbook.Worksheets.Add("New Sheet");

        worksheet.Cells[1, 1].Value = "ID";
        worksheet.Cells[1, 2].Value = "Name";
        worksheet.Cells[2, 1].Value = "1";
        worksheet.Cells[2, 2].Value = "One";
        worksheet.Cells[3, 1].Value = "2";
        worksheet.Cells[3, 2].Value = "Two";  

        memStream = new MemoryStream(package.GetAsByteArray());
    }

    return memStream;
}    

控制器

public FileStreamResult Download() {
    var memStream = BusinessLogic.Download();
    result File(memStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
}

这篇关于C#Asp.NET MVC使用FileStreamResult下载Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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