当我尝试导出到Excel时,页面内部选择了项目

时间:2012-04-17 17:19:08

标签: c# asp.net export-to-excel

我在Repeater中有一个GridView,我正在导出到Excel而没有问题。当我尝试使用DropDown.SelectedValue使用RowCreated事件放入GridView的头部时,我总是得到第一个项而不是SelectedValue。有人可以帮助我吗?

我有2个下拉列表ddlType和ddlYear并且它总是给我回到第一个项目的值:

if (e.Row.RowType == DataControlRowType.Header)
{
    if (ddlType.SelectedValue != "0")
        contador = int.Parse(ddlType.SelectedValue)-1;

    GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
    string HeaderBackColor = "#EDEDED";
    rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);

    Literal newCells = new Literal();
    newCells.Text = ddlYear.SelectedValue+@"</th>" +
     "</tr>" +
     "<tr bgcolor='" + HeaderBackColor + "'>";

    newCells.Text += @"<th colspan='8' style='width:100px' >" + typeA + "</th>" +

     "</tr>" +
     "<tr bgcolor='" + HeaderBackColor + "'>";



    switch (Nav)
    {
        case "0":
            newCells.Text += @"<th colspan='2' style='width:80px' >item1</th>
                               <th colspan='2' style='width:80px'>item2</th> 
                               <th colspan='2' style='width:80px'>item3</th>
                               <th colspan='2' style='width:80px'>tem4</th>";
            break;
        case "1":
            newCells.Text += @"<th colspan='2' style='width:80px' >item1</th>";
            break;
        case "2":
            newCells.Text += @"<th colspan='2' style='width:80px'>item2</th>";
            break;
        case "3":
            newCells.Text += @"<th colspan='2' style='width:80px'>item3</th>";
            break;
        case "4":
            newCells.Text += @"<th colspan='2' style='width:80px'>item4</th>";
            break;

    }

    newCells.Text += @"</tr><tr bgcolor='" + HeaderBackColor + "'>";

    if(Nav=="0")
        newCells.Text += @"<th>N</th>
                          <th>E</th>

                          <th>N</th>
                          <th>E</th>

                          <th>N</th>
                          <th>E</th>

                          <th>N</th>
                          <th>E";
    else
        newCells.Text += @"<th>N</th>
                           <th>E";

    TableCellCollection cells = e.Row.Cells;
    TableHeaderCell headerCell = new TableHeaderCell();

    headerCell.RowSpan = 4;
    headerCell.Controls.Add(newCells);

    headerCell.BackColor = System.Drawing.ColorTranslator.FromHtml("#6495ED");
    headerCell.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FFF");


    rowHeader.Cells.Add(headerCell);
    rowHeader.Visible = true;


    ((GridView)sender).Controls[0].Controls.AddAt(0, rowHeader);
}

我正在使用以下方法导出到Excel:

string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
MyRepeater.RenderControl(htw);

Response.Write(sw.ToString());
Response.End(); 

0 个答案:

没有答案