How do I render an XLSX excel file using your excel control

Originally Posted 26 November 2013, 6:22 pm EST

  • Originally Posted 26 November 2013, 6:22 pm EST

    This isn't working...

    If I replace application/vnd.openxmlformats-officedocument.spreadsheetml.sheet with application/vnd.ms-excel it works, but it is a xls file not a xlsx file. Is there a way to render a XLSX file?


    public ActionResult ExcelFormat()
    {
    System.Drawing.Image i = System.Drawing.Image.FromFile(Path.Combine(Server.MapPath("~/Content/pdftemplate/logo.jpg")));
    C1.C1Excel.C1XLBook c1XLBook1 = new C1.C1Excel.C1XLBook();
    C1.C1Excel.XLSheet sheet = c1XLBook1.Sheets[0];
    sheet[0, 0].Value = i;

    RenderExcel(c1XLBook1);
    return View();
    }

    protected void RenderExcel(C1XLBook doc)
    {
    // Render PDF document into memory-based PDF stream.
    MemoryStream ms = new MemoryStream();
    doc.Save(ms);
    // Get response object.
    int length;
    HttpResponse rsp = System.Web.HttpContext.Current.Response;
    // Clear it
    rsp.Clear();
    // Write PDF stream into response buffer
    rsp.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    length = (int)ms.Length;
    rsp.OutputStream.Write(ms.GetBuffer(), 0, length);
    // Finish.

    rsp.Flush();
    }
  • Reply

    jhunt,

    Thank you for sharing the code. I was able to replicate this issue and this looks like a bug to me. I have escalated this issue to the developers for further review. We will get back to you as soon as possible.

    Regards,
    Sankalp
  • Reply

    I added some additional header values and it started working.

            protected void RenderExcel(C1XLBook doc, String strFileName)
    {

    // Render PDF document into memory-based PDF stream.
    MemoryStream ms = new MemoryStream();
    doc.Save(ms, FileFormat.OpenXml);

    // Get response object.
    int length;
    HttpResponse rsp = System.Web.HttpContext.Current.Response;
    // Clear it
    rsp.Clear();
    // Write PDF stream into response buffer
    //rsp.ContentType = "application/vnd.ms-excel";

    rsp.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
    rsp.AddHeader("content-disposition", "attachment;filename=\"" + strFileName + "\"");
    rsp.AddHeader("Content-Length", ms.Length.ToString());
    length = (int)ms.Length;
    rsp.OutputStream.Write(ms.GetBuffer(), 0, length);
    // Finish.

    rsp.Flush();
    }
  • Reply

    jhunt,

    Thank you for sharing your solution. I am sure it will help other community members who might face this issue.

    Regards,
    Sankalp
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels