locked
How to generate Excel file using Post Method...? RRS feed

  • Question

  • User-1423656093 posted

    Hi all,

    Can anyone help to create and download Excel file Using WebApi Post mothod.

    Thanks in advance..!!!

    Monday, August 10, 2015 1:59 AM

All replies

  • User-138061656 posted

    hi Krish Kannaa ,

    use Response , follow the below code.

    Response.ClearContent();
    Response.Buffer = true;
    
    string fileName = "Excel.xls";
    string html = "<div>your excel content as html</div>";
    
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
    Response.ContentType = "application/ms-excel";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
    Response.Charset = "utf-8";
    Response.Write(html);

    Monday, August 10, 2015 4:16 AM
  • User-1423656093 posted

    Hi onurmenal,

    Thank for your response.

    I need one more clarification on this.... What is the return type for this post method....? like httpresponse,httpresponsemessage or anyother...?

     

     

    Monday, August 10, 2015 8:40 AM
  • User438962230 posted

    Hi Krish,

    Please firstly take a look at this KB article:

    Considerations for server-side Automation of Office

    Microsoft recommends that we use Open XML SDK on the server side to auto mate the Office files, including generating Excel workbooks. You have to install Open XML SDK before calling the API:

    http://www.microsoft.com/en-us/download/details.aspx?id=30425

    And this is the documentation about how to generating an Excel workbook:

    https://msdn.microsoft.com/en-us/library/office/ff478153.aspx

    See another document for the overview of Open XML SDK if you want to know the details:

    https://msdn.microsoft.com/en-us/library/office/bb448854.aspx

    Based on my own experience, it's a little bit hard to understand and use Open XML SDK, there's an opensource library ClosedXML which make it easier to automate the Office files:

    https://closedxml.codeplex.com/

    Some other libraries, e.g. NPOI, are also good choices, we don't need to have Office application installed on the server either.

    Monday, August 31, 2015 1:29 AM