Exception Details: System.Runtime.InteropServ
ices.COMEx
ception: Exception from HRESULT: 0x800A03EC.
Source Error:
Line 32: xlApp.Visible = true;
Line 33: string strFileName = sFileName;
Line 34: Excel.Workbook xlWB = xlApp.Workbooks.Open("Exce
lData.xls"
,
Line 35: null,
Line 36: null,
Source File: c:\inetpub\wwwroot\webappl
ication1\w
ebform2.as
px.cs Line: 34
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
;
using System.Diagnostics;
using System.Reflection;
using Excel= Microsoft.Office.Interop.E
xcel;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Runtime.InteropServ
ices; // For COMException
namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected Excel.Application xlApp;
protected Excel.Workbook xlWB1;
private void searchExcelSpreadSheet(str
ing sFileName)
{
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
string strFileName = sFileName;
Excel.Workbook xlWB = xlApp.Workbooks.Open("Exce
lData.xls"
,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null);
Excel.Worksheet xls = (Excel.Worksheet)xlWB.Shee
ts[1];
int intRows, intCols, r, c;
intRows = xls.UsedRange.Rows.Count;
intCols = xls.UsedRange.Columns.Coun
t;
for(r = 1; r < intRows; r++)
{
for(c=1; c < intCols; c++)
{
string text = (string)((Excel.Range)xls.
Cells[r,c]
).Text;
Response.Write (text+"<br>");
}
}
xlWB.Close(false, null, null);
xlApp.Quit();
xls = null;
xlWB = null;
xlApp = null;
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
searchExcelSpreadSheet ("\\Excel\\ExcelData.xls")
;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P
age_Load);
}
#endregion
}
}
Start Free Trial