2

this is my code: Html:

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

In js part of my code I have this

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

in my controller I Have this code :

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

every thing is fine and my excel file has been created in the folder in my server. but I want to copy the file to client machine. how can I do that ?

6

2 Answers 2

2

This should do the job

JS should be like:

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

and the 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;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You return http code so instead of this you should return

        Response.Clear();
        Response.AddHeader("", "");
        Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
        Response.AddHeader("content-disposition", "attachment;  filename="MyFile.xlsx");
        Response.ContentType = "application/text";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        Response.BinaryWrite(xlPackage.GetAsByteArray());
        Response.End();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.