如何将C#excel导出中的复选框替换为1和0?

时间:2014-10-28 13:47:14

标签: c# asp.net sql-server excel checkbox

在C#应用程序中,我动态创建了GridView并将其导出到Excel。我遇到的问题是,由于三个字段是SQL中的位列,因此它们导出为复选框而不是1和0。我在google上搜索了类似的问题,并找到了this article这个解决方案。但是,由于右键或左键单击对象仅检查或取消选中该框,因此无法解决此问题。

我还试图找到有关如何将位字段从C#导出为1和0并且无法找到任何内容的信息。这是我的导出脚本:

StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
GridView gridView = new GridView();
gridView.DataSource = sdsResults;
gridView.AutoGenerateColumns = true;
gridView.DataBind();
gridView.HeaderRow.Style.Add("background-color", "#003c74");
gridView.HeaderRow.Style.Add("color", "#ffffff");
for (int i = 0; i < gridView.Rows.Count; i++)
        {
            GridViewRow row = gridView.Rows[i];

            //Change Color back to white
            row.BackColor = System.Drawing.Color.White;

            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");

            //Apply style to Individual Cells of Alternating Row
            if (i % 2 != 0)
            {
                row.BackColor = System.Drawing.Color.AliceBlue;
            }

            foreach(TableCell cell in row.Cells)
            {
                if(cell.HasControls() == true)
                {
                  if(cell.Controls[0].GetType().ToString() == "System.Web.Ui.WebControls.CheckBox")
                   {
                       CheckBox chk = (CheckBox)cell.Controls[0];
                        if(chk.Checked)
                        {
                           cell.Text = "1";
                        }
                        else
                        {
                            cell.Text = "0";
                        }
                   }
                }
            }
        }
gridView.RenderControl(htmlWriter);
htmlWriter.Close();

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=myfile.xls");
Response.Charset = "";
Response.Write(writer.ToString());
Response.End();
Response.End();

4 个答案:

答案 0 :(得分:0)

如果您使用SQL查询构建数据源,则将位列CAST为int或varchar。如果无法完成,请在分配数据源之前尝试使用所需的数据类型定义GridView列。

答案 1 :(得分:0)

在for循环中,您可以执行以下操作:

foreach(TableCell cell in row.Cells)
 {
   if(cell.HasControls() == true)
    {
      if(cell.Controls[0].GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
       {
           CheckBox chk = (CheckBox)cell.Controls[0];
           if(chk.Checked)
            {
               cell.Text = "1";
            }
           else{
                cell.Text = "0";
               }
       }
    }
 }

答案 2 :(得分:0)

protected void btnXlsExport_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    gvStandard.AllowPaging = false;
    gvStandard.DataBind();
    for (int i = 0; i <= gvStandard.Rows.Count - 1; i++) {
        GridViewRow row = gvStandard.Rows(i);
        foreach (TableCell cell in row.Cells) {
            if (cell.HasControls() == true) {
                if (cell.Controls(0).GetType().ToString() == "System.Web.UI.WebControls.CheckBox") {
                    CheckBox chk = (CheckBox)cell.Controls(0);
                    if (chk.Checked) {
                        cell.Text = "True";
                    } else {
                        cell.Text = "False";
                    }
                }
            }
        }
    }
    gvStandard.HeaderRow.Style.Add("background-color", "#FFFFFF");
    gvStandard.RenderControl(hw);
    Response.Output.Write(sw);
    Response.Flush();
    Response.End();
}

答案 3 :(得分:-1)

受保护的子btnXlsExport_Click(发件人为对象,e为EventArgs)处理btnXlsExport.Click

    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    gvStandard.AllowPaging = False
    gvStandard.DataBind()
    For i As Integer = 0 To gvStandard.Rows.Count - 1
        Dim row As GridViewRow = gvStandard.Rows(i)
        For Each cell As TableCell In row.Cells
            If cell.HasControls() = True Then
                If cell.Controls(0).[GetType]().ToString() = "System.Web.UI.WebControls.CheckBox" Then
                    Dim chk As CheckBox = DirectCast(cell.Controls(0), CheckBox)
                    If chk.Checked Then
                        cell.Text = "True"
                    Else
                        cell.Text = "False"
                    End If
                End If
            End If
        Next
    Next
    gvStandard.HeaderRow.Style.Add("background-color", "#FFFFFF")
    gvStandard.RenderControl(hw)
    Response.Output.Write(sw)
    Response.Flush()
    Response.End()
End Sub