Click here to Skip to main content
15,888,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to view the excel document in aspx page using iframe.how to use this.

What I have tried:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ContentType="application/vnd.ms-excel" %>
<iframe src="" width="400" height="300" ></iframe>

i have used above code but the excel file is directly downloaded not opened how to do it.
Posted
Updated 26-Nov-16 4:35am

C#
string excelFilePath = "ExcelFilePath";
System.IO.FileInfo file = new System.IO.FileInfo(excelFilePath);
 
  if (file.Exists)
  {
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename="  +      file.Name);
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.WriteFile(file.FullName);
    Response.End();
  }
 
Share this answer
 
Comments
Raja Ganapathy 25-Nov-16 1:06am    
write a code in button click?
Raja Ganapathy 25-Nov-16 1:13am    
i want view the excel file in pop up view like gmail file attach file is opened.
Have a look at https://github.com/ExcelDataReader/ExcelDataReader.

It will load the excel object into a DataReader which you can then use to to bind into your presentation layer.
C#
var _excelFile = new FileInfo("location_of_excel_file");
if (_excelFile != null && _excelFile.Exists)
{
   var fs = File.Open(_excelFile.FullName, FileMode.Open, FileAccess.Read);
   var ext = _excelFile.Extension.Replace('.', ' ').Trim();

   using (IExcelDataReader excelReader = ext == "xls"
      ? ExcelReaderFactory.CreateBinaryReader(fs)
      : ExcelReaderFactory.CreateOpenXmlReader(fs))
   {
      DataTable baseData = null;
      DataSet result = excelReader.AsDataSet();
      for (int i = 0; i < result.Tables.Count; i += 1)
      {
         // this is one method
         if (result.Tables[i].TableName.ToLower().Contains("sheet1"))
         {
            baseData = result.Tables[i];
         }
      }

      if (baseData != null)
      {
         int line = 0;
         foreach (DataRow row in baseData.Rows)
         {
            // do something
         }
      }
   }
}
 
Share this answer
 
v4
You were given a valid answer on your previous question post and you haven't made any change ever since and posting same question again, with no effort. Which (reposting) is not allowed.

How to view the excel file using ASP.NET C#?[^].
 
Share this answer
 
Comments
Raja Ganapathy 27-Nov-16 23:50pm    
Okay sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900