简体   繁体   中英

Open excel file in asp.net

I am trying to open a Excel(.xls) file from asp.net.The code i am using is given here.It is showing a save as dialog box which i don't want .Please help me !!!

  Response.ContentType = "application/xls";
  Response.Flush();
  Response.WriteFile(fileName);
  Response.End();

Try adding response.addheader and use "binarywirte" instead of "writeFile" to your code like below sample code(working code) ...

   System.IO.FileStream fs = null;
        fs = System.IO.File.Open(filename, System.IO.FileMode.Open);

        byte[] btFile = new byte[fs.Length];
        fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
        fs.Close();
        Response.AddHeader("Content-disposition", "attachment; filename=" + filename);
        Response.ContentType = "application/octet-stream";
        Response.BinaryWrite(btFile);
        Response.End();

This is ultimately controlled through file associations in the operating system.You can modify registry settings on a per machine basis to achieve this result.

You would need to change the subkeys under HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes that relate to excel. Specifically you have to add the following value:

Name=BrowserFlags; Type=REG_DWORD; Value=8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM