Reputation: 21
I am working on an application where I have to get email IDs from an Excel sheet and send out an email to each ID. I have tried to display the email IDs in a grid as a test. My application is not throwing any errors but it is not executing in the required manner.
Can anyone help figure out why this is happening?
Upvotes: 1
Views: 2000
Reputation: 88
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Excel.Application appExl;
Excel.Workbook workbook;
Excel.Worksheet NwSheet;
Excel.Range ShtRange;
appExl = new Excel.Application();
/ /Opening Excel file
workbook = appExl.Workbooks.Open(Server.MapPath("firstexcel.xlsx"));
NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
int Cnum = 0;
int Rnum = 0;
ShtRange = NwSheet.UsedRange;
//Reading Excel file.
//Creating datatable to read the containt of the Sheet in File.
DataTable dt = new DataTable();
dt.Columns.Add("EMP NO");
dt.Columns.Add("NAME");
dt.Columns.Add("AGE");
dt.Columns.Add("PHN NO");
dt.Columns.Add("EMAIL");
for (Rnum = 2; Rnum <= ShtRange.Rows.Count; Rnum++)
{
DataRow dr = dt.NewRow();
//Reading Each Column value From sheet to datatable
for (Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
{
dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
}
// adding Row into DataTable
dt.Rows.Add(dr);
dt.AcceptChanges();
}
workbook.Close(true);
appExl.Quit();
//DataSource to GrigView
gvOne.DataSource = dt;
gvOne.DataBind();
}
}
Upvotes: 0
Reputation: 2735
The following link shows code how to extract data from required cells in an Excel sheet other than binding the whole excel sheet to a grid:
http://social.msdn.microsoft.com/Forums/en/vsto/thread/b6e8a28c-6760-4e86-a1aa-e2ce9ec36380
Also you can populate a DataTable using the selected values and then populate the DataGrid using that DataTable.
Upvotes: 1
Reputation: 21178
Take a look at this code which binds Excel to a GridView:
http://www.shiningstar.net/ASPNet_Articles/DataSet/DataSetProject5.aspx
It may provide you with a neutral test harness. Without your code and the spreadsheet it's hard to say what the problem is.
Upvotes: 1