如图所示,怎样直接在asp.net页面中打开Excel

解决方案 »

  1.   

    三种办法:
    1)IFRAME src指向EXCEL文件,但要考虑客户端OFFICE版本问题
    2)IFRAME src指向EXCEL生成的mht文件
    3)考虑使用第三方控件,如FarPoint等
      

  2.   

    protected void btnUpload_Click(object sender, EventArgs e)  
    {  
    if ((txtFilePath.HasFile))  
    {  
       
    OleDbConnection conn = new OleDbConnection();  
    OleDbCommand cmd = new OleDbCommand();  
    OleDbDataAdapter da = new OleDbDataAdapter();  
    DataSet ds = new DataSet();  
    string query = null;  
    string connString = "";  
    string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");  
    string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();  
      
    //Check file type  
    if (strFileType == ".xls" || strFileType == ".xlsx")  
    {  
    txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));  
    }  
    else 
    {  
    lblMessage.Text = "Only excel files allowed";  
    lblMessage.ForeColor = System.Drawing.Color.Red;  
    lblMessage.Visible = true;  
    return;  
    }  
      
    string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);  
       
    //Connection String to Excel Workbook  
    if (strFileType.Trim() == ".xls")  
    {  
    connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";  
    }  
    else if (strFileType.Trim() == ".xlsx")  
    {  
    connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";  
    }  
    query = "SELECT * FROM [Sheet1$]";  
    //query = "SELECT [Country],[Capital] FROM [Sheet1$] WHERE [Currency]=’Rupee’"  
    //query = "SELECT [Country],[Capital] FROM [Sheet1$]"  
       
    //Create the connection object  
    conn = new OleDbConnection(connString);  
    //Open connection  
    if (conn.State == ConnectionState.Closed) conn.Open();  
    //Create the command object  
    cmd = new OleDbCommand(query, conn);  
    da = new OleDbDataAdapter(cmd);  
    ds = new DataSet();  
    da.Fill(ds);  
      
    grvExcelData.DataSource = ds.Tables[0];  
    grvExcelData.DataBind();  
    lblMessage.Text = "Data retrieved successfully! Total Records:" + ds.Tables[0].Rows.Count;  
    lblMessage.ForeColor = System.Drawing.Color.Green;  
    lblMessage.Visible = true;  
       
    da.Dispose();  
    conn.Close();  
    conn.Dispose();  
    }  
    else 
    {  
    lblMessage.Text = "Please select an excel file first";  
    lblMessage.ForeColor = System.Drawing.Color.Red;  
    lblMessage.Visible = true;  
    }  
    }   
      

  3.   

    http://wyf.iteye.com/blog/531166
      

  4.   

    补充问题:我的需求是在页面直接打开Excel后,还要读取C#传过来的数据显示,对Excel进行操作后还要进行保存,最好在填写Excel不合规范的时候还可以像javascript那样弹出对话框
      

  5.   

    2楼的第一种方法和第二种方法可以打开,但不能满足我补充的需求,第三种方法FarPoint是收费的吧,而且FarPoint没有Excel自带的宏,项目中需要这个东西,用来判断用户的输入是否符合标准;
    3楼的方法不行,这就是把数据直接读取到页面,我需要的是直接在页面显示Excel;
    4楼的方法是输出excel
    谢谢大家的回复,期待大家继续给出建议...
      

  6.   

    实际上就是在线编辑Excel,而且要和C#交互
      

  7.   

    研究了很久,直接在页面打开Excel估计不行了,我转换了一种方法,把excel解析出来显示在页面,然后对页面 的值进行操作的同时更新到内存中的Excel中,这样它也可以同时触发Excel中的宏,感兴趣的可以试试,我也正在研究中