c# - 使用Web API和javascript在asp.net中将excel文件从服务器下载到本地计算机

标签 c# asp.net-web-api dotnetnuke epplus

这是我的代码: 网页:

 <a class="castrol_button " data-bind="click: createExcelFile">download excel file</a>

在我的代码的 js 部分中,我有这个

   createExcelFile = function (data, event) {
   //call an API
 }

在我的 Controller 中我有以下代码:

   [HttpGet]
    public HttpResponseMessage CreatePaymentExcelFile(long Customerid)
      {
        try
        {
        // get data from DB to list which name is lst
        // using epplus dll for creating Excel file
     var file =new FileInfo(HttpContext.Current.Server.MapPath("~/DesktopModules/Castrolo2c/Resource/PaymentList.xlsx");

        using (ExcelPackage xlPackage = new ExcelPackage(file))
        {
            ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("PaymentList");
            if (worksheet != null)
            {
                int row = 2;
                foreach( var i in res )
                {

                    worksheet.Cells[row, 1].Value = i.typename;
                    worksheet.Cells[row, 2].Value = i.pNO;
                    worksheet.Cells[row, 3].Value = i.Date;
                    worksheet.Cells[row, 4].Value = i.cashdate;
                    worksheet.Cells[row, 5].Value = i.Money;
                    worksheet.Cells[row, 6].Value = i.bedehkari;
                    worksheet.Cells[row, 7].Value = i.bestankari;
                    row++;
                }
                worksheet.Column(1).Width = 16;
                xlPackage.Workbook.Properties.Title = "patments";

                xlPackage.Workbook.Properties.Company = "Castrol";

                xlPackage.Save();

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (Exception e)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError, "ERR"));
        } } 

一切都很好,我的 Excel 文件已在我的服务器的文件夹中创建。但我想将文件复制到客户端计算机。我该怎么做?

最佳答案

这应该可以完成工作

JS 应该是这样的:

createExcelFile = function (data, event) {
    e.preventDefault(); 
    window.location.href = '...'; //The Api Address
}

和 API:

    [HttpGet]
    public HttpResponseMessage GetExcel()
    {
        using (var p = new OfficeOpenXml.ExcelPackage())
        {
            var ws = p.Workbook.Worksheets.Add("My WorkSheet");
            ws.Cells["A1"].Value = "A1";

            var stream = new System.IO.MemoryStream(p.GetAsByteArray());

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(stream)
            };
            result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
            {
                FileName = "myworkbook.xlsx"
            };
            result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentLength = stream.Length;
            return result;
        }
    }

关于c# - 使用Web API和javascript在asp.net中将excel文件从服务器下载到本地计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53359820/

相关文章:

c# - 让我的游戏重新开始 C#

c# - 如何使用 FromQuery Attribute 得到一个复杂的对象?

dotnetnuke - 基本 DotNetNuke 定位方法的一些问题

c# - 从浏览器运行 WCF 方法

c# - 当 T 是接口(interface)时,为什么我的从 T 到 SomeStruct<T> 的转换运算符不工作?

用于 Web 应用程序的 C#

c# - 如何最好地在 Web api 和客户端之间显示格式化属性

binding - POST 中的 Web API 模型绑定(bind)问题

azure - 在 Azure 网站上的 DotNetNuke 中部署扩展时出现问题

dotnetnuke - 如何在左侧菜单中显示主菜单的子项