Requirement - Excel sheet should be populated with data from the database
Issue - Excel sheet dialog box is opening with Open, save, Cancel.
But, there is again a dialog box which says that the file u are trying to open,'Rep.aspx', is in a different format than specified by the file extension. verify that the file is not corruptedand is from a trusted source before opening the file. Do you want to open the file now?
When i say 'Yes', the data isnt populated but a blank excel sheet with column headers is seen.
Not able to spot where the problem is :(
public partial class Rep : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlTable tblExel;
SqlConnection conn = new SqlConnection("ConnectionString");
private void Page_Load(object sender, System.EventArgs e)
{
try
{
conn.Open();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
SqlCommand cmd = new SqlCommand("Select CubicleId,EmployeeNo,EmployeeName,Project,EmailId, Department,DirectNo from PuneEmployee", conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string CubicleId = dr.GetValue(0).ToString();
int EmployeeNo = Int32.Parse(dr.GetValue(1).ToString());
string EmployeeName = dr.GetValue(2).ToString();
string Project = dr.GetValue(3).ToString();
string EmailId = dr.GetValue(4).ToString();
string Department = dr.GetValue(5).ToString();
string DirectNo = dr.GetValue(6).ToString();
AddTableRow(CubicleId, EmployeeNo, EmployeeName, Project, EmailId, Department, DirectNo);
}
dr.Close();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
private void AddTableRow(string CubicleId, int EmployeeNo, string EmployeeName, string Project, string EmailId, string Department, string DirectNo)
{
HtmlTableRow row1 = new HtmlTableRow();
row1.Height = "20";
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();
HtmlTableCell cell3 = new HtmlTableCell();
HtmlTableCell cell4 = new HtmlTableCell();
HtmlTableCell cell5 = new HtmlTableCell();
HtmlTableCell cell6 = new HtmlTableCell();
HtmlTableCell cell7 = new HtmlTableCell();
cell1.Controls.Add(new LiteralControl(CubicleId));
cell2.Controls.Add(new LiteralControl(EmployeeNo.ToString()));
cell3.Controls.Add(new LiteralControl(EmployeeName));
cell4.Controls.Add(new LiteralControl(Project));
cell5.Controls.Add(new LiteralControl(EmailId));
cell6.Controls.Add(new LiteralControl(Department));
cell7.Controls.Add(new LiteralControl(DirectNo));
row1.Cells.Add(cell1);
row1.Cells.Add(cell2);
row1.Cells.Add(cell3);
row1.Cells.Add(cell4);
row1.Cells.Add(cell5);
row1.Cells.Add(cell6);
row1.Cells.Add(cell7);
cell1.Width = "100";
cell2.Width = "250";
cell3.Width = "100";
cell4.Width = "100";
cell5.Width = "100";
cell6.Width = "100";
cell7.Width = "100";
tblExel.Rows.Add(row1);
}