如何在aspx页面中打开excel doc? [英] How to open excel doc in aspx page?

查看:108
本文介绍了如何在aspx页面中打开excel doc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用iframe.how查看aspx页面中的excel文档来使用它。



我尝试过:



<%@ Page Title =Language =C#MasterPageFile =〜/ MasterPage.masterAutoEventWireup =trueCodeFile =Default.aspx.cs Inherits =_ DefaultContentType =application / vnd.ms-excel%>

< iframe src =width =400height =300>< / iframe>



i使用了上面的代码,但是直接下载的excel文件未打开怎么办。

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.

推荐答案

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();
  }


查看https://github.com/ExcelDataReader/ExcelDataReader。



它会将excel对象加载到DataReader中,然后您可以使用它绑定到表示层。

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.
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
         }
      }
   }
}


您在上一个问题帖子中得到了有效答案,此后您没有做任何更改并再次发布相同的问题,没有付出任何努力。哪个(重新发布)是不允许的。



如何使用ASP.NET C#查看excel文件? [ ^ ]。
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#?[^].


这篇关于如何在aspx页面中打开excel doc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆