How to view Excel file directly from asp.net 4.0?

I just want to view Excel file directly from asp.net 4.0 with C#.

Here is my code :

protected void lbut_viewfile_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Excel.Workbook theWorkbook = 
         ExcelObj.Workbooks.Open(
             openFileDialog1.FileName, 0, true, 5, "", "", true,     
             Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", 
             false, false, 0, true);

    Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;

    Microsoft.Office.Interop.Excel.Worksheet worksheet = 
         (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);

    for (int i = 1; i <= 10; i++)
    {
        Microsoft.Office.Interop.Excel.Range range = 
             worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
        System.Array myvalues = (System.Array)range.Cells.Value;
        string[] strArray = ConvertToStringArray(myvalues);
    }

}

Here I can not get this ExcelObj.Workbooks.Open, openFileDialog1 and ConvertToStringArray I want which specific refrences it diserve.

Answers


You can't combine ASP.NET and Office together. This is strongly discouraged by Microsoft. Also you are combining Windows controls, which will run on the server and will never work on the client.

If you want to show the Excel file in the browser, I suggest to either let the user download the file and show it in it's default program, or read the file using EpPlus and create the output HTML yourself.


Need Your Help

How do I get the host domain name in ASP .NET without using HttpContext.Current.Request?

asp.net url dns

I've got an ASP .Net application running on IIS7. I'm using the current url that the site is running under to set some static properties on a class in my application. To do this, I'm getting the do...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.