2

I am trying to create a sandboxed webpart to export data from another webpart within the webpart page to Excel.

I tried using the Excel ActiveXObject in a client-side script, but it requires this META tag to be added:

<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="application/vnd.ms-excel">

How can I get this script to work? Alternatively is there another solution?

Note that it is enough if the solution works only in Internet Explorer, but I cannot change the security settings.

1 Answer 1

1

The metadata mentioned is merely to specify what kind of content the stream has. It is also used when trying to download a file. By example, more is in HTML <meta> http-equiv Attribute.

Use it in the HEAD section of your page, as in:

<head>
    <meta http-equiv="Content-Type" content="application/vnd.ms-excel">
    <meta http-equiv="Content-disposition": attachment; filename="file.xls">
</head>

In code, however, you use it as such:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
    "content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write("<head><meta http-equiv=Content-Type content=:" + '"' + "text/html; charset=utf-8" + '"' + "></head>");

// BUILD YOUR STRING HERE

HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();

I am not sure about an alternative just yet. I shall investigate in that direction.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.