0

I have already created a ashx file to open excel 2007 on website.

Here is the code in test.aspx: (call test.htm to open a popup)

    <a href="test.htm" target="_blank">Export</a>

Here is the code in test.htm: (using iframe to load the excel file)

<iframe src="handler1.ashx" style="height: 421px; width: 800px" ></iframe>

Here are the code in handle.ashx:

    void ProcessRequest(HttpContext context)
    {

        string filePath = "E:/test.xlsx";
        string filename = Path.GetFileName(filePath);
        context.Response.Clear();
        context.Response.AddHeader("Content-Disposition", "inline;filename=test.xlsx");
        context.Response.Charset = "";
        context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        context.Response.WriteFile(filePath);
        context.Response.Flush();
        context.Response.End();
    }

but the excel file is always opened by Microsoft Excel Application. Please help me.

Thank you so much.

Thang Lang
  • 75
  • 1
  • 11

1 Answers1

0

context.Response.WriteFile(filePath); // you are trying to write out into a file block.

before that you need to fetch all data into a memory. change to like this

context.Response.Write(YourExcelData);
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
  • hi Ravi, I tried to edit code as below, but it has error (cannot open ebcause file format or file ext is not valid) when opening file: FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte[] b = new byte[(int)fs.Length]; fs.Read(b, 0, (int)fs.Length); fs.Close(); context.Response.Write(b); – Thang Lang May 04 '12 at 04:36