Data not getting populated in excel - See code below

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shikha1234
    New Member
    • Mar 2008
    • 10

    Data not getting populated in excel - See code below

    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.P age
    {
    protected System.Web.UI.H tmlControls.Htm lTable tblExel;

    SqlConnection conn = new SqlConnection(" ConnectionStrin g");

    private void Page_Load(objec t sender, System.EventArg s e)
    {
    try
    {
    conn.Open();
    Response.Clear( );
    Response.Buffer = true;
    Response.Conten tType = "applicatio n/vnd.ms-excel";


    Response.Charse t = "";

    SqlCommand cmd = new SqlCommand("Sel ect CubicleId,Emplo yeeNo,EmployeeN ame,Project,Ema ilId,Department ,DirectNo from PuneEmployee", conn);
    SqlDataReader dr = cmd.ExecuteRead er();

    while (dr.Read())
    {
    string CubicleId = dr.GetValue(0). ToString();
    int EmployeeNo = Int32.Parse(dr. GetValue(1).ToS tring());
    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(Cub icleId, EmployeeNo, EmployeeName, Project, EmailId, Department, DirectNo);


    }
    dr.Close();
    conn.Close();
    }
    catch (Exception ex)
    {
    Response.Write( ex);
    }



    }

    private void AddTableRow(str ing 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.ToSt ring()));

    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.Ad d(row1);

    }
Working...